blob: 5ad7e13a48072a6cebd280b803b52b6c31ac94c9 [file] [log] [blame]
Benjamin Peterson2b37fc42010-03-24 22:10:42 +00001# Author: Steven J. Bethard <steven.bethard@gmail.com>.
Benjamin Peterson698a18a2010-03-02 22:34:37 +00002
3"""Command-line parsing library
4
5This module is an optparse-inspired command-line parsing library that:
6
7 - handles both optional and positional arguments
8 - produces highly informative usage messages
9 - supports parsers that dispatch to sub-parsers
10
11The following is a simple usage example that sums integers from the
12command-line and writes the result to a file::
13
14 parser = argparse.ArgumentParser(
15 description='sum the integers at the command line')
16 parser.add_argument(
17 'integers', metavar='int', nargs='+', type=int,
18 help='an integer to be summed')
19 parser.add_argument(
20 '--log', default=sys.stdout, type=argparse.FileType('w'),
21 help='the file where the sum should be written')
22 args = parser.parse_args()
23 args.log.write('%s' % sum(args.integers))
24 args.log.close()
25
26The module contains the following public classes:
27
28 - ArgumentParser -- The main entry point for command-line parsing. As the
29 example above shows, the add_argument() method is used to populate
30 the parser with actions for optional and positional arguments. Then
31 the parse_args() method is invoked to convert the args at the
32 command-line into an object with attributes.
33
34 - ArgumentError -- The exception raised by ArgumentParser objects when
35 there are errors with the parser's actions. Errors raised while
36 parsing the command-line are caught by ArgumentParser and emitted
37 as command-line messages.
38
39 - FileType -- A factory for defining types of files to be created. As the
40 example above shows, instances of FileType are typically passed as
41 the type= argument of add_argument() calls.
42
43 - Action -- The base class for parser actions. Typically actions are
44 selected by passing strings like 'store_true' or 'append_const' to
45 the action= argument of add_argument(). However, for greater
46 customization of ArgumentParser actions, subclasses of Action may
47 be defined and passed as the action= argument.
48
49 - HelpFormatter, RawDescriptionHelpFormatter, RawTextHelpFormatter,
50 ArgumentDefaultsHelpFormatter -- Formatter classes which
51 may be passed as the formatter_class= argument to the
52 ArgumentParser constructor. HelpFormatter is the default,
53 RawDescriptionHelpFormatter and RawTextHelpFormatter tell the parser
54 not to change the formatting for help text, and
55 ArgumentDefaultsHelpFormatter adds information about argument defaults
56 to the help.
57
58All other classes in this module are considered implementation details.
59(Also note that HelpFormatter and RawDescriptionHelpFormatter are only
60considered public as object names -- the API of the formatter objects is
61still considered an implementation detail.)
62"""
63
64__version__ = '1.1'
65__all__ = [
66 'ArgumentParser',
67 'ArgumentError',
Steven Bethard72c55382010-11-01 15:23:12 +000068 'ArgumentTypeError',
Benjamin Peterson698a18a2010-03-02 22:34:37 +000069 'FileType',
70 'HelpFormatter',
Steven Bethard72c55382010-11-01 15:23:12 +000071 'ArgumentDefaultsHelpFormatter',
Benjamin Peterson698a18a2010-03-02 22:34:37 +000072 'RawDescriptionHelpFormatter',
73 'RawTextHelpFormatter',
Steven Bethard0331e902011-03-26 14:48:04 +010074 'MetavarTypeHelpFormatter',
Steven Bethard72c55382010-11-01 15:23:12 +000075 'Namespace',
76 'Action',
77 'ONE_OR_MORE',
78 'OPTIONAL',
79 'PARSER',
80 'REMAINDER',
81 'SUPPRESS',
82 'ZERO_OR_MORE',
Benjamin Peterson698a18a2010-03-02 22:34:37 +000083]
84
85
Steven Bethard8a6a1982011-03-27 13:53:53 +020086import collections as _collections
Benjamin Peterson698a18a2010-03-02 22:34:37 +000087import copy as _copy
88import os as _os
89import re as _re
90import sys as _sys
91import textwrap as _textwrap
92
Éric Araujo12159152010-12-04 17:31:49 +000093from gettext import gettext as _, ngettext
Benjamin Peterson698a18a2010-03-02 22:34:37 +000094
Benjamin Peterson698a18a2010-03-02 22:34:37 +000095
Benjamin Peterson698a18a2010-03-02 22:34:37 +000096SUPPRESS = '==SUPPRESS=='
97
98OPTIONAL = '?'
99ZERO_OR_MORE = '*'
100ONE_OR_MORE = '+'
101PARSER = 'A...'
102REMAINDER = '...'
Steven Bethardfca2e8a2010-11-02 12:47:22 +0000103_UNRECOGNIZED_ARGS_ATTR = '_unrecognized_args'
Benjamin Peterson698a18a2010-03-02 22:34:37 +0000104
105# =============================
106# Utility functions and classes
107# =============================
108
109class _AttributeHolder(object):
110 """Abstract base class that provides __repr__.
111
112 The __repr__ method returns a string in the format::
113 ClassName(attr=name, attr=name, ...)
114 The attributes are determined either by a class-level attribute,
115 '_kwarg_names', or by inspecting the instance __dict__.
116 """
117
118 def __repr__(self):
119 type_name = type(self).__name__
120 arg_strings = []
121 for arg in self._get_args():
122 arg_strings.append(repr(arg))
123 for name, value in self._get_kwargs():
124 arg_strings.append('%s=%r' % (name, value))
125 return '%s(%s)' % (type_name, ', '.join(arg_strings))
126
127 def _get_kwargs(self):
Benjamin Peterson16f2fd02010-03-02 23:09:38 +0000128 return sorted(self.__dict__.items())
Benjamin Peterson698a18a2010-03-02 22:34:37 +0000129
130 def _get_args(self):
131 return []
132
133
134def _ensure_value(namespace, name, value):
135 if getattr(namespace, name, None) is None:
136 setattr(namespace, name, value)
137 return getattr(namespace, name)
138
139
140# ===============
141# Formatting Help
142# ===============
143
144class HelpFormatter(object):
145 """Formatter for generating usage messages and argument help strings.
146
147 Only the name of this class is considered a public API. All the methods
148 provided by the class are considered an implementation detail.
149 """
150
151 def __init__(self,
152 prog,
153 indent_increment=2,
154 max_help_position=24,
155 width=None):
156
157 # default setting for width
158 if width is None:
159 try:
160 width = int(_os.environ['COLUMNS'])
161 except (KeyError, ValueError):
162 width = 80
163 width -= 2
164
165 self._prog = prog
166 self._indent_increment = indent_increment
167 self._max_help_position = max_help_position
Serhiy Storchakaf4511122014-01-09 23:14:27 +0200168 self._max_help_position = min(max_help_position,
169 max(width - 20, indent_increment * 2))
Benjamin Peterson698a18a2010-03-02 22:34:37 +0000170 self._width = width
171
172 self._current_indent = 0
173 self._level = 0
174 self._action_max_length = 0
175
176 self._root_section = self._Section(self, None)
177 self._current_section = self._root_section
178
179 self._whitespace_matcher = _re.compile(r'\s+')
180 self._long_break_matcher = _re.compile(r'\n\n\n+')
181
182 # ===============================
183 # Section and indentation methods
184 # ===============================
185 def _indent(self):
186 self._current_indent += self._indent_increment
187 self._level += 1
188
189 def _dedent(self):
190 self._current_indent -= self._indent_increment
191 assert self._current_indent >= 0, 'Indent decreased below 0.'
192 self._level -= 1
193
194 class _Section(object):
195
196 def __init__(self, formatter, parent, heading=None):
197 self.formatter = formatter
198 self.parent = parent
199 self.heading = heading
200 self.items = []
201
202 def format_help(self):
203 # format the indented section
204 if self.parent is not None:
205 self.formatter._indent()
206 join = self.formatter._join_parts
207 for func, args in self.items:
208 func(*args)
209 item_help = join([func(*args) for func, args in self.items])
210 if self.parent is not None:
211 self.formatter._dedent()
212
213 # return nothing if the section was empty
214 if not item_help:
215 return ''
216
217 # add the heading if the section was non-empty
218 if self.heading is not SUPPRESS and self.heading is not None:
219 current_indent = self.formatter._current_indent
220 heading = '%*s%s:\n' % (current_indent, '', self.heading)
221 else:
222 heading = ''
223
224 # join the section-initial newline, the heading and the help
225 return join(['\n', heading, item_help, '\n'])
226
227 def _add_item(self, func, args):
228 self._current_section.items.append((func, args))
229
230 # ========================
231 # Message building methods
232 # ========================
233 def start_section(self, heading):
234 self._indent()
235 section = self._Section(self, self._current_section, heading)
236 self._add_item(section.format_help, [])
237 self._current_section = section
238
239 def end_section(self):
240 self._current_section = self._current_section.parent
241 self._dedent()
242
243 def add_text(self, text):
244 if text is not SUPPRESS and text is not None:
245 self._add_item(self._format_text, [text])
246
247 def add_usage(self, usage, actions, groups, prefix=None):
248 if usage is not SUPPRESS:
249 args = usage, actions, groups, prefix
250 self._add_item(self._format_usage, args)
251
252 def add_argument(self, action):
253 if action.help is not SUPPRESS:
254
255 # find all invocations
256 get_invocation = self._format_action_invocation
257 invocations = [get_invocation(action)]
258 for subaction in self._iter_indented_subactions(action):
259 invocations.append(get_invocation(subaction))
260
261 # update the maximum item length
262 invocation_length = max([len(s) for s in invocations])
263 action_length = invocation_length + self._current_indent
264 self._action_max_length = max(self._action_max_length,
265 action_length)
266
267 # add the item to the list
268 self._add_item(self._format_action, [action])
269
270 def add_arguments(self, actions):
271 for action in actions:
272 self.add_argument(action)
273
274 # =======================
275 # Help-formatting methods
276 # =======================
277 def format_help(self):
278 help = self._root_section.format_help()
279 if help:
280 help = self._long_break_matcher.sub('\n\n', help)
281 help = help.strip('\n') + '\n'
282 return help
283
284 def _join_parts(self, part_strings):
285 return ''.join([part
286 for part in part_strings
287 if part and part is not SUPPRESS])
288
289 def _format_usage(self, usage, actions, groups, prefix):
290 if prefix is None:
291 prefix = _('usage: ')
292
293 # if usage is specified, use that
294 if usage is not None:
295 usage = usage % dict(prog=self._prog)
296
297 # if no optionals or positionals are available, usage is just prog
298 elif usage is None and not actions:
299 usage = '%(prog)s' % dict(prog=self._prog)
300
301 # if optionals and positionals are available, calculate usage
302 elif usage is None:
303 prog = '%(prog)s' % dict(prog=self._prog)
304
305 # split optionals from positionals
306 optionals = []
307 positionals = []
308 for action in actions:
309 if action.option_strings:
310 optionals.append(action)
311 else:
312 positionals.append(action)
313
314 # build full usage string
315 format = self._format_actions_usage
316 action_usage = format(optionals + positionals, groups)
317 usage = ' '.join([s for s in [prog, action_usage] if s])
318
319 # wrap the usage parts if it's too long
320 text_width = self._width - self._current_indent
321 if len(prefix) + len(usage) > text_width:
322
323 # break usage into wrappable parts
324 part_regexp = r'\(.*?\)+|\[.*?\]+|\S+'
325 opt_usage = format(optionals, groups)
326 pos_usage = format(positionals, groups)
327 opt_parts = _re.findall(part_regexp, opt_usage)
328 pos_parts = _re.findall(part_regexp, pos_usage)
329 assert ' '.join(opt_parts) == opt_usage
330 assert ' '.join(pos_parts) == pos_usage
331
332 # helper for wrapping lines
333 def get_lines(parts, indent, prefix=None):
334 lines = []
335 line = []
336 if prefix is not None:
337 line_len = len(prefix) - 1
338 else:
339 line_len = len(indent) - 1
340 for part in parts:
Serhiy Storchakaf4511122014-01-09 23:14:27 +0200341 if line_len + 1 + len(part) > text_width and line:
Benjamin Peterson698a18a2010-03-02 22:34:37 +0000342 lines.append(indent + ' '.join(line))
343 line = []
344 line_len = len(indent) - 1
345 line.append(part)
346 line_len += len(part) + 1
347 if line:
348 lines.append(indent + ' '.join(line))
349 if prefix is not None:
350 lines[0] = lines[0][len(indent):]
351 return lines
352
353 # if prog is short, follow it with optionals or positionals
354 if len(prefix) + len(prog) <= 0.75 * text_width:
355 indent = ' ' * (len(prefix) + len(prog) + 1)
356 if opt_parts:
357 lines = get_lines([prog] + opt_parts, indent, prefix)
358 lines.extend(get_lines(pos_parts, indent))
359 elif pos_parts:
360 lines = get_lines([prog] + pos_parts, indent, prefix)
361 else:
362 lines = [prog]
363
364 # if prog is long, put it on its own line
365 else:
366 indent = ' ' * len(prefix)
367 parts = opt_parts + pos_parts
368 lines = get_lines(parts, indent)
369 if len(lines) > 1:
370 lines = []
371 lines.extend(get_lines(opt_parts, indent))
372 lines.extend(get_lines(pos_parts, indent))
373 lines = [prog] + lines
374
375 # join lines into usage
376 usage = '\n'.join(lines)
377
378 # prefix with 'usage:'
379 return '%s%s\n\n' % (prefix, usage)
380
381 def _format_actions_usage(self, actions, groups):
382 # find group indices and identify actions in groups
Benjamin Peterson16f2fd02010-03-02 23:09:38 +0000383 group_actions = set()
Benjamin Peterson698a18a2010-03-02 22:34:37 +0000384 inserts = {}
385 for group in groups:
386 try:
387 start = actions.index(group._group_actions[0])
388 except ValueError:
389 continue
390 else:
391 end = start + len(group._group_actions)
392 if actions[start:end] == group._group_actions:
393 for action in group._group_actions:
394 group_actions.add(action)
395 if not group.required:
Steven Bethard49998ee2010-11-01 16:29:26 +0000396 if start in inserts:
397 inserts[start] += ' ['
398 else:
399 inserts[start] = '['
Benjamin Peterson698a18a2010-03-02 22:34:37 +0000400 inserts[end] = ']'
401 else:
Steven Bethard49998ee2010-11-01 16:29:26 +0000402 if start in inserts:
403 inserts[start] += ' ('
404 else:
405 inserts[start] = '('
Benjamin Peterson698a18a2010-03-02 22:34:37 +0000406 inserts[end] = ')'
407 for i in range(start + 1, end):
408 inserts[i] = '|'
409
410 # collect all actions format strings
411 parts = []
412 for i, action in enumerate(actions):
413
414 # suppressed arguments are marked with None
415 # remove | separators for suppressed arguments
416 if action.help is SUPPRESS:
417 parts.append(None)
418 if inserts.get(i) == '|':
419 inserts.pop(i)
420 elif inserts.get(i + 1) == '|':
421 inserts.pop(i + 1)
422
423 # produce all arg strings
424 elif not action.option_strings:
Steven Bethard0331e902011-03-26 14:48:04 +0100425 default = self._get_default_metavar_for_positional(action)
426 part = self._format_args(action, default)
Benjamin Peterson698a18a2010-03-02 22:34:37 +0000427
428 # if it's in a group, strip the outer []
429 if action in group_actions:
430 if part[0] == '[' and part[-1] == ']':
431 part = part[1:-1]
432
433 # add the action string to the list
434 parts.append(part)
435
436 # produce the first way to invoke the option in brackets
437 else:
438 option_string = action.option_strings[0]
439
440 # if the Optional doesn't take a value, format is:
441 # -s or --long
442 if action.nargs == 0:
443 part = '%s' % option_string
444
445 # if the Optional takes a value, format is:
446 # -s ARGS or --long ARGS
447 else:
Steven Bethard0331e902011-03-26 14:48:04 +0100448 default = self._get_default_metavar_for_optional(action)
Benjamin Peterson698a18a2010-03-02 22:34:37 +0000449 args_string = self._format_args(action, default)
450 part = '%s %s' % (option_string, args_string)
451
452 # make it look optional if it's not required or in a group
453 if not action.required and action not in group_actions:
454 part = '[%s]' % part
455
456 # add the action string to the list
457 parts.append(part)
458
459 # insert things at the necessary indices
Benjamin Peterson16f2fd02010-03-02 23:09:38 +0000460 for i in sorted(inserts, reverse=True):
Benjamin Peterson698a18a2010-03-02 22:34:37 +0000461 parts[i:i] = [inserts[i]]
462
463 # join all the action items with spaces
464 text = ' '.join([item for item in parts if item is not None])
465
466 # clean up separators for mutually exclusive groups
467 open = r'[\[(]'
468 close = r'[\])]'
469 text = _re.sub(r'(%s) ' % open, r'\1', text)
470 text = _re.sub(r' (%s)' % close, r'\1', text)
471 text = _re.sub(r'%s *%s' % (open, close), r'', text)
472 text = _re.sub(r'\(([^|]*)\)', r'\1', text)
473 text = text.strip()
474
475 # return the text
476 return text
477
478 def _format_text(self, text):
479 if '%(prog)' in text:
480 text = text % dict(prog=self._prog)
Serhiy Storchakaf4511122014-01-09 23:14:27 +0200481 text_width = max(self._width - self._current_indent, 11)
Benjamin Peterson698a18a2010-03-02 22:34:37 +0000482 indent = ' ' * self._current_indent
483 return self._fill_text(text, text_width, indent) + '\n\n'
484
485 def _format_action(self, action):
486 # determine the required width and the entry label
487 help_position = min(self._action_max_length + 2,
488 self._max_help_position)
Serhiy Storchakaf4511122014-01-09 23:14:27 +0200489 help_width = max(self._width - help_position, 11)
Benjamin Peterson698a18a2010-03-02 22:34:37 +0000490 action_width = help_position - self._current_indent - 2
491 action_header = self._format_action_invocation(action)
492
493 # ho nelp; start on same line and add a final newline
494 if not action.help:
495 tup = self._current_indent, '', action_header
496 action_header = '%*s%s\n' % tup
497
498 # short action name; start on the same line and pad two spaces
499 elif len(action_header) <= action_width:
500 tup = self._current_indent, '', action_width, action_header
501 action_header = '%*s%-*s ' % tup
502 indent_first = 0
503
504 # long action name; start on the next line
505 else:
506 tup = self._current_indent, '', action_header
507 action_header = '%*s%s\n' % tup
508 indent_first = help_position
509
510 # collect the pieces of the action help
511 parts = [action_header]
512
513 # if there was help for the action, add lines of help text
514 if action.help:
515 help_text = self._expand_help(action)
516 help_lines = self._split_lines(help_text, help_width)
517 parts.append('%*s%s\n' % (indent_first, '', help_lines[0]))
518 for line in help_lines[1:]:
519 parts.append('%*s%s\n' % (help_position, '', line))
520
521 # or add a newline if the description doesn't end with one
522 elif not action_header.endswith('\n'):
523 parts.append('\n')
524
525 # if there are any sub-actions, add their help as well
526 for subaction in self._iter_indented_subactions(action):
527 parts.append(self._format_action(subaction))
528
529 # return a single string
530 return self._join_parts(parts)
531
532 def _format_action_invocation(self, action):
533 if not action.option_strings:
Steven Bethard0331e902011-03-26 14:48:04 +0100534 default = self._get_default_metavar_for_positional(action)
535 metavar, = self._metavar_formatter(action, default)(1)
Benjamin Peterson698a18a2010-03-02 22:34:37 +0000536 return metavar
537
538 else:
539 parts = []
540
541 # if the Optional doesn't take a value, format is:
542 # -s, --long
543 if action.nargs == 0:
544 parts.extend(action.option_strings)
545
546 # if the Optional takes a value, format is:
547 # -s ARGS, --long ARGS
548 else:
Steven Bethard0331e902011-03-26 14:48:04 +0100549 default = self._get_default_metavar_for_optional(action)
Benjamin Peterson698a18a2010-03-02 22:34:37 +0000550 args_string = self._format_args(action, default)
551 for option_string in action.option_strings:
552 parts.append('%s %s' % (option_string, args_string))
553
554 return ', '.join(parts)
555
556 def _metavar_formatter(self, action, default_metavar):
557 if action.metavar is not None:
558 result = action.metavar
559 elif action.choices is not None:
560 choice_strs = [str(choice) for choice in action.choices]
561 result = '{%s}' % ','.join(choice_strs)
562 else:
563 result = default_metavar
564
565 def format(tuple_size):
566 if isinstance(result, tuple):
567 return result
568 else:
569 return (result, ) * tuple_size
570 return format
571
572 def _format_args(self, action, default_metavar):
573 get_metavar = self._metavar_formatter(action, default_metavar)
574 if action.nargs is None:
575 result = '%s' % get_metavar(1)
576 elif action.nargs == OPTIONAL:
577 result = '[%s]' % get_metavar(1)
578 elif action.nargs == ZERO_OR_MORE:
579 result = '[%s [%s ...]]' % get_metavar(2)
580 elif action.nargs == ONE_OR_MORE:
581 result = '%s [%s ...]' % get_metavar(2)
582 elif action.nargs == REMAINDER:
583 result = '...'
584 elif action.nargs == PARSER:
585 result = '%s ...' % get_metavar(1)
586 else:
587 formats = ['%s' for _ in range(action.nargs)]
588 result = ' '.join(formats) % get_metavar(action.nargs)
589 return result
590
591 def _expand_help(self, action):
592 params = dict(vars(action), prog=self._prog)
593 for name in list(params):
594 if params[name] is SUPPRESS:
595 del params[name]
596 for name in list(params):
597 if hasattr(params[name], '__name__'):
598 params[name] = params[name].__name__
599 if params.get('choices') is not None:
600 choices_str = ', '.join([str(c) for c in params['choices']])
601 params['choices'] = choices_str
602 return self._get_help_string(action) % params
603
604 def _iter_indented_subactions(self, action):
605 try:
606 get_subactions = action._get_subactions
607 except AttributeError:
608 pass
609 else:
610 self._indent()
Philip Jenvey4993cc02012-10-01 12:53:43 -0700611 yield from get_subactions()
Benjamin Peterson698a18a2010-03-02 22:34:37 +0000612 self._dedent()
613
614 def _split_lines(self, text, width):
615 text = self._whitespace_matcher.sub(' ', text).strip()
616 return _textwrap.wrap(text, width)
617
618 def _fill_text(self, text, width, indent):
619 text = self._whitespace_matcher.sub(' ', text).strip()
620 return _textwrap.fill(text, width, initial_indent=indent,
621 subsequent_indent=indent)
622
623 def _get_help_string(self, action):
624 return action.help
625
Steven Bethard0331e902011-03-26 14:48:04 +0100626 def _get_default_metavar_for_optional(self, action):
627 return action.dest.upper()
628
629 def _get_default_metavar_for_positional(self, action):
630 return action.dest
631
Benjamin Peterson698a18a2010-03-02 22:34:37 +0000632
633class RawDescriptionHelpFormatter(HelpFormatter):
634 """Help message formatter which retains any formatting in descriptions.
635
636 Only the name of this class is considered a public API. All the methods
637 provided by the class are considered an implementation detail.
638 """
639
640 def _fill_text(self, text, width, indent):
Ezio Melottid8b509b2011-09-28 17:37:55 +0300641 return ''.join(indent + line for line in text.splitlines(keepends=True))
Benjamin Peterson698a18a2010-03-02 22:34:37 +0000642
643
644class RawTextHelpFormatter(RawDescriptionHelpFormatter):
645 """Help message formatter which retains formatting of all help text.
646
647 Only the name of this class is considered a public API. All the methods
648 provided by the class are considered an implementation detail.
649 """
650
651 def _split_lines(self, text, width):
652 return text.splitlines()
653
654
655class ArgumentDefaultsHelpFormatter(HelpFormatter):
656 """Help message formatter which adds default values to argument help.
657
658 Only the name of this class is considered a public API. All the methods
659 provided by the class are considered an implementation detail.
660 """
661
662 def _get_help_string(self, action):
663 help = action.help
664 if '%(default)' not in action.help:
665 if action.default is not SUPPRESS:
666 defaulting_nargs = [OPTIONAL, ZERO_OR_MORE]
667 if action.option_strings or action.nargs in defaulting_nargs:
668 help += ' (default: %(default)s)'
669 return help
670
671
Steven Bethard0331e902011-03-26 14:48:04 +0100672class MetavarTypeHelpFormatter(HelpFormatter):
673 """Help message formatter which uses the argument 'type' as the default
674 metavar value (instead of the argument 'dest')
675
676 Only the name of this class is considered a public API. All the methods
677 provided by the class are considered an implementation detail.
678 """
679
680 def _get_default_metavar_for_optional(self, action):
681 return action.type.__name__
682
683 def _get_default_metavar_for_positional(self, action):
684 return action.type.__name__
685
686
687
Benjamin Peterson698a18a2010-03-02 22:34:37 +0000688# =====================
689# Options and Arguments
690# =====================
691
692def _get_action_name(argument):
693 if argument is None:
694 return None
695 elif argument.option_strings:
696 return '/'.join(argument.option_strings)
697 elif argument.metavar not in (None, SUPPRESS):
698 return argument.metavar
699 elif argument.dest not in (None, SUPPRESS):
700 return argument.dest
701 else:
702 return None
703
704
705class ArgumentError(Exception):
706 """An error from creating or using an argument (optional or positional).
707
708 The string value of this exception is the message, augmented with
709 information about the argument that caused it.
710 """
711
712 def __init__(self, argument, message):
713 self.argument_name = _get_action_name(argument)
714 self.message = message
715
716 def __str__(self):
717 if self.argument_name is None:
718 format = '%(message)s'
719 else:
720 format = 'argument %(argument_name)s: %(message)s'
721 return format % dict(message=self.message,
722 argument_name=self.argument_name)
723
724
725class ArgumentTypeError(Exception):
726 """An error from trying to convert a command line string to a type."""
727 pass
728
729
730# ==============
731# Action classes
732# ==============
733
734class Action(_AttributeHolder):
735 """Information about how to convert command line strings to Python objects.
736
737 Action objects are used by an ArgumentParser to represent the information
738 needed to parse a single argument from one or more strings from the
739 command line. The keyword arguments to the Action constructor are also
740 all attributes of Action instances.
741
742 Keyword Arguments:
743
744 - option_strings -- A list of command-line option strings which
745 should be associated with this action.
746
747 - dest -- The name of the attribute to hold the created object(s)
748
749 - nargs -- The number of command-line arguments that should be
750 consumed. By default, one argument will be consumed and a single
751 value will be produced. Other values include:
752 - N (an integer) consumes N arguments (and produces a list)
753 - '?' consumes zero or one arguments
754 - '*' consumes zero or more arguments (and produces a list)
755 - '+' consumes one or more arguments (and produces a list)
756 Note that the difference between the default and nargs=1 is that
757 with the default, a single value will be produced, while with
758 nargs=1, a list containing a single value will be produced.
759
760 - const -- The value to be produced if the option is specified and the
761 option uses an action that takes no values.
762
763 - default -- The value to be produced if the option is not specified.
764
R David Murray15cd9a02012-07-21 17:04:25 -0400765 - type -- A callable that accepts a single string argument, and
766 returns the converted value. The standard Python types str, int,
767 float, and complex are useful examples of such callables. If None,
768 str is used.
Benjamin Peterson698a18a2010-03-02 22:34:37 +0000769
770 - choices -- A container of values that should be allowed. If not None,
771 after a command-line argument has been converted to the appropriate
772 type, an exception will be raised if it is not a member of this
773 collection.
774
775 - required -- True if the action must always be specified at the
776 command line. This is only meaningful for optional command-line
777 arguments.
778
779 - help -- The help string describing the argument.
780
781 - metavar -- The name to be used for the option's argument with the
782 help string. If None, the 'dest' value will be used as the name.
783 """
784
785 def __init__(self,
786 option_strings,
787 dest,
788 nargs=None,
789 const=None,
790 default=None,
791 type=None,
792 choices=None,
793 required=False,
794 help=None,
795 metavar=None):
796 self.option_strings = option_strings
797 self.dest = dest
798 self.nargs = nargs
799 self.const = const
800 self.default = default
801 self.type = type
802 self.choices = choices
803 self.required = required
804 self.help = help
805 self.metavar = metavar
806
807 def _get_kwargs(self):
808 names = [
809 'option_strings',
810 'dest',
811 'nargs',
812 'const',
813 'default',
814 'type',
815 'choices',
816 'help',
817 'metavar',
818 ]
819 return [(name, getattr(self, name)) for name in names]
820
821 def __call__(self, parser, namespace, values, option_string=None):
822 raise NotImplementedError(_('.__call__() not defined'))
823
824
825class _StoreAction(Action):
826
827 def __init__(self,
828 option_strings,
829 dest,
830 nargs=None,
831 const=None,
832 default=None,
833 type=None,
834 choices=None,
835 required=False,
836 help=None,
837 metavar=None):
838 if nargs == 0:
839 raise ValueError('nargs for store actions must be > 0; if you '
840 'have nothing to store, actions such as store '
841 'true or store const may be more appropriate')
842 if const is not None and nargs != OPTIONAL:
843 raise ValueError('nargs must be %r to supply const' % OPTIONAL)
844 super(_StoreAction, self).__init__(
845 option_strings=option_strings,
846 dest=dest,
847 nargs=nargs,
848 const=const,
849 default=default,
850 type=type,
851 choices=choices,
852 required=required,
853 help=help,
854 metavar=metavar)
855
856 def __call__(self, parser, namespace, values, option_string=None):
857 setattr(namespace, self.dest, values)
858
859
860class _StoreConstAction(Action):
861
862 def __init__(self,
863 option_strings,
864 dest,
865 const,
866 default=None,
867 required=False,
868 help=None,
869 metavar=None):
870 super(_StoreConstAction, self).__init__(
871 option_strings=option_strings,
872 dest=dest,
873 nargs=0,
874 const=const,
875 default=default,
876 required=required,
877 help=help)
878
879 def __call__(self, parser, namespace, values, option_string=None):
880 setattr(namespace, self.dest, self.const)
881
882
883class _StoreTrueAction(_StoreConstAction):
884
885 def __init__(self,
886 option_strings,
887 dest,
888 default=False,
889 required=False,
890 help=None):
891 super(_StoreTrueAction, self).__init__(
892 option_strings=option_strings,
893 dest=dest,
894 const=True,
895 default=default,
896 required=required,
897 help=help)
898
899
900class _StoreFalseAction(_StoreConstAction):
901
902 def __init__(self,
903 option_strings,
904 dest,
905 default=True,
906 required=False,
907 help=None):
908 super(_StoreFalseAction, self).__init__(
909 option_strings=option_strings,
910 dest=dest,
911 const=False,
912 default=default,
913 required=required,
914 help=help)
915
916
917class _AppendAction(Action):
918
919 def __init__(self,
920 option_strings,
921 dest,
922 nargs=None,
923 const=None,
924 default=None,
925 type=None,
926 choices=None,
927 required=False,
928 help=None,
929 metavar=None):
930 if nargs == 0:
931 raise ValueError('nargs for append actions must be > 0; if arg '
932 'strings are not supplying the value to append, '
933 'the append const action may be more appropriate')
934 if const is not None and nargs != OPTIONAL:
935 raise ValueError('nargs must be %r to supply const' % OPTIONAL)
936 super(_AppendAction, self).__init__(
937 option_strings=option_strings,
938 dest=dest,
939 nargs=nargs,
940 const=const,
941 default=default,
942 type=type,
943 choices=choices,
944 required=required,
945 help=help,
946 metavar=metavar)
947
948 def __call__(self, parser, namespace, values, option_string=None):
949 items = _copy.copy(_ensure_value(namespace, self.dest, []))
950 items.append(values)
951 setattr(namespace, self.dest, items)
952
953
954class _AppendConstAction(Action):
955
956 def __init__(self,
957 option_strings,
958 dest,
959 const,
960 default=None,
961 required=False,
962 help=None,
963 metavar=None):
964 super(_AppendConstAction, self).__init__(
965 option_strings=option_strings,
966 dest=dest,
967 nargs=0,
968 const=const,
969 default=default,
970 required=required,
971 help=help,
972 metavar=metavar)
973
974 def __call__(self, parser, namespace, values, option_string=None):
975 items = _copy.copy(_ensure_value(namespace, self.dest, []))
976 items.append(self.const)
977 setattr(namespace, self.dest, items)
978
979
980class _CountAction(Action):
981
982 def __init__(self,
983 option_strings,
984 dest,
985 default=None,
986 required=False,
987 help=None):
988 super(_CountAction, self).__init__(
989 option_strings=option_strings,
990 dest=dest,
991 nargs=0,
992 default=default,
993 required=required,
994 help=help)
995
996 def __call__(self, parser, namespace, values, option_string=None):
997 new_count = _ensure_value(namespace, self.dest, 0) + 1
998 setattr(namespace, self.dest, new_count)
999
1000
1001class _HelpAction(Action):
1002
1003 def __init__(self,
1004 option_strings,
1005 dest=SUPPRESS,
1006 default=SUPPRESS,
1007 help=None):
1008 super(_HelpAction, self).__init__(
1009 option_strings=option_strings,
1010 dest=dest,
1011 default=default,
1012 nargs=0,
1013 help=help)
1014
1015 def __call__(self, parser, namespace, values, option_string=None):
1016 parser.print_help()
1017 parser.exit()
1018
1019
1020class _VersionAction(Action):
1021
1022 def __init__(self,
1023 option_strings,
1024 version=None,
1025 dest=SUPPRESS,
1026 default=SUPPRESS,
Steven Bethard50fe5932010-05-24 03:47:38 +00001027 help="show program's version number and exit"):
Benjamin Peterson698a18a2010-03-02 22:34:37 +00001028 super(_VersionAction, self).__init__(
1029 option_strings=option_strings,
1030 dest=dest,
1031 default=default,
1032 nargs=0,
1033 help=help)
1034 self.version = version
1035
1036 def __call__(self, parser, namespace, values, option_string=None):
1037 version = self.version
1038 if version is None:
1039 version = parser.version
1040 formatter = parser._get_formatter()
1041 formatter.add_text(version)
Eli Benderskycdac5512013-09-06 06:49:15 -07001042 parser._print_message(formatter.format_help(), _sys.stdout)
1043 parser.exit()
Benjamin Peterson698a18a2010-03-02 22:34:37 +00001044
1045
1046class _SubParsersAction(Action):
1047
1048 class _ChoicesPseudoAction(Action):
1049
Steven Bethardfd311a72010-12-18 11:19:23 +00001050 def __init__(self, name, aliases, help):
1051 metavar = dest = name
1052 if aliases:
1053 metavar += ' (%s)' % ', '.join(aliases)
Benjamin Peterson698a18a2010-03-02 22:34:37 +00001054 sup = super(_SubParsersAction._ChoicesPseudoAction, self)
Steven Bethardfd311a72010-12-18 11:19:23 +00001055 sup.__init__(option_strings=[], dest=dest, help=help,
1056 metavar=metavar)
Benjamin Peterson698a18a2010-03-02 22:34:37 +00001057
1058 def __init__(self,
1059 option_strings,
1060 prog,
1061 parser_class,
1062 dest=SUPPRESS,
1063 help=None,
1064 metavar=None):
1065
1066 self._prog_prefix = prog
1067 self._parser_class = parser_class
Steven Bethard8a6a1982011-03-27 13:53:53 +02001068 self._name_parser_map = _collections.OrderedDict()
Benjamin Peterson698a18a2010-03-02 22:34:37 +00001069 self._choices_actions = []
1070
1071 super(_SubParsersAction, self).__init__(
1072 option_strings=option_strings,
1073 dest=dest,
1074 nargs=PARSER,
1075 choices=self._name_parser_map,
1076 help=help,
1077 metavar=metavar)
1078
1079 def add_parser(self, name, **kwargs):
1080 # set prog from the existing prefix
1081 if kwargs.get('prog') is None:
1082 kwargs['prog'] = '%s %s' % (self._prog_prefix, name)
1083
Steven Bethardfd311a72010-12-18 11:19:23 +00001084 aliases = kwargs.pop('aliases', ())
1085
Benjamin Peterson698a18a2010-03-02 22:34:37 +00001086 # create a pseudo-action to hold the choice help
1087 if 'help' in kwargs:
1088 help = kwargs.pop('help')
Steven Bethardfd311a72010-12-18 11:19:23 +00001089 choice_action = self._ChoicesPseudoAction(name, aliases, help)
Benjamin Peterson698a18a2010-03-02 22:34:37 +00001090 self._choices_actions.append(choice_action)
1091
1092 # create the parser and add it to the map
1093 parser = self._parser_class(**kwargs)
1094 self._name_parser_map[name] = parser
Steven Bethardfd311a72010-12-18 11:19:23 +00001095
1096 # make parser available under aliases also
1097 for alias in aliases:
1098 self._name_parser_map[alias] = parser
1099
Benjamin Peterson698a18a2010-03-02 22:34:37 +00001100 return parser
1101
1102 def _get_subactions(self):
1103 return self._choices_actions
1104
1105 def __call__(self, parser, namespace, values, option_string=None):
1106 parser_name = values[0]
1107 arg_strings = values[1:]
1108
1109 # set the parser name if requested
1110 if self.dest is not SUPPRESS:
1111 setattr(namespace, self.dest, parser_name)
1112
1113 # select the parser
1114 try:
1115 parser = self._name_parser_map[parser_name]
1116 except KeyError:
Éric Araujobb48a8b2010-12-03 19:41:00 +00001117 args = {'parser_name': parser_name,
1118 'choices': ', '.join(self._name_parser_map)}
1119 msg = _('unknown parser %(parser_name)r (choices: %(choices)s)') % args
Benjamin Peterson698a18a2010-03-02 22:34:37 +00001120 raise ArgumentError(self, msg)
1121
1122 # parse all the remaining options into the namespace
Steven Bethardfca2e8a2010-11-02 12:47:22 +00001123 # store any unrecognized options on the object, so that the top
1124 # level parser can decide what to do with them
1125 namespace, arg_strings = parser.parse_known_args(arg_strings, namespace)
1126 if arg_strings:
1127 vars(namespace).setdefault(_UNRECOGNIZED_ARGS_ATTR, [])
1128 getattr(namespace, _UNRECOGNIZED_ARGS_ATTR).extend(arg_strings)
Benjamin Peterson698a18a2010-03-02 22:34:37 +00001129
1130
1131# ==============
1132# Type classes
1133# ==============
1134
1135class FileType(object):
1136 """Factory for creating file object types
1137
1138 Instances of FileType are typically passed as type= arguments to the
1139 ArgumentParser add_argument() method.
1140
1141 Keyword Arguments:
1142 - mode -- A string indicating how the file is to be opened. Accepts the
1143 same values as the builtin open() function.
1144 - bufsize -- The file's desired buffer size. Accepts the same values as
1145 the builtin open() function.
Petri Lehtinen74d6c252012-12-15 22:39:32 +02001146 - encoding -- The file's encoding. Accepts the same values as the
Terry Jan Reedy8e7586b2013-03-11 18:38:13 -04001147 builtin open() function.
Petri Lehtinen74d6c252012-12-15 22:39:32 +02001148 - errors -- A string indicating how encoding and decoding errors are to
1149 be handled. Accepts the same value as the builtin open() function.
Benjamin Peterson698a18a2010-03-02 22:34:37 +00001150 """
1151
Petri Lehtinen74d6c252012-12-15 22:39:32 +02001152 def __init__(self, mode='r', bufsize=-1, encoding=None, errors=None):
Benjamin Peterson698a18a2010-03-02 22:34:37 +00001153 self._mode = mode
1154 self._bufsize = bufsize
Petri Lehtinen74d6c252012-12-15 22:39:32 +02001155 self._encoding = encoding
1156 self._errors = errors
Benjamin Peterson698a18a2010-03-02 22:34:37 +00001157
1158 def __call__(self, string):
1159 # the special argument "-" means sys.std{in,out}
1160 if string == '-':
1161 if 'r' in self._mode:
1162 return _sys.stdin
1163 elif 'w' in self._mode:
1164 return _sys.stdout
1165 else:
Éric Araujoa9c7a8f2010-12-03 19:19:17 +00001166 msg = _('argument "-" with mode %r') % self._mode
Benjamin Peterson698a18a2010-03-02 22:34:37 +00001167 raise ValueError(msg)
1168
1169 # all other arguments are used as file names
Steven Bethardb0270112011-01-24 21:02:50 +00001170 try:
Petri Lehtinen74d6c252012-12-15 22:39:32 +02001171 return open(string, self._mode, self._bufsize, self._encoding,
1172 self._errors)
Andrew Svetlovf7a17b42012-12-25 16:47:37 +02001173 except OSError as e:
Steven Bethardb0270112011-01-24 21:02:50 +00001174 message = _("can't open '%s': %s")
1175 raise ArgumentTypeError(message % (string, e))
Benjamin Peterson698a18a2010-03-02 22:34:37 +00001176
1177 def __repr__(self):
Steven Bethardb0270112011-01-24 21:02:50 +00001178 args = self._mode, self._bufsize
Petri Lehtinen74d6c252012-12-15 22:39:32 +02001179 kwargs = [('encoding', self._encoding), ('errors', self._errors)]
1180 args_str = ', '.join([repr(arg) for arg in args if arg != -1] +
1181 ['%s=%r' % (kw, arg) for kw, arg in kwargs
1182 if arg is not None])
Benjamin Peterson698a18a2010-03-02 22:34:37 +00001183 return '%s(%s)' % (type(self).__name__, args_str)
1184
1185# ===========================
1186# Optional and Positional Parsing
1187# ===========================
1188
1189class Namespace(_AttributeHolder):
1190 """Simple object for storing attributes.
1191
1192 Implements equality by attribute names and values, and provides a simple
1193 string representation.
1194 """
1195
1196 def __init__(self, **kwargs):
1197 for name in kwargs:
1198 setattr(self, name, kwargs[name])
1199
1200 def __eq__(self, other):
1201 return vars(self) == vars(other)
1202
1203 def __ne__(self, other):
1204 return not (self == other)
1205
1206 def __contains__(self, key):
1207 return key in self.__dict__
1208
1209
1210class _ActionsContainer(object):
1211
1212 def __init__(self,
1213 description,
1214 prefix_chars,
1215 argument_default,
1216 conflict_handler):
1217 super(_ActionsContainer, self).__init__()
1218
1219 self.description = description
1220 self.argument_default = argument_default
1221 self.prefix_chars = prefix_chars
1222 self.conflict_handler = conflict_handler
1223
1224 # set up registries
1225 self._registries = {}
1226
1227 # register actions
1228 self.register('action', None, _StoreAction)
1229 self.register('action', 'store', _StoreAction)
1230 self.register('action', 'store_const', _StoreConstAction)
1231 self.register('action', 'store_true', _StoreTrueAction)
1232 self.register('action', 'store_false', _StoreFalseAction)
1233 self.register('action', 'append', _AppendAction)
1234 self.register('action', 'append_const', _AppendConstAction)
1235 self.register('action', 'count', _CountAction)
1236 self.register('action', 'help', _HelpAction)
1237 self.register('action', 'version', _VersionAction)
1238 self.register('action', 'parsers', _SubParsersAction)
1239
1240 # raise an exception if the conflict handler is invalid
1241 self._get_handler()
1242
1243 # action storage
1244 self._actions = []
1245 self._option_string_actions = {}
1246
1247 # groups
1248 self._action_groups = []
1249 self._mutually_exclusive_groups = []
1250
1251 # defaults storage
1252 self._defaults = {}
1253
1254 # determines whether an "option" looks like a negative number
1255 self._negative_number_matcher = _re.compile(r'^-\d+$|^-\d*\.\d+$')
1256
1257 # whether or not there are any optionals that look like negative
1258 # numbers -- uses a list so it can be shared and edited
1259 self._has_negative_number_optionals = []
1260
1261 # ====================
1262 # Registration methods
1263 # ====================
1264 def register(self, registry_name, value, object):
1265 registry = self._registries.setdefault(registry_name, {})
1266 registry[value] = object
1267
1268 def _registry_get(self, registry_name, value, default=None):
1269 return self._registries[registry_name].get(value, default)
1270
1271 # ==================================
1272 # Namespace default accessor methods
1273 # ==================================
1274 def set_defaults(self, **kwargs):
1275 self._defaults.update(kwargs)
1276
1277 # if these defaults match any existing arguments, replace
1278 # the previous default on the object with the new one
1279 for action in self._actions:
1280 if action.dest in kwargs:
1281 action.default = kwargs[action.dest]
1282
1283 def get_default(self, dest):
1284 for action in self._actions:
1285 if action.dest == dest and action.default is not None:
1286 return action.default
1287 return self._defaults.get(dest, None)
1288
1289
1290 # =======================
1291 # Adding argument actions
1292 # =======================
1293 def add_argument(self, *args, **kwargs):
1294 """
1295 add_argument(dest, ..., name=value, ...)
1296 add_argument(option_string, option_string, ..., name=value, ...)
1297 """
1298
1299 # if no positional args are supplied or only one is supplied and
1300 # it doesn't look like an option string, parse a positional
1301 # argument
1302 chars = self.prefix_chars
1303 if not args or len(args) == 1 and args[0][0] not in chars:
1304 if args and 'dest' in kwargs:
1305 raise ValueError('dest supplied twice for positional argument')
1306 kwargs = self._get_positional_kwargs(*args, **kwargs)
1307
1308 # otherwise, we're adding an optional argument
1309 else:
1310 kwargs = self._get_optional_kwargs(*args, **kwargs)
1311
1312 # if no default was supplied, use the parser-level default
1313 if 'default' not in kwargs:
1314 dest = kwargs['dest']
1315 if dest in self._defaults:
1316 kwargs['default'] = self._defaults[dest]
1317 elif self.argument_default is not None:
1318 kwargs['default'] = self.argument_default
1319
1320 # create the action object, and add it to the parser
1321 action_class = self._pop_action_class(kwargs)
Florent Xicluna5d1155c2011-10-28 14:45:05 +02001322 if not callable(action_class):
Steven Bethard7cb20a82011-04-04 01:53:02 +02001323 raise ValueError('unknown action "%s"' % (action_class,))
Benjamin Peterson698a18a2010-03-02 22:34:37 +00001324 action = action_class(**kwargs)
1325
1326 # raise an error if the action type is not callable
1327 type_func = self._registry_get('type', action.type, action.type)
Florent Xicluna5d1155c2011-10-28 14:45:05 +02001328 if not callable(type_func):
Steven Bethard7cb20a82011-04-04 01:53:02 +02001329 raise ValueError('%r is not callable' % (type_func,))
Benjamin Peterson698a18a2010-03-02 22:34:37 +00001330
Steven Bethard8d9a4622011-03-26 17:33:56 +01001331 # raise an error if the metavar does not match the type
1332 if hasattr(self, "_get_formatter"):
1333 try:
1334 self._get_formatter()._format_args(action, None)
1335 except TypeError:
1336 raise ValueError("length of metavar tuple does not match nargs")
1337
Benjamin Peterson698a18a2010-03-02 22:34:37 +00001338 return self._add_action(action)
1339
1340 def add_argument_group(self, *args, **kwargs):
1341 group = _ArgumentGroup(self, *args, **kwargs)
1342 self._action_groups.append(group)
1343 return group
1344
1345 def add_mutually_exclusive_group(self, **kwargs):
1346 group = _MutuallyExclusiveGroup(self, **kwargs)
1347 self._mutually_exclusive_groups.append(group)
1348 return group
1349
1350 def _add_action(self, action):
1351 # resolve any conflicts
1352 self._check_conflict(action)
1353
1354 # add to actions list
1355 self._actions.append(action)
1356 action.container = self
1357
1358 # index the action by any option strings it has
1359 for option_string in action.option_strings:
1360 self._option_string_actions[option_string] = action
1361
1362 # set the flag if any option strings look like negative numbers
1363 for option_string in action.option_strings:
1364 if self._negative_number_matcher.match(option_string):
1365 if not self._has_negative_number_optionals:
1366 self._has_negative_number_optionals.append(True)
1367
1368 # return the created action
1369 return action
1370
1371 def _remove_action(self, action):
1372 self._actions.remove(action)
1373
1374 def _add_container_actions(self, container):
1375 # collect groups by titles
1376 title_group_map = {}
1377 for group in self._action_groups:
1378 if group.title in title_group_map:
1379 msg = _('cannot merge actions - two groups are named %r')
1380 raise ValueError(msg % (group.title))
1381 title_group_map[group.title] = group
1382
1383 # map each action to its group
1384 group_map = {}
1385 for group in container._action_groups:
1386
1387 # if a group with the title exists, use that, otherwise
1388 # create a new group matching the container's group
1389 if group.title not in title_group_map:
1390 title_group_map[group.title] = self.add_argument_group(
1391 title=group.title,
1392 description=group.description,
1393 conflict_handler=group.conflict_handler)
1394
1395 # map the actions to their new group
1396 for action in group._group_actions:
1397 group_map[action] = title_group_map[group.title]
1398
1399 # add container's mutually exclusive groups
1400 # NOTE: if add_mutually_exclusive_group ever gains title= and
1401 # description= then this code will need to be expanded as above
1402 for group in container._mutually_exclusive_groups:
1403 mutex_group = self.add_mutually_exclusive_group(
1404 required=group.required)
1405
1406 # map the actions to their new mutex group
1407 for action in group._group_actions:
1408 group_map[action] = mutex_group
1409
1410 # add all actions to this container or their group
1411 for action in container._actions:
1412 group_map.get(action, self)._add_action(action)
1413
1414 def _get_positional_kwargs(self, dest, **kwargs):
1415 # make sure required is not specified
1416 if 'required' in kwargs:
1417 msg = _("'required' is an invalid argument for positionals")
1418 raise TypeError(msg)
1419
1420 # mark positional arguments as required if at least one is
1421 # always required
1422 if kwargs.get('nargs') not in [OPTIONAL, ZERO_OR_MORE]:
1423 kwargs['required'] = True
1424 if kwargs.get('nargs') == ZERO_OR_MORE and 'default' not in kwargs:
1425 kwargs['required'] = True
1426
1427 # return the keyword arguments with no option strings
1428 return dict(kwargs, dest=dest, option_strings=[])
1429
1430 def _get_optional_kwargs(self, *args, **kwargs):
1431 # determine short and long option strings
1432 option_strings = []
1433 long_option_strings = []
1434 for option_string in args:
1435 # error on strings that don't start with an appropriate prefix
1436 if not option_string[0] in self.prefix_chars:
Éric Araujobb48a8b2010-12-03 19:41:00 +00001437 args = {'option': option_string,
1438 'prefix_chars': self.prefix_chars}
1439 msg = _('invalid option string %(option)r: '
1440 'must start with a character %(prefix_chars)r')
1441 raise ValueError(msg % args)
Benjamin Peterson698a18a2010-03-02 22:34:37 +00001442
1443 # strings starting with two prefix characters are long options
1444 option_strings.append(option_string)
1445 if option_string[0] in self.prefix_chars:
1446 if len(option_string) > 1:
1447 if option_string[1] in self.prefix_chars:
1448 long_option_strings.append(option_string)
1449
1450 # infer destination, '--foo-bar' -> 'foo_bar' and '-x' -> 'x'
1451 dest = kwargs.pop('dest', None)
1452 if dest is None:
1453 if long_option_strings:
1454 dest_option_string = long_option_strings[0]
1455 else:
1456 dest_option_string = option_strings[0]
1457 dest = dest_option_string.lstrip(self.prefix_chars)
1458 if not dest:
1459 msg = _('dest= is required for options like %r')
1460 raise ValueError(msg % option_string)
1461 dest = dest.replace('-', '_')
1462
1463 # return the updated keyword arguments
1464 return dict(kwargs, dest=dest, option_strings=option_strings)
1465
1466 def _pop_action_class(self, kwargs, default=None):
1467 action = kwargs.pop('action', default)
1468 return self._registry_get('action', action, action)
1469
1470 def _get_handler(self):
1471 # determine function from conflict handler string
1472 handler_func_name = '_handle_conflict_%s' % self.conflict_handler
1473 try:
1474 return getattr(self, handler_func_name)
1475 except AttributeError:
1476 msg = _('invalid conflict_resolution value: %r')
1477 raise ValueError(msg % self.conflict_handler)
1478
1479 def _check_conflict(self, action):
1480
1481 # find all options that conflict with this option
1482 confl_optionals = []
1483 for option_string in action.option_strings:
1484 if option_string in self._option_string_actions:
1485 confl_optional = self._option_string_actions[option_string]
1486 confl_optionals.append((option_string, confl_optional))
1487
1488 # resolve any conflicts
1489 if confl_optionals:
1490 conflict_handler = self._get_handler()
1491 conflict_handler(action, confl_optionals)
1492
1493 def _handle_conflict_error(self, action, conflicting_actions):
Éric Araujo12159152010-12-04 17:31:49 +00001494 message = ngettext('conflicting option string: %s',
1495 'conflicting option strings: %s',
1496 len(conflicting_actions))
Benjamin Peterson698a18a2010-03-02 22:34:37 +00001497 conflict_string = ', '.join([option_string
1498 for option_string, action
1499 in conflicting_actions])
1500 raise ArgumentError(action, message % conflict_string)
1501
1502 def _handle_conflict_resolve(self, action, conflicting_actions):
1503
1504 # remove all conflicting options
1505 for option_string, action in conflicting_actions:
1506
1507 # remove the conflicting option
1508 action.option_strings.remove(option_string)
1509 self._option_string_actions.pop(option_string, None)
1510
1511 # if the option now has no option string, remove it from the
1512 # container holding it
1513 if not action.option_strings:
1514 action.container._remove_action(action)
1515
1516
1517class _ArgumentGroup(_ActionsContainer):
1518
1519 def __init__(self, container, title=None, description=None, **kwargs):
1520 # add any missing keyword arguments by checking the container
1521 update = kwargs.setdefault
1522 update('conflict_handler', container.conflict_handler)
1523 update('prefix_chars', container.prefix_chars)
1524 update('argument_default', container.argument_default)
1525 super_init = super(_ArgumentGroup, self).__init__
1526 super_init(description=description, **kwargs)
1527
1528 # group attributes
1529 self.title = title
1530 self._group_actions = []
1531
1532 # share most attributes with the container
1533 self._registries = container._registries
1534 self._actions = container._actions
1535 self._option_string_actions = container._option_string_actions
1536 self._defaults = container._defaults
1537 self._has_negative_number_optionals = \
1538 container._has_negative_number_optionals
Georg Brandl0f6b47a2011-01-30 12:19:35 +00001539 self._mutually_exclusive_groups = container._mutually_exclusive_groups
Benjamin Peterson698a18a2010-03-02 22:34:37 +00001540
1541 def _add_action(self, action):
1542 action = super(_ArgumentGroup, self)._add_action(action)
1543 self._group_actions.append(action)
1544 return action
1545
1546 def _remove_action(self, action):
1547 super(_ArgumentGroup, self)._remove_action(action)
1548 self._group_actions.remove(action)
1549
1550
1551class _MutuallyExclusiveGroup(_ArgumentGroup):
1552
1553 def __init__(self, container, required=False):
1554 super(_MutuallyExclusiveGroup, self).__init__(container)
1555 self.required = required
1556 self._container = container
1557
1558 def _add_action(self, action):
1559 if action.required:
1560 msg = _('mutually exclusive arguments must be optional')
1561 raise ValueError(msg)
1562 action = self._container._add_action(action)
1563 self._group_actions.append(action)
1564 return action
1565
1566 def _remove_action(self, action):
1567 self._container._remove_action(action)
1568 self._group_actions.remove(action)
1569
1570
1571class ArgumentParser(_AttributeHolder, _ActionsContainer):
1572 """Object for parsing command line strings into Python objects.
1573
1574 Keyword Arguments:
1575 - prog -- The name of the program (default: sys.argv[0])
1576 - usage -- A usage message (default: auto-generated from arguments)
1577 - description -- A description of what the program does
1578 - epilog -- Text following the argument descriptions
1579 - parents -- Parsers whose arguments should be copied into this one
1580 - formatter_class -- HelpFormatter class for printing help messages
1581 - prefix_chars -- Characters that prefix optional arguments
1582 - fromfile_prefix_chars -- Characters that prefix files containing
1583 additional arguments
1584 - argument_default -- The default value for all arguments
1585 - conflict_handler -- String indicating how to handle conflicts
1586 - add_help -- Add a -h/-help option
1587 """
1588
1589 def __init__(self,
1590 prog=None,
1591 usage=None,
1592 description=None,
1593 epilog=None,
Benjamin Peterson698a18a2010-03-02 22:34:37 +00001594 parents=[],
1595 formatter_class=HelpFormatter,
1596 prefix_chars='-',
1597 fromfile_prefix_chars=None,
1598 argument_default=None,
1599 conflict_handler='error',
1600 add_help=True):
1601
Benjamin Peterson698a18a2010-03-02 22:34:37 +00001602 superinit = super(ArgumentParser, self).__init__
1603 superinit(description=description,
1604 prefix_chars=prefix_chars,
1605 argument_default=argument_default,
1606 conflict_handler=conflict_handler)
1607
1608 # default setting for prog
1609 if prog is None:
1610 prog = _os.path.basename(_sys.argv[0])
1611
1612 self.prog = prog
1613 self.usage = usage
1614 self.epilog = epilog
Benjamin Peterson698a18a2010-03-02 22:34:37 +00001615 self.formatter_class = formatter_class
1616 self.fromfile_prefix_chars = fromfile_prefix_chars
1617 self.add_help = add_help
1618
1619 add_group = self.add_argument_group
1620 self._positionals = add_group(_('positional arguments'))
1621 self._optionals = add_group(_('optional arguments'))
1622 self._subparsers = None
1623
1624 # register types
1625 def identity(string):
1626 return string
1627 self.register('type', None, identity)
1628
Florent Xiclunaaf1adbe2012-07-07 17:02:22 +02001629 # add help argument if necessary
Benjamin Peterson698a18a2010-03-02 22:34:37 +00001630 # (using explicit default to override global argument_default)
R. David Murray88c49fe2010-08-03 17:56:09 +00001631 default_prefix = '-' if '-' in prefix_chars else prefix_chars[0]
Benjamin Peterson698a18a2010-03-02 22:34:37 +00001632 if self.add_help:
1633 self.add_argument(
R. David Murray88c49fe2010-08-03 17:56:09 +00001634 default_prefix+'h', default_prefix*2+'help',
1635 action='help', default=SUPPRESS,
Benjamin Peterson698a18a2010-03-02 22:34:37 +00001636 help=_('show this help message and exit'))
Benjamin Peterson698a18a2010-03-02 22:34:37 +00001637
1638 # add parent arguments and defaults
1639 for parent in parents:
1640 self._add_container_actions(parent)
1641 try:
1642 defaults = parent._defaults
1643 except AttributeError:
1644 pass
1645 else:
1646 self._defaults.update(defaults)
1647
1648 # =======================
1649 # Pretty __repr__ methods
1650 # =======================
1651 def _get_kwargs(self):
1652 names = [
1653 'prog',
1654 'usage',
1655 'description',
Benjamin Peterson698a18a2010-03-02 22:34:37 +00001656 'formatter_class',
1657 'conflict_handler',
1658 'add_help',
1659 ]
1660 return [(name, getattr(self, name)) for name in names]
1661
1662 # ==================================
1663 # Optional/Positional adding methods
1664 # ==================================
1665 def add_subparsers(self, **kwargs):
1666 if self._subparsers is not None:
1667 self.error(_('cannot have multiple subparser arguments'))
1668
1669 # add the parser class to the arguments if it's not present
1670 kwargs.setdefault('parser_class', type(self))
1671
1672 if 'title' in kwargs or 'description' in kwargs:
1673 title = _(kwargs.pop('title', 'subcommands'))
1674 description = _(kwargs.pop('description', None))
1675 self._subparsers = self.add_argument_group(title, description)
1676 else:
1677 self._subparsers = self._positionals
1678
1679 # prog defaults to the usage message of this parser, skipping
1680 # optional arguments and with no "usage:" prefix
1681 if kwargs.get('prog') is None:
1682 formatter = self._get_formatter()
1683 positionals = self._get_positional_actions()
1684 groups = self._mutually_exclusive_groups
1685 formatter.add_usage(self.usage, positionals, groups, '')
1686 kwargs['prog'] = formatter.format_help().strip()
1687
1688 # create the parsers action and add it to the positionals list
1689 parsers_class = self._pop_action_class(kwargs, 'parsers')
1690 action = parsers_class(option_strings=[], **kwargs)
1691 self._subparsers._add_action(action)
1692
1693 # return the created parsers action
1694 return action
1695
1696 def _add_action(self, action):
1697 if action.option_strings:
1698 self._optionals._add_action(action)
1699 else:
1700 self._positionals._add_action(action)
1701 return action
1702
1703 def _get_optional_actions(self):
1704 return [action
1705 for action in self._actions
1706 if action.option_strings]
1707
1708 def _get_positional_actions(self):
1709 return [action
1710 for action in self._actions
1711 if not action.option_strings]
1712
1713 # =====================================
1714 # Command line argument parsing methods
1715 # =====================================
1716 def parse_args(self, args=None, namespace=None):
1717 args, argv = self.parse_known_args(args, namespace)
1718 if argv:
1719 msg = _('unrecognized arguments: %s')
1720 self.error(msg % ' '.join(argv))
1721 return args
1722
1723 def parse_known_args(self, args=None, namespace=None):
Benjamin Peterson698a18a2010-03-02 22:34:37 +00001724 if args is None:
R David Murrayb5228282012-09-08 12:08:01 -04001725 # args default to the system args
Benjamin Peterson698a18a2010-03-02 22:34:37 +00001726 args = _sys.argv[1:]
R David Murrayb5228282012-09-08 12:08:01 -04001727 else:
1728 # make sure that args are mutable
1729 args = list(args)
Benjamin Peterson698a18a2010-03-02 22:34:37 +00001730
1731 # default Namespace built from parser defaults
1732 if namespace is None:
1733 namespace = Namespace()
1734
1735 # add any action defaults that aren't present
1736 for action in self._actions:
1737 if action.dest is not SUPPRESS:
1738 if not hasattr(namespace, action.dest):
1739 if action.default is not SUPPRESS:
R David Murray6fb8fb12012-08-31 22:45:20 -04001740 setattr(namespace, action.dest, action.default)
Benjamin Peterson698a18a2010-03-02 22:34:37 +00001741
1742 # add any parser defaults that aren't present
1743 for dest in self._defaults:
1744 if not hasattr(namespace, dest):
1745 setattr(namespace, dest, self._defaults[dest])
1746
1747 # parse the arguments and exit if there are any errors
1748 try:
Steven Bethardfca2e8a2010-11-02 12:47:22 +00001749 namespace, args = self._parse_known_args(args, namespace)
1750 if hasattr(namespace, _UNRECOGNIZED_ARGS_ATTR):
1751 args.extend(getattr(namespace, _UNRECOGNIZED_ARGS_ATTR))
1752 delattr(namespace, _UNRECOGNIZED_ARGS_ATTR)
1753 return namespace, args
Benjamin Peterson698a18a2010-03-02 22:34:37 +00001754 except ArgumentError:
1755 err = _sys.exc_info()[1]
1756 self.error(str(err))
1757
1758 def _parse_known_args(self, arg_strings, namespace):
1759 # replace arg strings that are file references
1760 if self.fromfile_prefix_chars is not None:
1761 arg_strings = self._read_args_from_files(arg_strings)
1762
1763 # map all mutually exclusive arguments to the other arguments
1764 # they can't occur with
1765 action_conflicts = {}
1766 for mutex_group in self._mutually_exclusive_groups:
1767 group_actions = mutex_group._group_actions
1768 for i, mutex_action in enumerate(mutex_group._group_actions):
1769 conflicts = action_conflicts.setdefault(mutex_action, [])
1770 conflicts.extend(group_actions[:i])
1771 conflicts.extend(group_actions[i + 1:])
1772
1773 # find all option indices, and determine the arg_string_pattern
1774 # which has an 'O' if there is an option at an index,
1775 # an 'A' if there is an argument, or a '-' if there is a '--'
1776 option_string_indices = {}
1777 arg_string_pattern_parts = []
1778 arg_strings_iter = iter(arg_strings)
1779 for i, arg_string in enumerate(arg_strings_iter):
1780
1781 # all args after -- are non-options
1782 if arg_string == '--':
1783 arg_string_pattern_parts.append('-')
1784 for arg_string in arg_strings_iter:
1785 arg_string_pattern_parts.append('A')
1786
1787 # otherwise, add the arg to the arg strings
1788 # and note the index if it was an option
1789 else:
1790 option_tuple = self._parse_optional(arg_string)
1791 if option_tuple is None:
1792 pattern = 'A'
1793 else:
1794 option_string_indices[i] = option_tuple
1795 pattern = 'O'
1796 arg_string_pattern_parts.append(pattern)
1797
1798 # join the pieces together to form the pattern
1799 arg_strings_pattern = ''.join(arg_string_pattern_parts)
1800
1801 # converts arg strings to the appropriate and then takes the action
Benjamin Peterson16f2fd02010-03-02 23:09:38 +00001802 seen_actions = set()
1803 seen_non_default_actions = set()
Benjamin Peterson698a18a2010-03-02 22:34:37 +00001804
1805 def take_action(action, argument_strings, option_string=None):
1806 seen_actions.add(action)
1807 argument_values = self._get_values(action, argument_strings)
1808
1809 # error if this argument is not allowed with other previously
1810 # seen arguments, assuming that actions that use the default
1811 # value don't really count as "present"
1812 if argument_values is not action.default:
1813 seen_non_default_actions.add(action)
1814 for conflict_action in action_conflicts.get(action, []):
1815 if conflict_action in seen_non_default_actions:
1816 msg = _('not allowed with argument %s')
1817 action_name = _get_action_name(conflict_action)
1818 raise ArgumentError(action, msg % action_name)
1819
1820 # take the action if we didn't receive a SUPPRESS value
1821 # (e.g. from a default)
1822 if argument_values is not SUPPRESS:
1823 action(self, namespace, argument_values, option_string)
1824
1825 # function to convert arg_strings into an optional action
1826 def consume_optional(start_index):
1827
1828 # get the optional identified at this index
1829 option_tuple = option_string_indices[start_index]
1830 action, option_string, explicit_arg = option_tuple
1831
1832 # identify additional optionals in the same arg string
1833 # (e.g. -xyz is the same as -x -y -z if no args are required)
1834 match_argument = self._match_argument
1835 action_tuples = []
1836 while True:
1837
1838 # if we found no optional action, skip it
1839 if action is None:
1840 extras.append(arg_strings[start_index])
1841 return start_index + 1
1842
1843 # if there is an explicit argument, try to match the
1844 # optional's string arguments to only this
1845 if explicit_arg is not None:
1846 arg_count = match_argument(action, 'A')
1847
1848 # if the action is a single-dash option and takes no
1849 # arguments, try to parse more single-dash options out
1850 # of the tail of the option string
1851 chars = self.prefix_chars
1852 if arg_count == 0 and option_string[1] not in chars:
1853 action_tuples.append((action, [], option_string))
Steven Bethard1ca45a52010-11-01 15:57:36 +00001854 char = option_string[0]
1855 option_string = char + explicit_arg[0]
1856 new_explicit_arg = explicit_arg[1:] or None
1857 optionals_map = self._option_string_actions
1858 if option_string in optionals_map:
1859 action = optionals_map[option_string]
1860 explicit_arg = new_explicit_arg
Benjamin Peterson698a18a2010-03-02 22:34:37 +00001861 else:
1862 msg = _('ignored explicit argument %r')
1863 raise ArgumentError(action, msg % explicit_arg)
1864
1865 # if the action expect exactly one argument, we've
1866 # successfully matched the option; exit the loop
1867 elif arg_count == 1:
1868 stop = start_index + 1
1869 args = [explicit_arg]
1870 action_tuples.append((action, args, option_string))
1871 break
1872
1873 # error if a double-dash option did not use the
1874 # explicit argument
1875 else:
1876 msg = _('ignored explicit argument %r')
1877 raise ArgumentError(action, msg % explicit_arg)
1878
1879 # if there is no explicit argument, try to match the
1880 # optional's string arguments with the following strings
1881 # if successful, exit the loop
1882 else:
1883 start = start_index + 1
1884 selected_patterns = arg_strings_pattern[start:]
1885 arg_count = match_argument(action, selected_patterns)
1886 stop = start + arg_count
1887 args = arg_strings[start:stop]
1888 action_tuples.append((action, args, option_string))
1889 break
1890
1891 # add the Optional to the list and return the index at which
1892 # the Optional's string args stopped
1893 assert action_tuples
1894 for action, args, option_string in action_tuples:
1895 take_action(action, args, option_string)
1896 return stop
1897
1898 # the list of Positionals left to be parsed; this is modified
1899 # by consume_positionals()
1900 positionals = self._get_positional_actions()
1901
1902 # function to convert arg_strings into positional actions
1903 def consume_positionals(start_index):
1904 # match as many Positionals as possible
1905 match_partial = self._match_arguments_partial
1906 selected_pattern = arg_strings_pattern[start_index:]
1907 arg_counts = match_partial(positionals, selected_pattern)
1908
1909 # slice off the appropriate arg strings for each Positional
1910 # and add the Positional and its args to the list
1911 for action, arg_count in zip(positionals, arg_counts):
1912 args = arg_strings[start_index: start_index + arg_count]
1913 start_index += arg_count
1914 take_action(action, args)
1915
1916 # slice off the Positionals that we just parsed and return the
1917 # index at which the Positionals' string args stopped
1918 positionals[:] = positionals[len(arg_counts):]
1919 return start_index
1920
1921 # consume Positionals and Optionals alternately, until we have
1922 # passed the last option string
1923 extras = []
1924 start_index = 0
1925 if option_string_indices:
1926 max_option_string_index = max(option_string_indices)
1927 else:
1928 max_option_string_index = -1
1929 while start_index <= max_option_string_index:
1930
1931 # consume any Positionals preceding the next option
1932 next_option_string_index = min([
1933 index
1934 for index in option_string_indices
1935 if index >= start_index])
1936 if start_index != next_option_string_index:
1937 positionals_end_index = consume_positionals(start_index)
1938
1939 # only try to parse the next optional if we didn't consume
1940 # the option string during the positionals parsing
1941 if positionals_end_index > start_index:
1942 start_index = positionals_end_index
1943 continue
1944 else:
1945 start_index = positionals_end_index
1946
1947 # if we consumed all the positionals we could and we're not
1948 # at the index of an option string, there were extra arguments
1949 if start_index not in option_string_indices:
1950 strings = arg_strings[start_index:next_option_string_index]
1951 extras.extend(strings)
1952 start_index = next_option_string_index
1953
1954 # consume the next optional and any arguments for it
1955 start_index = consume_optional(start_index)
1956
1957 # consume any positionals following the last Optional
1958 stop_index = consume_positionals(start_index)
1959
1960 # if we didn't consume all the argument strings, there were extras
1961 extras.extend(arg_strings[stop_index:])
1962
R David Murray64b0ef12012-08-31 23:09:34 -04001963 # make sure all required actions were present and also convert
1964 # action defaults which were not given as arguments
1965 required_actions = []
Benjamin Peterson698a18a2010-03-02 22:34:37 +00001966 for action in self._actions:
R David Murray6fb8fb12012-08-31 22:45:20 -04001967 if action not in seen_actions:
1968 if action.required:
R David Murray64b0ef12012-08-31 23:09:34 -04001969 required_actions.append(_get_action_name(action))
R David Murray6fb8fb12012-08-31 22:45:20 -04001970 else:
1971 # Convert action default now instead of doing it before
1972 # parsing arguments to avoid calling convert functions
1973 # twice (which may fail) if the argument was given, but
1974 # only if it was defined already in the namespace
1975 if (action.default is not None and
Barry Warsawd89774e2012-09-12 15:31:38 -04001976 isinstance(action.default, str) and
R David Murray64b0ef12012-08-31 23:09:34 -04001977 hasattr(namespace, action.dest) and
1978 action.default is getattr(namespace, action.dest)):
R David Murray6fb8fb12012-08-31 22:45:20 -04001979 setattr(namespace, action.dest,
1980 self._get_value(action, action.default))
Benjamin Peterson698a18a2010-03-02 22:34:37 +00001981
R David Murrayf97c59a2011-06-09 12:34:07 -04001982 if required_actions:
1983 self.error(_('the following arguments are required: %s') %
1984 ', '.join(required_actions))
Benjamin Peterson698a18a2010-03-02 22:34:37 +00001985
1986 # make sure all required groups had one option present
1987 for group in self._mutually_exclusive_groups:
1988 if group.required:
1989 for action in group._group_actions:
1990 if action in seen_non_default_actions:
1991 break
1992
1993 # if no actions were used, report the error
1994 else:
1995 names = [_get_action_name(action)
1996 for action in group._group_actions
1997 if action.help is not SUPPRESS]
1998 msg = _('one of the arguments %s is required')
1999 self.error(msg % ' '.join(names))
2000
2001 # return the updated namespace and the extra arguments
2002 return namespace, extras
2003
2004 def _read_args_from_files(self, arg_strings):
2005 # expand arguments referencing files
2006 new_arg_strings = []
2007 for arg_string in arg_strings:
2008
2009 # for regular arguments, just add them back into the list
R David Murrayb94082a2012-07-21 22:20:11 -04002010 if not arg_string or arg_string[0] not in self.fromfile_prefix_chars:
Benjamin Peterson698a18a2010-03-02 22:34:37 +00002011 new_arg_strings.append(arg_string)
2012
2013 # replace arguments referencing files with the file content
2014 else:
2015 try:
Giampaolo Rodola'2f50aaf2013-02-12 02:04:27 +01002016 with open(arg_string[1:]) as args_file:
Benjamin Peterson698a18a2010-03-02 22:34:37 +00002017 arg_strings = []
2018 for arg_line in args_file.read().splitlines():
2019 for arg in self.convert_arg_line_to_args(arg_line):
2020 arg_strings.append(arg)
2021 arg_strings = self._read_args_from_files(arg_strings)
2022 new_arg_strings.extend(arg_strings)
Andrew Svetlovf7a17b42012-12-25 16:47:37 +02002023 except OSError:
Benjamin Peterson698a18a2010-03-02 22:34:37 +00002024 err = _sys.exc_info()[1]
2025 self.error(str(err))
2026
2027 # return the modified argument list
2028 return new_arg_strings
2029
2030 def convert_arg_line_to_args(self, arg_line):
2031 return [arg_line]
2032
2033 def _match_argument(self, action, arg_strings_pattern):
2034 # match the pattern for this action to the arg strings
2035 nargs_pattern = self._get_nargs_pattern(action)
2036 match = _re.match(nargs_pattern, arg_strings_pattern)
2037
2038 # raise an exception if we weren't able to find a match
2039 if match is None:
2040 nargs_errors = {
2041 None: _('expected one argument'),
2042 OPTIONAL: _('expected at most one argument'),
2043 ONE_OR_MORE: _('expected at least one argument'),
2044 }
Éric Araujo12159152010-12-04 17:31:49 +00002045 default = ngettext('expected %s argument',
2046 'expected %s arguments',
2047 action.nargs) % action.nargs
Benjamin Peterson698a18a2010-03-02 22:34:37 +00002048 msg = nargs_errors.get(action.nargs, default)
2049 raise ArgumentError(action, msg)
2050
2051 # return the number of arguments matched
2052 return len(match.group(1))
2053
2054 def _match_arguments_partial(self, actions, arg_strings_pattern):
2055 # progressively shorten the actions list by slicing off the
2056 # final actions until we find a match
2057 result = []
2058 for i in range(len(actions), 0, -1):
2059 actions_slice = actions[:i]
2060 pattern = ''.join([self._get_nargs_pattern(action)
2061 for action in actions_slice])
2062 match = _re.match(pattern, arg_strings_pattern)
2063 if match is not None:
2064 result.extend([len(string) for string in match.groups()])
2065 break
2066
2067 # return the list of arg string counts
2068 return result
2069
2070 def _parse_optional(self, arg_string):
2071 # if it's an empty string, it was meant to be a positional
2072 if not arg_string:
2073 return None
2074
2075 # if it doesn't start with a prefix, it was meant to be positional
2076 if not arg_string[0] in self.prefix_chars:
2077 return None
2078
2079 # if the option string is present in the parser, return the action
2080 if arg_string in self._option_string_actions:
2081 action = self._option_string_actions[arg_string]
2082 return action, arg_string, None
2083
2084 # if it's just a single character, it was meant to be positional
2085 if len(arg_string) == 1:
2086 return None
2087
2088 # if the option string before the "=" is present, return the action
2089 if '=' in arg_string:
2090 option_string, explicit_arg = arg_string.split('=', 1)
2091 if option_string in self._option_string_actions:
2092 action = self._option_string_actions[option_string]
2093 return action, option_string, explicit_arg
2094
2095 # search through all possible prefixes of the option string
2096 # and all actions in the parser for possible interpretations
2097 option_tuples = self._get_option_tuples(arg_string)
2098
2099 # if multiple actions match, the option string was ambiguous
2100 if len(option_tuples) > 1:
2101 options = ', '.join([option_string
2102 for action, option_string, explicit_arg in option_tuples])
Éric Araujobb48a8b2010-12-03 19:41:00 +00002103 args = {'option': arg_string, 'matches': options}
2104 msg = _('ambiguous option: %(option)s could match %(matches)s')
2105 self.error(msg % args)
Benjamin Peterson698a18a2010-03-02 22:34:37 +00002106
2107 # if exactly one action matched, this segmentation is good,
2108 # so return the parsed action
2109 elif len(option_tuples) == 1:
2110 option_tuple, = option_tuples
2111 return option_tuple
2112
2113 # if it was not found as an option, but it looks like a negative
2114 # number, it was meant to be positional
2115 # unless there are negative-number-like options
2116 if self._negative_number_matcher.match(arg_string):
2117 if not self._has_negative_number_optionals:
2118 return None
2119
2120 # if it contains a space, it was meant to be a positional
2121 if ' ' in arg_string:
2122 return None
2123
2124 # it was meant to be an optional but there is no such option
2125 # in this parser (though it might be a valid option in a subparser)
2126 return None, arg_string, None
2127
2128 def _get_option_tuples(self, option_string):
2129 result = []
2130
2131 # option strings starting with two prefix characters are only
2132 # split at the '='
2133 chars = self.prefix_chars
2134 if option_string[0] in chars and option_string[1] in chars:
2135 if '=' in option_string:
2136 option_prefix, explicit_arg = option_string.split('=', 1)
2137 else:
2138 option_prefix = option_string
2139 explicit_arg = None
2140 for option_string in self._option_string_actions:
2141 if option_string.startswith(option_prefix):
2142 action = self._option_string_actions[option_string]
2143 tup = action, option_string, explicit_arg
2144 result.append(tup)
2145
2146 # single character options can be concatenated with their arguments
2147 # but multiple character options always have to have their argument
2148 # separate
2149 elif option_string[0] in chars and option_string[1] not in chars:
2150 option_prefix = option_string
2151 explicit_arg = None
2152 short_option_prefix = option_string[:2]
2153 short_explicit_arg = option_string[2:]
2154
2155 for option_string in self._option_string_actions:
2156 if option_string == short_option_prefix:
2157 action = self._option_string_actions[option_string]
2158 tup = action, option_string, short_explicit_arg
2159 result.append(tup)
2160 elif option_string.startswith(option_prefix):
2161 action = self._option_string_actions[option_string]
2162 tup = action, option_string, explicit_arg
2163 result.append(tup)
2164
2165 # shouldn't ever get here
2166 else:
2167 self.error(_('unexpected option string: %s') % option_string)
2168
2169 # return the collected option tuples
2170 return result
2171
2172 def _get_nargs_pattern(self, action):
2173 # in all examples below, we have to allow for '--' args
2174 # which are represented as '-' in the pattern
2175 nargs = action.nargs
2176
2177 # the default (None) is assumed to be a single argument
2178 if nargs is None:
2179 nargs_pattern = '(-*A-*)'
2180
2181 # allow zero or one arguments
2182 elif nargs == OPTIONAL:
2183 nargs_pattern = '(-*A?-*)'
2184
2185 # allow zero or more arguments
2186 elif nargs == ZERO_OR_MORE:
2187 nargs_pattern = '(-*[A-]*)'
2188
2189 # allow one or more arguments
2190 elif nargs == ONE_OR_MORE:
2191 nargs_pattern = '(-*A[A-]*)'
2192
2193 # allow any number of options or arguments
2194 elif nargs == REMAINDER:
2195 nargs_pattern = '([-AO]*)'
2196
2197 # allow one argument followed by any number of options or arguments
2198 elif nargs == PARSER:
2199 nargs_pattern = '(-*A[-AO]*)'
2200
2201 # all others should be integers
2202 else:
2203 nargs_pattern = '(-*%s-*)' % '-*'.join('A' * nargs)
2204
2205 # if this is an optional action, -- is not allowed
2206 if action.option_strings:
2207 nargs_pattern = nargs_pattern.replace('-*', '')
2208 nargs_pattern = nargs_pattern.replace('-', '')
2209
2210 # return the pattern
2211 return nargs_pattern
2212
2213 # ========================
2214 # Value conversion methods
2215 # ========================
2216 def _get_values(self, action, arg_strings):
R David Murray00528e82012-07-21 22:48:35 -04002217 # for everything but PARSER, REMAINDER args, strip out first '--'
Benjamin Peterson698a18a2010-03-02 22:34:37 +00002218 if action.nargs not in [PARSER, REMAINDER]:
R David Murray00528e82012-07-21 22:48:35 -04002219 try:
2220 arg_strings.remove('--')
2221 except ValueError:
2222 pass
Benjamin Peterson698a18a2010-03-02 22:34:37 +00002223
2224 # optional argument produces a default when not present
2225 if not arg_strings and action.nargs == OPTIONAL:
2226 if action.option_strings:
2227 value = action.const
2228 else:
2229 value = action.default
Benjamin Peterson16f2fd02010-03-02 23:09:38 +00002230 if isinstance(value, str):
Benjamin Peterson698a18a2010-03-02 22:34:37 +00002231 value = self._get_value(action, value)
2232 self._check_value(action, value)
2233
2234 # when nargs='*' on a positional, if there were no command-line
2235 # args, use the default if it is anything other than None
2236 elif (not arg_strings and action.nargs == ZERO_OR_MORE and
2237 not action.option_strings):
2238 if action.default is not None:
2239 value = action.default
2240 else:
2241 value = arg_strings
2242 self._check_value(action, value)
2243
2244 # single argument or optional argument produces a single value
2245 elif len(arg_strings) == 1 and action.nargs in [None, OPTIONAL]:
2246 arg_string, = arg_strings
2247 value = self._get_value(action, arg_string)
2248 self._check_value(action, value)
2249
2250 # REMAINDER arguments convert all values, checking none
2251 elif action.nargs == REMAINDER:
2252 value = [self._get_value(action, v) for v in arg_strings]
2253
2254 # PARSER arguments convert all values, but check only the first
2255 elif action.nargs == PARSER:
2256 value = [self._get_value(action, v) for v in arg_strings]
2257 self._check_value(action, value[0])
2258
2259 # all other types of nargs produce a list
2260 else:
2261 value = [self._get_value(action, v) for v in arg_strings]
2262 for v in value:
2263 self._check_value(action, v)
2264
2265 # return the converted value
2266 return value
2267
2268 def _get_value(self, action, arg_string):
2269 type_func = self._registry_get('type', action.type, action.type)
Florent Xicluna5d1155c2011-10-28 14:45:05 +02002270 if not callable(type_func):
Benjamin Peterson698a18a2010-03-02 22:34:37 +00002271 msg = _('%r is not callable')
2272 raise ArgumentError(action, msg % type_func)
2273
2274 # convert the value to the appropriate type
2275 try:
2276 result = type_func(arg_string)
2277
2278 # ArgumentTypeErrors indicate errors
2279 except ArgumentTypeError:
2280 name = getattr(action.type, '__name__', repr(action.type))
2281 msg = str(_sys.exc_info()[1])
2282 raise ArgumentError(action, msg)
2283
2284 # TypeErrors or ValueErrors also indicate errors
2285 except (TypeError, ValueError):
2286 name = getattr(action.type, '__name__', repr(action.type))
Éric Araujobb48a8b2010-12-03 19:41:00 +00002287 args = {'type': name, 'value': arg_string}
2288 msg = _('invalid %(type)s value: %(value)r')
2289 raise ArgumentError(action, msg % args)
Benjamin Peterson698a18a2010-03-02 22:34:37 +00002290
2291 # return the converted value
2292 return result
2293
2294 def _check_value(self, action, value):
2295 # converted value must be one of the choices (if specified)
2296 if action.choices is not None and value not in action.choices:
Éric Araujobb48a8b2010-12-03 19:41:00 +00002297 args = {'value': value,
2298 'choices': ', '.join(map(repr, action.choices))}
2299 msg = _('invalid choice: %(value)r (choose from %(choices)s)')
2300 raise ArgumentError(action, msg % args)
Benjamin Peterson698a18a2010-03-02 22:34:37 +00002301
2302 # =======================
2303 # Help-formatting methods
2304 # =======================
2305 def format_usage(self):
2306 formatter = self._get_formatter()
2307 formatter.add_usage(self.usage, self._actions,
2308 self._mutually_exclusive_groups)
2309 return formatter.format_help()
2310
2311 def format_help(self):
2312 formatter = self._get_formatter()
2313
2314 # usage
2315 formatter.add_usage(self.usage, self._actions,
2316 self._mutually_exclusive_groups)
2317
2318 # description
2319 formatter.add_text(self.description)
2320
2321 # positionals, optionals and user-defined groups
2322 for action_group in self._action_groups:
2323 formatter.start_section(action_group.title)
2324 formatter.add_text(action_group.description)
2325 formatter.add_arguments(action_group._group_actions)
2326 formatter.end_section()
2327
2328 # epilog
2329 formatter.add_text(self.epilog)
2330
2331 # determine help from format above
2332 return formatter.format_help()
2333
Benjamin Peterson698a18a2010-03-02 22:34:37 +00002334 def _get_formatter(self):
2335 return self.formatter_class(prog=self.prog)
2336
2337 # =====================
2338 # Help-printing methods
2339 # =====================
2340 def print_usage(self, file=None):
2341 if file is None:
2342 file = _sys.stdout
2343 self._print_message(self.format_usage(), file)
2344
2345 def print_help(self, file=None):
2346 if file is None:
2347 file = _sys.stdout
2348 self._print_message(self.format_help(), file)
2349
Benjamin Peterson698a18a2010-03-02 22:34:37 +00002350 def _print_message(self, message, file=None):
2351 if message:
2352 if file is None:
2353 file = _sys.stderr
2354 file.write(message)
2355
2356 # ===============
2357 # Exiting methods
2358 # ===============
2359 def exit(self, status=0, message=None):
2360 if message:
2361 self._print_message(message, _sys.stderr)
2362 _sys.exit(status)
2363
2364 def error(self, message):
2365 """error(message: string)
2366
2367 Prints a usage message incorporating the message to stderr and
2368 exits.
2369
2370 If you override this in a subclass, it should not return -- it
2371 should either exit or raise an exception.
2372 """
2373 self.print_usage(_sys.stderr)
Éric Araujobb48a8b2010-12-03 19:41:00 +00002374 args = {'prog': self.prog, 'message': message}
2375 self.exit(2, _('%(prog)s: error: %(message)s\n') % args)