-
Notifications
You must be signed in to change notification settings - Fork 0
/
cliargs.py
87 lines (73 loc) · 2.74 KB
/
cliargs.py
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
# Richard Darst, 2006.
# GPLv2+
"""This program is an easy command line parser. The advantage is that
it is fast and easy, and that you don't have to specify what options
to use beforehand.
It returns a tuple consisting of:
- List of arguments
- Dictionary of options.
- All options have the form '--xxx'
- '--xxx' makes 'xxx' appear as a dict key with value of True.
- '--xxx=yyy' makes 'xxx' appear in the dict with a vaule of 'yyy'.
- '--' means 'no more options after this point'
See below for examples.
(rkd zgib.net)
"""
import sys
def get_cliargs(argv=None, multiopt=False, allowedargs=None):
"""Parse cli arguments.
Returns (args, options)"""
if argv is None:
argv = sys.argv
if type(argv) == str:
# print "automatically converting to a list using split"
argv = argv.split()
argv = [ x for x in argv ] # basically argv.copy(), if it existed
options = { }
args = [ ]
while len(argv) > 0:
nextarg = argv.pop(0)
if nextarg == "--": # standard for ending options
args.extend(argv)
break
if nextarg[0:2] != "--":
args.append(nextarg)
continue
nextarg = nextarg[2:]
if nextarg.find("=") == -1:
name = nextarg
val = True
else:
name, val = nextarg.split("=", 1)
if options.has_key(name):
if not multiopt:
raise Exception("already have this option: %s"%name)
# handling of multiarg mode here
if allowedargs and name not in allowedargs:
print "Error-- arg %s found and not expected"%name
break
options[name] = val
return args, options
if __name__ == "__main__":
# Example use:
sys.argv = ["program name", "--hi", "option", "--anoption", "--blah=xxx",
# the "--" means "everything past this point is not an option"
"--", "--notanoption"]
args, options = get_cliargs()
print args
# --> ['program name', 'option', '--notanoption']
print options
# --> {'blah': 'xxx', 'hi': True, 'anoption': True}
print
print get_cliargs("--hi")
# --> ([], {'hi': True})
print get_cliargs("--hi --whathappen")
# --> ([], {'hi': True, 'whathappen': True})
print get_cliargs("--hi --withoption=one")
# --> ([], {'withoption': 'one', 'hi': True})
print get_cliargs("--hi option --anoption")
# --> (['option'], {'hi': True, 'anoption': True})
print get_cliargs("--hi option --anoption -- --notanoption")
# --> (['option', '--notanoption'], {'hi': True, 'anoption': True})
print get_cliargs(["--hi", "option", "--anoption", "--", "--notanoption"])
# --> (['option', '--notanoption'], {'hi': True, 'anoption': True})