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