forked from torch/torch7
-
Notifications
You must be signed in to change notification settings - Fork 1
/
CmdLine.lua
269 lines (242 loc) · 7.05 KB
/
CmdLine.lua
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
local CmdLine = torch.class('torch.CmdLine')
local function strip(str)
return string.match(str, '%-*(.*)')
end
local function pad(str, sz)
return str .. string.rep(' ', sz-#str)
end
function CmdLine:error(msg)
print('')
io.stderr:write(msg)
print('')
self:help()
os.exit(1)
end
function CmdLine:__readArgument__(params, arg, i, nArgument)
local argument = self.arguments[nArgument]
local value = arg[i]
if nArgument > #self.arguments then
self:error('invalid argument: ' .. value)
end
if argument.type and type(value) ~= argument.type then
self:error('invalid argument type for argument ' .. argument.key .. ' (should be ' .. argument.type .. ')')
end
params[strip(argument.key)] = value
return 1
end
function CmdLine:__readOption__(params, arg, i)
local key = arg[i]
local option = self.options[key]
if not option then
self:error('unknown option ' .. key)
end
if option.type and option.type == 'boolean' then
params[strip(key)] = not option.default
return 1
else
local value = arg[i+1]
if not value then
self:error('missing argument for option ' .. key)
end
if not option.type or option.type == 'string' then
elseif option.type == 'number' then
value = tonumber(value)
else
self:error('unknown required option type ' .. option.type)
end
if not value then
self:error('invalid type for option ' .. key .. ' (should be ' .. option.type .. ')')
end
params[strip(key)] = value
return 2
end
end
function CmdLine:__init(argseparator_,keyseparator_)
self.argseparator = argseparator_ or ','
self.keyseparator = keyseparator_ or '='
self.options = {}
self.arguments = {}
self.helplines = {}
self.dateformat = nil
self.silentio = false
end
function CmdLine:silent()
self.silentio = true
end
function CmdLine:addTime(name, format)
format = format or '%Y-%m-%d %H:%M:%S'
if type(format) ~= 'string' then
error('Argument has to be string')
end
if name ~= nil then
name = '[' .. name .. ']: '
else
name = ''
end
self.dateformat = format .. name
end
function CmdLine:argument(key, help, _type_)
table.insert(self.arguments, {key=key, help=help, type=_type_})
table.insert(self.helplines, self.arguments[#self.arguments])
end
function CmdLine:option(key, default, help, _type_)
if default == nil then
error('option ' .. key .. ' has no default value')
end
_type_ = _type_ or type(default)
if type(default) ~= _type_ then
error('option ' .. key .. ' has wrong default type value')
end
self.options[key] = {key=key, default=default, help=help, type=_type_}
table.insert(self.helplines, self.options[key])
end
function CmdLine:default()
local params = {}
for option,v in pairs(self.options) do
params[strip(option)] = v.default
end
return params
end
function CmdLine:parse(arg)
local i = 1
local params = self:default()
local nArgument = 0
while i <= #arg do
if arg[i] == '-help' or arg[i] == '-h' or arg[i] == '--help' then
self:help(arg)
os.exit(0)
end
if self.options[arg[i]] then
i = i + self:__readOption__(params, arg, i)
else
nArgument = nArgument + 1
i = i + self:__readArgument__(params, arg, i, nArgument)
end
end
if nArgument ~= #self.arguments then
self:error('not enough arguments')
end
return params
end
function CmdLine:string(prefix, params, ignore)
local arguments = {}
local options = {}
prefix = prefix or ''
for k,v in pairs(params) do
if ignore[k] then
print('-- ignore option ' .. k)
elseif self.options['-' .. k] then
if v ~= self.options['-' .. k].default or ignore[k] == false then
if type(v) == 'boolean' then
if v then
v = 't'
else
v = 'f'
end
end
table.insert(options, k .. self.keyseparator .. v)
print(k,v,self.options['-' .. k].default)
end
else
local narg
for i=1,#self.arguments do
if strip(self.arguments[i].key) == k then
narg = i
end
end
if narg then
arguments[narg] = k .. self.keyseparator .. v
else
print('WARNING: unknown option/argument: ' .. k .. ' IGNORING for DIRECTORY NAME')
end
end
end
table.sort(options)
local str = table.concat(arguments, self.argseparator)
if str == '' then
str = table.concat(options, self.argseparator)
else
str = str .. self.argseparator .. table.concat(options, self.argseparator)
end
if str == '' then
return prefix
else
return prefix .. self.argseparator .. str
end
end
local oprint = nil
function CmdLine:log(file, params)
local f = (io.type(file) == 'file' and file) or io.open(file, 'w')
oprint = oprint or print -- get the current print function lazily
function print(...)
local n = select("#", ...)
local arg = {...}
if not self.silentio then
oprint(...)
end
local str = {}
if self.dateformat then
table.insert(str, os.date(self.dateformat))
end
for i=1,n do
table.insert(str,tostring(arg[i]))
end
table.insert(str,'\n')
f:write(table.concat(str,' '))
f:flush()
end
print('[program started on ' .. os.date() .. ']')
print('[command line arguments]')
if params then
for k,v in pairs(params) do
print(k,v)
end
end
print('[----------------------]')
end
function CmdLine:text(txt)
txt = txt or ''
assert(type(txt) == 'string')
table.insert(self.helplines, txt)
end
function CmdLine:help(arg)
io.write('Usage: ')
if arg then io.write(arg[0] .. ' ') end
io.write('[options]')
for i=1,#self.arguments do
io.write(' <' .. strip(self.arguments[i].key) .. '>')
end
io.write('\n')
-- first pass to compute max length
local optsz = 0
for _,option in ipairs(self.helplines) do
if type(option) == 'table' then
if option.default ~= nil then -- it is an option
if #option.key > optsz then
optsz = #option.key
end
else -- it is an argument
if #strip(option.key)+2 > optsz then
optsz = #strip(option.key)+2
end
end
end
end
-- second pass to print
for _,option in ipairs(self.helplines) do
if type(option) == 'table' then
io.write(' ')
if option.default ~= nil then -- it is an option
io.write(pad(option.key, optsz))
if option.help then io.write(' ' .. option.help) end
io.write(' [' .. tostring(option.default) .. ']')
else -- it is an argument
io.write(pad('<' .. strip(option.key) .. '>', optsz))
if option.help then io.write(' ' .. option.help) end
end
else
io.write(option) -- just some additional help
end
io.write('\n')
end
end