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