##// END OF EJS Templates
Make fancyopts handle no arguments...
mpm@selenic.com -
r164:2e87f048 default
parent child Browse files
Show More
@@ -1,51 +1,51 b''
1 1 import sys, os, getopt
2 2
3 def fancyopts(args, options, state, syntax=''):
3 def fancyopts(args, options, state, syntax='', minlen = 0):
4 4 long=[]
5 5 short=''
6 6 map={}
7 7 dt={}
8 8
9 9 def help(state, opt, arg, options=options, syntax=syntax):
10 10 print "Usage: ", syntax
11 11
12 12 for s, l, d, c in options:
13 13 opt=' '
14 14 if s: opt = opt + '-' + s + ' '
15 15 if l: opt = opt + '--' + l + ' '
16 16 if d: opt = opt + '(' + str(d) + ')'
17 17 print opt
18 18 if c: print ' %s' % c
19 19 sys.exit(0)
20 20
21 if len(args) == 0:
21 if len(args) < minlen:
22 22 help(state, None, args)
23 23
24 24 options=[('h', 'help', help, 'Show usage info')] + options
25 25
26 26 for s, l, d, c in options:
27 27 map['-'+s] = map['--'+l]=l
28 28 state[l] = d
29 29 dt[l] = type(d)
30 30 if not d is None and not type(d) is type(help): s, l=s+':', l+'='
31 31 if s: short = short + s
32 32 if l: long.append(l)
33 33
34 34 if os.environ.has_key("HG_OPTS"):
35 35 args = os.environ["HG_OPTS"].split() + args
36 36
37 37 try:
38 38 opts, args = getopt.getopt(args, short, long)
39 39 except getopt.GetoptError:
40 40 help(state, None, args)
41 41 sys.exit(-1)
42 42
43 43 for opt, arg in opts:
44 44 if dt[map[opt]] is type(help): state[map[opt]](state,map[opt],arg)
45 45 elif dt[map[opt]] is type(1): state[map[opt]] = int(arg)
46 46 elif dt[map[opt]] is type(''): state[map[opt]] = arg
47 47 elif dt[map[opt]] is type([]): state[map[opt]].append(arg)
48 48 elif dt[map[opt]] is type(None): state[map[opt]] = 1
49 49
50 50 return args
51 51
General Comments 0
You need to be logged in to leave comments. Login now