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