blob: 61dd5fc58edbe5343fa41cbc65cdce8de9eb3385 [file] [log] [blame]
Tarek Ziade1231a4e2011-05-19 13:07:25 +02001"""Command line parsing machinery.
2
3The FancyGetopt class is a Wrapper around the getopt module that
4provides the following additional features:
5 * short and long options are tied together
6 * options have help strings, so fancy_getopt could potentially
7 create a complete usage summary
8 * options set attributes of a passed-in object.
9
10It is used under the hood by the command classes. Do not use directly.
11"""
12
13import getopt
14import re
15import sys
Tarek Ziade1231a4e2011-05-19 13:07:25 +020016import textwrap
17
18from packaging.errors import PackagingGetoptError, PackagingArgError
19
20# Much like command_re in packaging.core, this is close to but not quite
21# the same as a Python NAME -- except, in the spirit of most GNU
22# utilities, we use '-' in place of '_'. (The spirit of LISP lives on!)
23# The similarities to NAME are again not a coincidence...
24longopt_pat = r'[a-zA-Z](?:[a-zA-Z0-9-]*)'
25longopt_re = re.compile(r'^%s$' % longopt_pat)
26
27# For recognizing "negative alias" options, eg. "quiet=!verbose"
28neg_alias_re = re.compile("^(%s)=!(%s)$" % (longopt_pat, longopt_pat))
29
30
31class FancyGetopt:
32 """Wrapper around the standard 'getopt()' module that provides some
33 handy extra functionality:
34 * short and long options are tied together
35 * options have help strings, and help text can be assembled
36 from them
37 * options set attributes of a passed-in object
38 * boolean options can have "negative aliases" -- eg. if
39 --quiet is the "negative alias" of --verbose, then "--quiet"
40 on the command line sets 'verbose' to false
41 """
42
43 def __init__(self, option_table=None):
44
45 # The option table is (currently) a list of tuples. The
46 # tuples may have 3 or four values:
47 # (long_option, short_option, help_string [, repeatable])
48 # if an option takes an argument, its long_option should have '='
49 # appended; short_option should just be a single character, no ':'
50 # in any case. If a long_option doesn't have a corresponding
51 # short_option, short_option should be None. All option tuples
52 # must have long options.
53 self.option_table = option_table
54
55 # 'option_index' maps long option names to entries in the option
56 # table (ie. those 3-tuples).
57 self.option_index = {}
58 if self.option_table:
59 self._build_index()
60
61 # 'alias' records (duh) alias options; {'foo': 'bar'} means
62 # --foo is an alias for --bar
63 self.alias = {}
64
65 # 'negative_alias' keeps track of options that are the boolean
66 # opposite of some other option
67 self.negative_alias = {}
68
69 # These keep track of the information in the option table. We
70 # don't actually populate these structures until we're ready to
71 # parse the command line, since the 'option_table' passed in here
72 # isn't necessarily the final word.
73 self.short_opts = []
74 self.long_opts = []
75 self.short2long = {}
76 self.attr_name = {}
77 self.takes_arg = {}
78
79 # And 'option_order' is filled up in 'getopt()'; it records the
80 # original order of options (and their values) on the command line,
81 # but expands short options, converts aliases, etc.
82 self.option_order = []
83
84 def _build_index(self):
85 self.option_index.clear()
86 for option in self.option_table:
87 self.option_index[option[0]] = option
88
89 def set_option_table(self, option_table):
90 self.option_table = option_table
91 self._build_index()
92
93 def add_option(self, long_option, short_option=None, help_string=None):
94 if long_option in self.option_index:
95 raise PackagingGetoptError(
96 "option conflict: already an option '%s'" % long_option)
97 else:
98 option = (long_option, short_option, help_string)
99 self.option_table.append(option)
100 self.option_index[long_option] = option
101
102 def has_option(self, long_option):
103 """Return true if the option table for this parser has an
104 option with long name 'long_option'."""
105 return long_option in self.option_index
106
107 def _check_alias_dict(self, aliases, what):
108 assert isinstance(aliases, dict)
109 for alias, opt in aliases.items():
110 if alias not in self.option_index:
111 raise PackagingGetoptError(
112 ("invalid %s '%s': "
113 "option '%s' not defined") % (what, alias, alias))
114 if opt not in self.option_index:
115 raise PackagingGetoptError(
116 ("invalid %s '%s': "
117 "aliased option '%s' not defined") % (what, alias, opt))
118
119 def set_aliases(self, alias):
120 """Set the aliases for this option parser."""
121 self._check_alias_dict(alias, "alias")
122 self.alias = alias
123
124 def set_negative_aliases(self, negative_alias):
125 """Set the negative aliases for this option parser.
126 'negative_alias' should be a dictionary mapping option names to
127 option names, both the key and value must already be defined
128 in the option table."""
129 self._check_alias_dict(negative_alias, "negative alias")
130 self.negative_alias = negative_alias
131
132 def _grok_option_table(self):
133 """Populate the various data structures that keep tabs on the
134 option table. Called by 'getopt()' before it can do anything
135 worthwhile.
136 """
137 self.long_opts = []
138 self.short_opts = []
139 self.short2long.clear()
140 self.repeat = {}
141
142 for option in self.option_table:
143 if len(option) == 3:
Éric Araujoa94bdee2011-05-31 15:05:38 +0200144 longopt, short, help = option
Tarek Ziade1231a4e2011-05-19 13:07:25 +0200145 repeat = 0
146 elif len(option) == 4:
Éric Araujoa94bdee2011-05-31 15:05:38 +0200147 longopt, short, help, repeat = option
Tarek Ziade1231a4e2011-05-19 13:07:25 +0200148 else:
149 # the option table is part of the code, so simply
150 # assert that it is correct
151 raise ValueError("invalid option tuple: %r" % option)
152
153 # Type- and value-check the option names
Éric Araujoa94bdee2011-05-31 15:05:38 +0200154 if not isinstance(longopt, str) or len(longopt) < 2:
Tarek Ziade1231a4e2011-05-19 13:07:25 +0200155 raise PackagingGetoptError(
156 ("invalid long option '%s': "
Éric Araujoa94bdee2011-05-31 15:05:38 +0200157 "must be a string of length >= 2") % longopt)
Tarek Ziade1231a4e2011-05-19 13:07:25 +0200158
159 if (not ((short is None) or
160 (isinstance(short, str) and len(short) == 1))):
161 raise PackagingGetoptError(
162 ("invalid short option '%s': "
163 "must be a single character or None") % short)
164
Éric Araujoa94bdee2011-05-31 15:05:38 +0200165 self.repeat[longopt] = repeat
166 self.long_opts.append(longopt)
Tarek Ziade1231a4e2011-05-19 13:07:25 +0200167
Éric Araujoa94bdee2011-05-31 15:05:38 +0200168 if longopt[-1] == '=': # option takes an argument?
Tarek Ziade1231a4e2011-05-19 13:07:25 +0200169 if short:
170 short = short + ':'
Éric Araujoa94bdee2011-05-31 15:05:38 +0200171 longopt = longopt[0:-1]
172 self.takes_arg[longopt] = 1
Tarek Ziade1231a4e2011-05-19 13:07:25 +0200173 else:
174
175 # Is option is a "negative alias" for some other option (eg.
176 # "quiet" == "!verbose")?
Éric Araujoa94bdee2011-05-31 15:05:38 +0200177 alias_to = self.negative_alias.get(longopt)
Tarek Ziade1231a4e2011-05-19 13:07:25 +0200178 if alias_to is not None:
179 if self.takes_arg[alias_to]:
180 raise PackagingGetoptError(
181 ("invalid negative alias '%s': "
182 "aliased option '%s' takes a value") % \
Éric Araujoa94bdee2011-05-31 15:05:38 +0200183 (longopt, alias_to))
Tarek Ziade1231a4e2011-05-19 13:07:25 +0200184
Éric Araujoa94bdee2011-05-31 15:05:38 +0200185 self.long_opts[-1] = longopt # XXX redundant?!
186 self.takes_arg[longopt] = 0
Tarek Ziade1231a4e2011-05-19 13:07:25 +0200187
188 else:
Éric Araujoa94bdee2011-05-31 15:05:38 +0200189 self.takes_arg[longopt] = 0
Tarek Ziade1231a4e2011-05-19 13:07:25 +0200190
191 # If this is an alias option, make sure its "takes arg" flag is
192 # the same as the option it's aliased to.
Éric Araujoa94bdee2011-05-31 15:05:38 +0200193 alias_to = self.alias.get(longopt)
Tarek Ziade1231a4e2011-05-19 13:07:25 +0200194 if alias_to is not None:
Éric Araujoa94bdee2011-05-31 15:05:38 +0200195 if self.takes_arg[longopt] != self.takes_arg[alias_to]:
Tarek Ziade1231a4e2011-05-19 13:07:25 +0200196 raise PackagingGetoptError(
197 ("invalid alias '%s': inconsistent with "
198 "aliased option '%s' (one of them takes a value, "
Éric Araujoa94bdee2011-05-31 15:05:38 +0200199 "the other doesn't") % (longopt, alias_to))
Tarek Ziade1231a4e2011-05-19 13:07:25 +0200200
201 # Now enforce some bondage on the long option name, so we can
202 # later translate it to an attribute name on some object. Have
203 # to do this a bit late to make sure we've removed any trailing
204 # '='.
Éric Araujoa94bdee2011-05-31 15:05:38 +0200205 if not longopt_re.match(longopt):
Tarek Ziade1231a4e2011-05-19 13:07:25 +0200206 raise PackagingGetoptError(
207 ("invalid long option name '%s' " +
Éric Araujoa94bdee2011-05-31 15:05:38 +0200208 "(must be letters, numbers, hyphens only") % longopt)
Tarek Ziade1231a4e2011-05-19 13:07:25 +0200209
Éric Araujoa94bdee2011-05-31 15:05:38 +0200210 self.attr_name[longopt] = longopt.replace('-', '_')
Tarek Ziade1231a4e2011-05-19 13:07:25 +0200211 if short:
212 self.short_opts.append(short)
Éric Araujoa94bdee2011-05-31 15:05:38 +0200213 self.short2long[short[0]] = longopt
Tarek Ziade1231a4e2011-05-19 13:07:25 +0200214
215 def getopt(self, args=None, object=None):
216 """Parse command-line options in args. Store as attributes on object.
217
218 If 'args' is None or not supplied, uses 'sys.argv[1:]'. If
219 'object' is None or not supplied, creates a new OptionDummy
220 object, stores option values there, and returns a tuple (args,
221 object). If 'object' is supplied, it is modified in place and
222 'getopt()' just returns 'args'; in both cases, the returned
223 'args' is a modified copy of the passed-in 'args' list, which
224 is left untouched.
225 """
226 if args is None:
227 args = sys.argv[1:]
228 if object is None:
229 object = OptionDummy()
230 created_object = 1
231 else:
232 created_object = 0
233
234 self._grok_option_table()
235
236 short_opts = ' '.join(self.short_opts)
237
238 try:
239 opts, args = getopt.getopt(args, short_opts, self.long_opts)
240 except getopt.error as msg:
241 raise PackagingArgError(msg)
242
243 for opt, val in opts:
244 if len(opt) == 2 and opt[0] == '-': # it's a short option
245 opt = self.short2long[opt[1]]
246 else:
247 assert len(opt) > 2 and opt[:2] == '--'
248 opt = opt[2:]
249
250 alias = self.alias.get(opt)
251 if alias:
252 opt = alias
253
254 if not self.takes_arg[opt]: # boolean option?
255 assert val == '', "boolean option can't have value"
256 alias = self.negative_alias.get(opt)
257 if alias:
258 opt = alias
259 val = 0
260 else:
261 val = 1
262
263 attr = self.attr_name[opt]
264 # The only repeating option at the moment is 'verbose'.
265 # It has a negative option -q quiet, which should set verbose = 0.
266 if val and self.repeat.get(attr) is not None:
267 val = getattr(object, attr, 0) + 1
268 setattr(object, attr, val)
269 self.option_order.append((opt, val))
270
271 # for opts
272 if created_object:
273 return args, object
274 else:
275 return args
276
277 def get_option_order(self):
278 """Returns the list of (option, value) tuples processed by the
279 previous run of 'getopt()'. Raises RuntimeError if
280 'getopt()' hasn't been called yet.
281 """
282 if self.option_order is None:
283 raise RuntimeError("'getopt()' hasn't been called yet")
284 else:
285 return self.option_order
286
287 return self.option_order
288
289 def generate_help(self, header=None):
290 """Generate help text (a list of strings, one per suggested line of
291 output) from the option table for this FancyGetopt object.
292 """
293 # Blithely assume the option table is good: probably wouldn't call
294 # 'generate_help()' unless you've already called 'getopt()'.
295
296 # First pass: determine maximum length of long option names
297 max_opt = 0
298 for option in self.option_table:
Éric Araujoa94bdee2011-05-31 15:05:38 +0200299 longopt = option[0]
Tarek Ziade1231a4e2011-05-19 13:07:25 +0200300 short = option[1]
Éric Araujoa94bdee2011-05-31 15:05:38 +0200301 l = len(longopt)
302 if longopt[-1] == '=':
Tarek Ziade1231a4e2011-05-19 13:07:25 +0200303 l = l - 1
304 if short is not None:
305 l = l + 5 # " (-x)" where short == 'x'
306 if l > max_opt:
307 max_opt = l
308
309 opt_width = max_opt + 2 + 2 + 2 # room for indent + dashes + gutter
310
311 # Typical help block looks like this:
312 # --foo controls foonabulation
313 # Help block for longest option looks like this:
314 # --flimflam set the flim-flam level
315 # and with wrapped text:
316 # --flimflam set the flim-flam level (must be between
317 # 0 and 100, except on Tuesdays)
318 # Options with short names will have the short name shown (but
319 # it doesn't contribute to max_opt):
320 # --foo (-f) controls foonabulation
321 # If adding the short option would make the left column too wide,
322 # we push the explanation off to the next line
323 # --flimflam (-l)
324 # set the flim-flam level
325 # Important parameters:
326 # - 2 spaces before option block start lines
327 # - 2 dashes for each long option name
328 # - min. 2 spaces between option and explanation (gutter)
329 # - 5 characters (incl. space) for short option name
330
331 # Now generate lines of help text. (If 80 columns were good enough
332 # for Jesus, then 78 columns are good enough for me!)
333 line_width = 78
334 text_width = line_width - opt_width
335 big_indent = ' ' * opt_width
336 if header:
337 lines = [header]
338 else:
339 lines = ['Option summary:']
340
341 for option in self.option_table:
Éric Araujoa94bdee2011-05-31 15:05:38 +0200342 longopt, short, help = option[:3]
Tarek Ziade1231a4e2011-05-19 13:07:25 +0200343 text = textwrap.wrap(help, text_width)
344
345 # Case 1: no short option at all (makes life easy)
346 if short is None:
347 if text:
Éric Araujoa94bdee2011-05-31 15:05:38 +0200348 lines.append(" --%-*s %s" % (max_opt, longopt, text[0]))
Tarek Ziade1231a4e2011-05-19 13:07:25 +0200349 else:
Éric Araujoa94bdee2011-05-31 15:05:38 +0200350 lines.append(" --%-*s " % (max_opt, longopt))
Tarek Ziade1231a4e2011-05-19 13:07:25 +0200351
352 # Case 2: we have a short option, so we have to include it
353 # just after the long option
354 else:
Éric Araujoa94bdee2011-05-31 15:05:38 +0200355 opt_names = "%s (-%s)" % (longopt, short)
Tarek Ziade1231a4e2011-05-19 13:07:25 +0200356 if text:
357 lines.append(" --%-*s %s" %
358 (max_opt, opt_names, text[0]))
359 else:
360 lines.append(" --%-*s" % opt_names)
361
362 for l in text[1:]:
363 lines.append(big_indent + l)
364
365 return lines
366
367 def print_help(self, header=None, file=None):
368 if file is None:
369 file = sys.stdout
370 for line in self.generate_help(header):
371 file.write(line + "\n")
372
373
374def fancy_getopt(options, negative_opt, object, args):
375 parser = FancyGetopt(options)
376 parser.set_negative_aliases(negative_opt)
377 return parser.getopt(args, object)
378
379
Tarek Ziade1231a4e2011-05-19 13:07:25 +0200380class OptionDummy:
381 """Dummy class just used as a place to hold command-line option
382 values as instance attributes."""
383
384 def __init__(self, options=[]):
385 """Create a new OptionDummy instance. The attributes listed in
386 'options' will be initialized to None."""
387 for opt in options:
388 setattr(self, opt, None)