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