fancyopts.py
29 lines
| 801 B
| text/x-python
|
PythonLexer
/ mercurial / fancyopts.py
mark.williamson@cl.cam.ac.uk
|
r667 | import getopt | ||
mpm@selenic.com
|
r0 | |||
mpm@selenic.com
|
r596 | def fancyopts(args, options, state): | ||
mpm@selenic.com
|
r0 | long=[] | ||
short='' | ||||
map={} | ||||
dt={} | ||||
for s, l, d, c in options: | ||||
map['-'+s] = map['--'+l]=l | ||||
state[l] = d | ||||
dt[l] = type(d) | ||||
mpm@selenic.com
|
r959 | if not d is None and not callable(d): | ||
if s: s += ':' | ||||
if l: l += '=' | ||||
mpm@selenic.com
|
r0 | if s: short = short + s | ||
if l: long.append(l) | ||||
mpm@selenic.com
|
r293 | opts, args = getopt.getopt(args, short, long) | ||
mpm@selenic.com
|
r0 | |||
for opt, arg in opts: | ||||
mpm@selenic.com
|
r293 | if dt[map[opt]] is type(fancyopts): state[map[opt]](state,map[opt],arg) | ||
mpm@selenic.com
|
r0 | elif dt[map[opt]] is type(1): state[map[opt]] = int(arg) | ||
elif dt[map[opt]] is type(''): state[map[opt]] = arg | ||||
elif dt[map[opt]] is type([]): state[map[opt]].append(arg) | ||||
elif dt[map[opt]] is type(None): state[map[opt]] = 1 | ||||
mpm@selenic.com
|
r209 | |||
mpm@selenic.com
|
r0 | return args | ||