Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathOptionParser.lua
More file actions
Latest commit
202 lines (189 loc) · 5.94 KB
/
Copy pathOptionParser.lua
File metadata and controls
202 lines (189 loc) · 5.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
--
-- Lua command line option parser.
-- Interface based on Pythons optparse.
-- http://docs.python.org/lib/module-optparse.html
-- (c) 2008 David Manura, Licensed under the same terms as Lua (MIT license)
--
-- To be used like this:
-- t={usage="<some usage message>", version="<version string>"}
-- op = xlua.OptionParser(t)
-- op:option{"<opt>", action=<action>, dest=<dest>, help="<help message for this option>"}
--
-- with :
-- <opt> the option string to be used (can be anything, if one letter opt, then should be -x val, more letters: -xy=val )
-- <action> one of
-- - store: store in options as key, val
-- - store_true: stores key, true
-- - store_false: stores key, false
-- <dest> is the key under which the option is saved
--
-- options,args = op.parse_args()
--
-- now options is the table of options (key, val) and args is the table with non-option arguments.
-- You can use op.fail(message) for failing and op.help() for printing the usage as you like.
--
-- modifed by Benoit Corda, Clement Farabet
--
localOptionParser= {}
functionxlua.OptionParser(usage)
localself= {}
self.usage=usage
self.option_descriptions= {}
self.option_of= {}
fork,vinpairs(OptionParser) do
self[k] =v
end
self:option{"-h", "--help", action="store_true", dest="help",
help="show this help message and exit"}
returnself
end
functionOptionParser:fail(s) -- extension
io.stderr:write(s..'\n')
self:help()
os.exit(1)
end
functionOptionParser:option(optdesc)
self.option_descriptions[#self.option_descriptions+1] =optdesc
for_,vinipairs(optdesc) do
self.option_of[v] =optdesc
end
end
functionOptionParser:parse(options)
localoptions=optionsor {}
localargs= {}
-- set the default
for_,vinipairs(self.option_descriptions) do
ifv.default~=nilandoptions[v.dest]==nilthen
options[v.dest] =v.default
end
end
ifnotargthen
options.__main__=false-- python like main
self.options=options
returnoptions, args
end
options.__main__=true-- python like main
-- expand options (e.g. "--input=file" -> "--input", "file")
localunpack=unpackortable.unpack
localarg= {unpack(arg)}
fori=#arg,1,-1dolocalv=arg[i]
localflag, val=v:match('^(%-%-%w+)=(.*)')
ifflagthen
arg[i] =flag
table.insert(arg, i+1, val)
end
end
locali=1
whilei<=#argdo
localv=arg[i]
localoptdesc=self.option_of[v]
ifoptdescthen
localdefault=optdesc.default
localaction=optdesc.action
localval=default
ifaction=='store' oraction==nilthen
i=i+1
val=arg[i] ordefault
ifnotvalthenself:fail('option requires an argument ' ..v) end
elseifaction=='store_true' then
val=true
elseifaction=='store_false' then
val=false
end
options[optdesc.dest] =val
else
ifv:match('^%-') thenself:fail('invalid option ' ..v) end
args[#args+1] =v
end
i=i+1
end
fork,optinpairs(self.option_of) do
ifopt.reqandnotoptions[opt.dest] then
self:fail('option '..k..' requires an argument ')
end
end
ifoptions.helpthen
self:help()
os.exit()
end
-- set the default if nil
self.options=options
returnoptions, args
end
functionOptionParser:flags(optdesc)
localsflags= {}
localaction=optdescandoptdesc.action
for_,flaginipairs(optdesc) do
localsflagend
ifaction==niloraction=='store' then
localmetavar=optdesc.metavaroroptdesc.dest:upper()
sflagend=#flag==2and'' ..metavar
or'=' ..metavar
else
sflagend=''
end
sflags[#sflags+1] =flag..sflagend
end
returntable.concat(sflags, ', ')
end
functionOptionParser:help()
ifarg[-1] then
io.stdout:write("Usage: " ..self.usage:gsub('%%prog', (arg[-1] ..'' ..arg[0])) .."\n")
elseifarg[0] then
io.stdout:write("Usage: " ..self.usage:gsub('%%prog', arg[0]) .."\n")
else
io.stdout:write("Usage: " ..self.usage:gsub('%%prog', 'THISPROG') .."\n")
end
io.stdout:write("\n")
io.stdout:write("Options:\n")
pad=0
for_,optdescinipairs(self.option_descriptions) do
pad=math.max(pad, #self:flags(optdesc))
end
for_,optdescinipairs(self.option_descriptions) do
localdefstr=''
ifoptdesc.reqthen
defstr=' [REQUIRED]'
elseifoptdesc.defaultthen
defstr=' [default = ' ..tostring(optdesc.default) ..']'
end
io.stdout:write("" ..self:flags(optdesc) ..
string.rep('', pad-#self:flags(optdesc)) ..
"" ..optdesc.help..defstr.."\n")
end
end
functionOptionParser:tostring(generatefilename, params)
localstr=''
ifnotgeneratefilenamethen
str='<'.. ((argandarg[0]) or'interpreted.lua'):gsub('.lua','') .."> configuration:\n"
fork,vinpairs(self.options) do
str=str..' + ' ..k..' = ' ..tostring(v) ..'\n'
end
else
localfirst=true
fori,entryinipairs(self.option_descriptions) do
localkey=entry[1]
localmatch=true
if#params>0then
match=false
fori,paraminipairs(params) do
ifkey==paramthenmatch=true; breakend
end
end
localval=self.options[entry.dest]
ifvalandmatchthen
iffirstthen
str=str..key..'=' ..tostring(val)
else
str=str..',' ..key..'=' ..tostring(val)
end
first=false
end
end
str=str:gsub('/','|'):gsub('','_')
end
returnstr
end
functionOptionParser:summarize(compact)
io.write(self:tostring(compact))
end