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