Show More
@@ -1,103 +1,110 | |||||
|
1 | # fancyopts.py - better command line parsing | |||
|
2 | # | |||
|
3 | # Copyright 2005-2009 Matt Mackall <mpm@selenic.com> and others | |||
|
4 | # | |||
|
5 | # This software may be used and distributed according to the terms of the | |||
|
6 | # GNU General Public License version 2, incorporated herein by reference. | |||
|
7 | ||||
1 | import getopt |
|
8 | import getopt | |
2 |
|
9 | |||
3 | def gnugetopt(args, options, longoptions): |
|
10 | def gnugetopt(args, options, longoptions): | |
4 | """Parse options mostly like getopt.gnu_getopt. |
|
11 | """Parse options mostly like getopt.gnu_getopt. | |
5 |
|
12 | |||
6 | This is different from getopt.gnu_getopt in that an argument of - will |
|
13 | This is different from getopt.gnu_getopt in that an argument of - will | |
7 | become an argument of - instead of vanishing completely. |
|
14 | become an argument of - instead of vanishing completely. | |
8 | """ |
|
15 | """ | |
9 | extraargs = [] |
|
16 | extraargs = [] | |
10 | if '--' in args: |
|
17 | if '--' in args: | |
11 | stopindex = args.index('--') |
|
18 | stopindex = args.index('--') | |
12 | extraargs = args[stopindex+1:] |
|
19 | extraargs = args[stopindex+1:] | |
13 | args = args[:stopindex] |
|
20 | args = args[:stopindex] | |
14 | opts, parseargs = getopt.getopt(args, options, longoptions) |
|
21 | opts, parseargs = getopt.getopt(args, options, longoptions) | |
15 | args = [] |
|
22 | args = [] | |
16 | while parseargs: |
|
23 | while parseargs: | |
17 | arg = parseargs.pop(0) |
|
24 | arg = parseargs.pop(0) | |
18 | if arg and arg[0] == '-' and len(arg) > 1: |
|
25 | if arg and arg[0] == '-' and len(arg) > 1: | |
19 | parseargs.insert(0, arg) |
|
26 | parseargs.insert(0, arg) | |
20 | topts, newparseargs = getopt.getopt(parseargs, options, longoptions) |
|
27 | topts, newparseargs = getopt.getopt(parseargs, options, longoptions) | |
21 | opts = opts + topts |
|
28 | opts = opts + topts | |
22 | parseargs = newparseargs |
|
29 | parseargs = newparseargs | |
23 | else: |
|
30 | else: | |
24 | args.append(arg) |
|
31 | args.append(arg) | |
25 | args.extend(extraargs) |
|
32 | args.extend(extraargs) | |
26 | return opts, args |
|
33 | return opts, args | |
27 |
|
34 | |||
28 |
|
35 | |||
29 | def fancyopts(args, options, state, gnu=False): |
|
36 | def fancyopts(args, options, state, gnu=False): | |
30 | """ |
|
37 | """ | |
31 | read args, parse options, and store options in state |
|
38 | read args, parse options, and store options in state | |
32 |
|
39 | |||
33 | each option is a tuple of: |
|
40 | each option is a tuple of: | |
34 |
|
41 | |||
35 | short option or '' |
|
42 | short option or '' | |
36 | long option |
|
43 | long option | |
37 | default value |
|
44 | default value | |
38 | description |
|
45 | description | |
39 |
|
46 | |||
40 | option types include: |
|
47 | option types include: | |
41 |
|
48 | |||
42 | boolean or none - option sets variable in state to true |
|
49 | boolean or none - option sets variable in state to true | |
43 | string - parameter string is stored in state |
|
50 | string - parameter string is stored in state | |
44 | list - parameter string is added to a list |
|
51 | list - parameter string is added to a list | |
45 | integer - parameter strings is stored as int |
|
52 | integer - parameter strings is stored as int | |
46 | function - call function with parameter |
|
53 | function - call function with parameter | |
47 |
|
54 | |||
48 | non-option args are returned |
|
55 | non-option args are returned | |
49 | """ |
|
56 | """ | |
50 | namelist = [] |
|
57 | namelist = [] | |
51 | shortlist = '' |
|
58 | shortlist = '' | |
52 | argmap = {} |
|
59 | argmap = {} | |
53 | defmap = {} |
|
60 | defmap = {} | |
54 |
|
61 | |||
55 | for short, name, default, comment in options: |
|
62 | for short, name, default, comment in options: | |
56 | # convert opts to getopt format |
|
63 | # convert opts to getopt format | |
57 | oname = name |
|
64 | oname = name | |
58 | name = name.replace('-', '_') |
|
65 | name = name.replace('-', '_') | |
59 |
|
66 | |||
60 | argmap['-' + short] = argmap['--' + oname] = name |
|
67 | argmap['-' + short] = argmap['--' + oname] = name | |
61 | defmap[name] = default |
|
68 | defmap[name] = default | |
62 |
|
69 | |||
63 | # copy defaults to state |
|
70 | # copy defaults to state | |
64 | if isinstance(default, list): |
|
71 | if isinstance(default, list): | |
65 | state[name] = default[:] |
|
72 | state[name] = default[:] | |
66 | elif callable(default): |
|
73 | elif callable(default): | |
67 | state[name] = None |
|
74 | state[name] = None | |
68 | else: |
|
75 | else: | |
69 | state[name] = default |
|
76 | state[name] = default | |
70 |
|
77 | |||
71 | # does it take a parameter? |
|
78 | # does it take a parameter? | |
72 | if not (default is None or default is True or default is False): |
|
79 | if not (default is None or default is True or default is False): | |
73 | if short: short += ':' |
|
80 | if short: short += ':' | |
74 | if oname: oname += '=' |
|
81 | if oname: oname += '=' | |
75 | if short: |
|
82 | if short: | |
76 | shortlist += short |
|
83 | shortlist += short | |
77 | if name: |
|
84 | if name: | |
78 | namelist.append(oname) |
|
85 | namelist.append(oname) | |
79 |
|
86 | |||
80 | # parse arguments |
|
87 | # parse arguments | |
81 | if gnu: |
|
88 | if gnu: | |
82 | parse = gnugetopt |
|
89 | parse = gnugetopt | |
83 | else: |
|
90 | else: | |
84 | parse = getopt.getopt |
|
91 | parse = getopt.getopt | |
85 | opts, args = parse(args, shortlist, namelist) |
|
92 | opts, args = parse(args, shortlist, namelist) | |
86 |
|
93 | |||
87 | # transfer result to state |
|
94 | # transfer result to state | |
88 | for opt, val in opts: |
|
95 | for opt, val in opts: | |
89 | name = argmap[opt] |
|
96 | name = argmap[opt] | |
90 | t = type(defmap[name]) |
|
97 | t = type(defmap[name]) | |
91 | if t is type(fancyopts): |
|
98 | if t is type(fancyopts): | |
92 | state[name] = defmap[name](val) |
|
99 | state[name] = defmap[name](val) | |
93 | elif t is type(1): |
|
100 | elif t is type(1): | |
94 | state[name] = int(val) |
|
101 | state[name] = int(val) | |
95 | elif t is type(''): |
|
102 | elif t is type(''): | |
96 | state[name] = val |
|
103 | state[name] = val | |
97 | elif t is type([]): |
|
104 | elif t is type([]): | |
98 | state[name].append(val) |
|
105 | state[name].append(val) | |
99 | elif t is type(None) or t is type(False): |
|
106 | elif t is type(None) or t is type(False): | |
100 | state[name] = True |
|
107 | state[name] = True | |
101 |
|
108 | |||
102 | # return unparsed args |
|
109 | # return unparsed args | |
103 | return args |
|
110 | return args |
General Comments 0
You need to be logged in to leave comments.
Login now