forked from rlowrance/re
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommandLine.lua
More file actions
64 lines (55 loc) · 1.78 KB
/
Copy pathCommandLine.lua
File metadata and controls
64 lines (55 loc) · 1.78 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
-- CommandLine.lua
-- parse command line with only '--NAME VALUE' arguments
-- NOTE: Can later be extended to include other types of arguments
-- API overview
if false then
cl = CommandLine(arg)
flag = cl.isPresent('--flag') -- true or false
strOrNil = cl.maybeValue('--flag') -- if not present, return nil
arg1 = cl.required('--arg1') -- errors if not present
arg2 = cl.defaultable('--arg2', 'default value')
end
require 'makeVp'
require 'parseCommandLine'
require 'torch'
require 'validateAttributes'
-- construction
local CommandLine = torch.class('CommandLine')
function CommandLine:__init(arg)
validateAttributes(arg, 'table')
self.arg = arg
end
-- isPresent(key)
function CommandLine:isPresent(key)
local vp = makeVp(0, 'CommandLine:isPresent')
vp(1, 'key', key)
validateAttributes(key, 'string')
return parseCommandLine(self.arg, 'present', key)
end
-- maybeValue(key)
function CommandLine:maybeValue(key)
validateAttributes(key, 'string')
return parseCommandLine(self.arg, 'value', key) -- return nil if not present
end
-- required(key)
function CommandLine:required(key)
validateAttributes(key, 'string')
local str = parseCommandLine(self.arg, 'value', key)
assert(str ~= nil, 'missing keyword ' .. key)
return str
end
-- defaultable(key, defaultValue)
function CommandLine:defaultable(key, defaultValue)
local vp = makeVp(0, 'CommandLine:defaultable')
vp(1, 'key', key, 'defaultValue', defaultValue)
validateAttributes(key, 'string')
validateAttributes(defaultValue, {'string', nil})
local str = parseCommandLine(self.arg, 'value', key)
if str == nil then
vp(2, 'returning default value', defaultValue)
return defaultValue
else
vp(2, 'returning supplied value', str)
return str
end
end