-
Notifications
You must be signed in to change notification settings - Fork 52
/
cliargs.py
executable file
·106 lines (87 loc) · 3.2 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# Geodict
# Copyright (C) 2010 Pete Warden <[email protected]>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import sys, string
def print_usage_and_exit(cliargs):
print "Usage:"
for long, arginfo in cliargs.items():
short = arginfo['short']
type = arginfo['type']
required = (type=='required')
optional = (type=='optional')
description = arginfo['description']
output = '-'+short+'/--'+long+' '
if optional or required:
output += '<value> '
output += ': '+description
if required:
output += ' (required)'
print output
exit()
def get_options(cliargs):
options = {'unnamed': [] }
skip_next = False
for index in range(1, len(sys.argv)):
if skip_next:
skip_next = False
continue
currentarg = sys.argv[index].lower()
argparts = currentarg.split('=')
namepart = argparts[0]
if namepart.startswith('--'):
longname = namepart[2:]
elif namepart.startswith('-'):
shortname = namepart[1:]
longname = shortname
for name, info in cliargs.items():
if shortname==info['short']:
longname = name
break
else:
longname = 'unnamed'
if longname=='unnamed':
options['unnamed'].append(namepart)
else:
if longname not in cliargs:
print "Unknown argument '"+longname+"'"
print_usage_and_exit(cliargs)
arginfo = cliargs[longname]
argtype = arginfo['type']
if argtype=='switch':
value = True
elif len(argparts) > 1:
value = argparts[1]
elif (index+1) < len(sys.argv):
value = sys.argv[index+1]
skip_next = True
else:
print "Missing value after '"+longname+"'"
print_usage_and_exit(cliargs)
options[longname] = value
for longname, arginfo in cliargs.items():
type = arginfo['type']
if longname not in options:
if type == 'required':
print "Missing required value for '"+longname+"'"
print_usage_and_exit(cliargs)
elif type == 'optional':
if not 'default' in arginfo:
die('Missing default value for '+longname)
options[longname] = arginfo['default']
elif type == 'switch':
options[longname] = False
else:
die('Unknown type "'+type+'" for '+longname)
return options