2005-07-11 04:01:11 +04:00
|
|
|
import getopt
|
2005-05-04 01:16:10 +04:00
|
|
|
|
2005-07-03 23:47:45 +04:00
|
|
|
def fancyopts(args, options, state):
|
2005-11-14 04:59:35 +03:00
|
|
|
long = []
|
|
|
|
short = ''
|
|
|
|
map = {}
|
|
|
|
dt = {}
|
2005-05-04 01:16:10 +04:00
|
|
|
|
|
|
|
for s, l, d, c in options:
|
2005-08-25 09:25:55 +04:00
|
|
|
pl = l.replace('-', '_')
|
|
|
|
map['-'+s] = map['--'+l] = pl
|
|
|
|
state[pl] = d
|
|
|
|
dt[pl] = type(d)
|
2006-12-01 21:18:16 +03:00
|
|
|
if (d is not None and d is not True and d is not False and
|
|
|
|
not callable(d)):
|
2005-08-20 06:56:20 +04:00
|
|
|
if s: s += ':'
|
|
|
|
if l: l += '='
|
2005-05-04 01:16:10 +04:00
|
|
|
if s: short = short + s
|
|
|
|
if l: long.append(l)
|
|
|
|
|
2005-06-09 08:11:57 +04:00
|
|
|
opts, args = getopt.getopt(args, short, long)
|
2005-05-04 01:16:10 +04:00
|
|
|
|
|
|
|
for opt, arg in opts:
|
2006-11-17 10:06:54 +03:00
|
|
|
if dt[map[opt]] is type(fancyopts): state[map[opt]](state, map[opt], arg)
|
2005-05-04 01:16:10 +04:00
|
|
|
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)
|
2006-12-01 10:42:52 +03:00
|
|
|
elif dt[map[opt]] is type(None): state[map[opt]] = True
|
2006-12-01 21:18:16 +03:00
|
|
|
elif dt[map[opt]] is type(False): state[map[opt]] = True
|
2005-06-01 12:25:42 +04:00
|
|
|
|
2005-05-04 01:16:10 +04:00
|
|
|
return args
|
|
|
|
|