liquid_feedback_frontend

view utils/optparse.lua @ 1040:d35d84938556

Added tag v2.2.5 for changeset aa6ed1b99297
author jbe
date Wed Aug 14 20:37:57 2013 +0200 (2013-08-14)
parents eeb167cf9dc4
children
line source
1 -- Lua command line option parser.
2 -- Interface based on Pythons optparse.
3 -- http://docs.python.org/lib/module-optparse.html
4 -- (c) 2008 David Manura, Licensed under the same terms as Lua (MIT license)
5 --
6 -- To be used like this:
7 -- t={usage="<some usage message>", version="<version string>"}
8 -- op=OptionParser(t)
9 -- op=add_option{"<opt>", action=<action>, dest=<dest>, help="<help message for this option>"}
10 --
11 -- with :
12 -- <opt> the option string to be used (can be anything, if one letter opt, then should be -x val, more letters: -xy=val )
13 -- <action> one of
14 -- - store: store in options as key, val
15 -- - store_true: stores key, true
16 -- - store_false: stores key, false
17 -- <dest> is the key under which the option is saved
18 --
19 -- options,args = op.parse_args()
20 --
21 -- now options is the table of options (key, val) and args is the table with non-option arguments.
22 -- You can use op.fail(message) for failing and op.print_help() for printing the usage as you like.
24 --module('utils.optparse')
26 local function OptionParser(t)
27 local usage = t.usage
28 local version = t.version
29 local commands = t.commands
31 local o = {}
32 local option_descriptions = {}
33 local option_of = {}
35 function o.fail(s) -- extension
36 io.stderr:write(s .. '\n')
37 os.exit(1)
38 end
40 function o.add_option(optdesc)
41 option_descriptions[#option_descriptions+1] = optdesc
42 for _,v in pairs(optdesc) do
43 option_of[v] = optdesc
44 end
45 end
46 function o.parse_args()
47 -- expand options (e.g. "--input=file" -> "--input", "file")
48 local arg = {unpack(arg)}
49 for i=#arg,1,-1 do local v = arg[i]
50 local flag, val = v:match('^(%-%-%w+)=(.*)')
51 if flag then
52 arg[i] = flag
53 table.insert(arg, i+1, val)
54 end
55 end
57 local options = {}
58 for _,optdesc in ipairs(option_descriptions) do
59 options[optdesc["dest"]] = optdesc.default
60 end
61 local args = {}
62 local i = 1
63 while i <= #arg do local v = arg[i]
64 local optdesc = option_of[v]
65 if optdesc then
66 local action = optdesc.action
67 local val
68 if action == 'store' or action == nil then
69 i = i + 1
70 val = arg[i]
71 if not val then o.fail('option requires an argument ' .. v) end
72 elseif action == 'store_true' then
73 val = true
74 elseif action == 'store_false' then
75 val = false
76 end
77 options[optdesc.dest] = val
78 else
79 if v:match('^%-') then o.fail('invalid option ' .. v) end
80 args[#args+1] = v
81 end
82 i = i + 1
83 end
84 if options.help then
85 o.print_help()
86 os.exit()
87 end
88 if options.version then
89 io.stdout:write(t.version .. "\n")
90 os.exit()
91 end
92 return options, args
93 end
95 local function flags_str(optdesc)
96 local sflags = {}
97 local action = optdesc.action
98 for _,flag in ipairs(optdesc) do
99 local sflagend
100 if action == nil or action == 'store' then
101 local metavar = optdesc.metavar or optdesc.dest:upper()
102 sflagend = #flag == 2 and ' ' .. metavar
103 or '=' .. metavar
104 else
105 sflagend = ''
106 end
107 sflags[#sflags+1] = flag .. sflagend
108 end
109 return table.concat(sflags, ', ')
110 end
112 function o.print_help()
113 io.stdout:write("Usage: " .. usage:gsub('%%prog', arg[0]) .. "\n")
114 io.stdout:write("\n")
115 io.stdout:write("Options:\n")
116 for _,optdesc in ipairs(option_descriptions) do
117 io.stdout:write(" " .. flags_str(optdesc) ..
118 " " .. optdesc.help .. "\n")
119 end
120 if commands then
121 io.stdout:write("\nCommands:\n")
122 for _,command in ipairs(commands) do
123 io.stdout:write(" " .. command[1] ..
124 string.rep(" ", 30-#command[1]) ..
125 command[2] .. "\n")
126 end
127 end
129 end
130 o.add_option{"--help", action="store_true", dest="help",
131 help="show this help message and exit"}
132 if t.version then
133 o.add_option{"--version", action="store_true", dest="version",
134 help="output version info."}
135 end
136 return o
137 end
139 return OptionParser

Impressum / About Us