| 36 |
kaklik |
1 |
# Written by Bill Bumgarner and Bram Cohen
|
|
|
2 |
# see LICENSE.txt for license information
|
|
|
3 |
|
|
|
4 |
from types import *
|
|
|
5 |
from cStringIO import StringIO
|
|
|
6 |
|
|
|
7 |
|
|
|
8 |
def splitLine(line, COLS=80, indent=10):
|
|
|
9 |
indent = " " * indent
|
|
|
10 |
width = COLS - (len(indent) + 1)
|
|
|
11 |
if indent and width < 15:
|
|
|
12 |
width = COLS - 2
|
|
|
13 |
indent = " "
|
|
|
14 |
s = StringIO()
|
|
|
15 |
i = 0
|
|
|
16 |
for word in line.split():
|
|
|
17 |
if i == 0:
|
|
|
18 |
s.write(indent+word)
|
|
|
19 |
i = len(word)
|
|
|
20 |
continue
|
|
|
21 |
if i + len(word) >= width:
|
|
|
22 |
s.write('\n'+indent+word)
|
|
|
23 |
i = len(word)
|
|
|
24 |
continue
|
|
|
25 |
s.write(' '+word)
|
|
|
26 |
i += len(word) + 1
|
|
|
27 |
return s.getvalue()
|
|
|
28 |
|
|
|
29 |
def formatDefinitions(options, COLS, presets = {}):
|
|
|
30 |
s = StringIO()
|
|
|
31 |
for (longname, default, doc) in options:
|
|
|
32 |
s.write('--' + longname + ' <arg>\n')
|
|
|
33 |
default = presets.get(longname, default)
|
|
|
34 |
if type(default) in (IntType, LongType):
|
|
|
35 |
try:
|
|
|
36 |
default = int(default)
|
|
|
37 |
except:
|
|
|
38 |
pass
|
|
|
39 |
if default is not None:
|
|
|
40 |
doc += ' (defaults to ' + repr(default) + ')'
|
|
|
41 |
s.write(splitLine(doc,COLS,10))
|
|
|
42 |
s.write('\n\n')
|
|
|
43 |
return s.getvalue()
|
|
|
44 |
|
|
|
45 |
|
|
|
46 |
def usage(str):
|
|
|
47 |
raise ValueError(str)
|
|
|
48 |
|
|
|
49 |
|
|
|
50 |
def defaultargs(options):
|
|
|
51 |
l = {}
|
|
|
52 |
for (longname, default, doc) in options:
|
|
|
53 |
if default is not None:
|
|
|
54 |
l[longname] = default
|
|
|
55 |
return l
|
|
|
56 |
|
|
|
57 |
|
|
|
58 |
def parseargs(argv, options, minargs = None, maxargs = None, presets = {}):
|
|
|
59 |
config = {}
|
|
|
60 |
longkeyed = {}
|
|
|
61 |
for option in options:
|
|
|
62 |
longname, default, doc = option
|
|
|
63 |
longkeyed[longname] = option
|
|
|
64 |
config[longname] = default
|
|
|
65 |
for longname in presets.keys(): # presets after defaults but before arguments
|
|
|
66 |
config[longname] = presets[longname]
|
|
|
67 |
options = []
|
|
|
68 |
args = []
|
|
|
69 |
pos = 0
|
|
|
70 |
while pos < len(argv):
|
|
|
71 |
if argv[pos][:2] != '--':
|
|
|
72 |
args.append(argv[pos])
|
|
|
73 |
pos += 1
|
|
|
74 |
else:
|
|
|
75 |
if pos == len(argv) - 1:
|
|
|
76 |
usage('parameter passed in at end with no value')
|
|
|
77 |
key, value = argv[pos][2:], argv[pos+1]
|
|
|
78 |
pos += 2
|
|
|
79 |
if not longkeyed.has_key(key):
|
|
|
80 |
usage('unknown key --' + key)
|
|
|
81 |
longname, default, doc = longkeyed[key]
|
|
|
82 |
try:
|
|
|
83 |
t = type(config[longname])
|
|
|
84 |
if t is NoneType or t is StringType:
|
|
|
85 |
config[longname] = value
|
|
|
86 |
elif t in (IntType, LongType):
|
|
|
87 |
config[longname] = long(value)
|
|
|
88 |
elif t is FloatType:
|
|
|
89 |
config[longname] = float(value)
|
|
|
90 |
else:
|
|
|
91 |
assert 0
|
|
|
92 |
except ValueError, e:
|
|
|
93 |
usage('wrong format of --%s - %s' % (key, str(e)))
|
|
|
94 |
for key, value in config.items():
|
|
|
95 |
if value is None:
|
|
|
96 |
usage("Option --%s is required." % key)
|
|
|
97 |
if minargs is not None and len(args) < minargs:
|
|
|
98 |
usage("Must supply at least %d args." % minargs)
|
|
|
99 |
if maxargs is not None and len(args) > maxargs:
|
|
|
100 |
usage("Too many args - %d max." % maxargs)
|
|
|
101 |
return (config, args)
|
|
|
102 |
|
|
|
103 |
def test_parseargs():
|
|
|
104 |
assert parseargs(('d', '--a', 'pq', 'e', '--b', '3', '--c', '4.5', 'f'), (('a', 'x', ''), ('b', 1, ''), ('c', 2.3, ''))) == ({'a': 'pq', 'b': 3, 'c': 4.5}, ['d', 'e', 'f'])
|
|
|
105 |
assert parseargs([], [('a', 'x', '')]) == ({'a': 'x'}, [])
|
|
|
106 |
assert parseargs(['--a', 'x', '--a', 'y'], [('a', '', '')]) == ({'a': 'y'}, [])
|
|
|
107 |
try:
|
|
|
108 |
parseargs([], [('a', 'x', '')])
|
|
|
109 |
except ValueError:
|
|
|
110 |
pass
|
|
|
111 |
try:
|
|
|
112 |
parseargs(['--a', 'x'], [])
|
|
|
113 |
except ValueError:
|
|
|
114 |
pass
|
|
|
115 |
try:
|
|
|
116 |
parseargs(['--a'], [('a', 'x', '')])
|
|
|
117 |
except ValueError:
|
|
|
118 |
pass
|
|
|
119 |
try:
|
|
|
120 |
parseargs([], [], 1, 2)
|
|
|
121 |
except ValueError:
|
|
|
122 |
pass
|
|
|
123 |
assert parseargs(['x'], [], 1, 2) == ({}, ['x'])
|
|
|
124 |
assert parseargs(['x', 'y'], [], 1, 2) == ({}, ['x', 'y'])
|
|
|
125 |
try:
|
|
|
126 |
parseargs(['x', 'y', 'z'], [], 1, 2)
|
|
|
127 |
except ValueError:
|
|
|
128 |
pass
|
|
|
129 |
try:
|
|
|
130 |
parseargs(['--a', '2.0'], [('a', 3, '')])
|
|
|
131 |
except ValueError:
|
|
|
132 |
pass
|
|
|
133 |
try:
|
|
|
134 |
parseargs(['--a', 'z'], [('a', 2.1, '')])
|
|
|
135 |
except ValueError:
|
|
|
136 |
pass
|
|
|
137 |
|