Advertisement
ElvishJerricco

argparse

Sep 8th, 2014
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.63 KB | None | 0 0
  1. ------------------------------------------------------------------
  2. -- Parameter
  3. ------------------------------------------------------------------
  4.  
  5. local Parameter = {}
  6. function Parameter:matches(arg, options, tArgs)
  7.     if arg:sub(1,1) ~= "-" then
  8.         return false
  9.     end
  10.     arg = arg:sub(2)
  11.    
  12.     if not (arg:find("^"..self.name.."$") or arg:find("^"..self.sShortcut.."$")) then
  13.         return false
  14.     end
  15.  
  16.     local val = table.remove(tArgs, 1)
  17.  
  18.     if self.isMulti then
  19.         options[self.name] = options[self.name] or {}
  20.         table.insert(options[self.name], val)
  21.     else
  22.         options[self.name] = val
  23.     end
  24.  
  25.     return true
  26. end
  27.  
  28. function Parameter:shortcut(shortcut)
  29.     self.sShortcut = shortcut
  30.     return self
  31. end
  32.  
  33. function Parameter:multi()
  34.     self.isMulti = true
  35.     return self
  36. end
  37.  
  38. ------------------------------------------------------------------
  39. -- Switch
  40. ------------------------------------------------------------------
  41.  
  42. local Switch = {}
  43. function Switch:matches(arg, options, tArgs)
  44.     if arg:sub(1,1) ~= "-" then
  45.         return false
  46.     end
  47.     arg = arg:sub(2)
  48.    
  49.     if not (arg:find("^"..self.name.."$") or arg:find("^"..self.sShortcut.."$")) then
  50.         return false
  51.     end
  52.  
  53.     options[self.name] = true
  54.     return true
  55. end
  56.  
  57. function Switch:shortcut(shortcut)
  58.     self.sShortcut = shortcut
  59.     return self
  60. end
  61.  
  62. ------------------------------------------------------------------
  63. -- Argument
  64. ------------------------------------------------------------------
  65.  
  66. local Argument = {}
  67. function Argument:matches(arg, options, tArgs)
  68.     if self.matched then
  69.         return false
  70.     end
  71.  
  72.     if self.nCount == 1 then
  73.         options[self.name] = arg
  74.     else
  75.         local count = self.nCount
  76.         if count == "*" then
  77.             count = #tArgs
  78.         else
  79.             count = count - 1
  80.         end
  81.         local args = {arg}
  82.         for i=1, count do
  83.             table.insert(args, table.remove(tArgs, 1))
  84.         end
  85.         options[self.name] = args
  86.     end
  87.  
  88.     self.matched = true
  89.     return true
  90. end
  91.  
  92. function Argument:count(count)
  93.     assert(type(count) == "number" or count == "*", "Bad argument to Argument:count. Expected number, got " .. count)
  94.     self.nCount = count
  95.     return self
  96. end
  97.  
  98. ------------------------------------------------------------------
  99. -- Parser
  100. ------------------------------------------------------------------
  101.  
  102. local Parser = {}
  103. function Parser:parameter(name)
  104.     local param = setmetatable({name=name,sShortcut=name}, {__index=Parameter})
  105.     table.insert(self.parameters, param)
  106.     return param
  107. end
  108.  
  109. function Parser:switch(name)
  110.     local switch = setmetatable({name=name,sShortcut=name}, {__index=Switch})
  111.     table.insert(self.switches, switch)
  112.     return switch
  113. end
  114.  
  115. function Parser:argument(name)
  116.     local arg = setmetatable({name=name,nCount=1}, {__index=Argument})
  117.     table.insert(self.arguments, arg)
  118.     return arg
  119. end
  120.  
  121. function Parser:usage(str)
  122.     self.sUsage = str
  123. end
  124.  
  125. function Parser:printUsage()
  126.     print(self.sUsage)
  127. end
  128.  
  129. function Parser:parseArg(arg, options, tArgs)
  130.     for i,v in ipairs(self.parameters) do
  131.         if v:matches(arg, options, tArgs) then
  132.             return true
  133.         end
  134.     end
  135.     for i,v in ipairs(self.switches) do
  136.         if v:matches(arg, options, tArgs) then
  137.             return true
  138.         end
  139.     end
  140.     for i,v in ipairs(self.arguments) do
  141.         if v:matches(arg, options, tArgs) then
  142.             return true
  143.         end
  144.     end
  145.     return false
  146. end
  147.  
  148. function Parser:parse(options, ...)
  149.     local tArgs = {...}
  150.     for arg in function() return table.remove(tArgs, 1) end do
  151.         if not self:parseArg(arg, options, tArgs) then
  152.             print(tArgs.error or ("Unknown argument: "..arg))
  153.             self:printUsage()
  154.             return false
  155.         end
  156.     end
  157.     return options
  158. end
  159.  
  160. function new()
  161.     local parser = setmetatable({parameters={},switches={},arguments={}}, {__index=Parser})
  162.     return parser
  163. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement