My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
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
import sys, getopt

def parse_command_line():
"""
add usage text here
"""
optdict = {
'args' : [],
'arg_required' : False, # a:
'not_required' : False, # n
'required' : False, # r
'foo' : 'bar', # foo=
}

try:
args = sys.argv[1:]
optlist, args = getopt.getopt(args, 'a:nr', ['foo='])
except Exception, e:
f = "\n\tERROR:%s\n%s\n"
sys.stderr.write(f % (e, parse_command_line.__doc__))
sys.exit(1)

for opt,val in optlist:
if opt == '--foo':
optdict['foo'] = val
elif opt == '-a':
optdict['arg_required'] = val
elif opt == '-n':
optdict['not_required'] = True
elif opt == '-r':
optdict['required'] = True

optdict['args'] = args

if not optdict['arg_required'] or not optdict['required']:
f = "\n\t-a=<val> and -r parameters required to run\n%s\n"
sys.stderr.write(f % (parse_command_line.__doc__))
sys.exit(1)

return optdict

optdict = parse_command_line()
print optdict

Change log

r283 by klenwell on Jan 27, 2012   Diff
renames python parseopt example to getopt
Go to: 
Project members, sign in to write a code review

Older revisions

r282 by klenwell on Jan 27, 2012   Diff
adds python get opt example
All revisions of this file

File info

Size: 1142 bytes, 44 lines

File properties

svn:mergeinfo
Powered by Google Project Hosting