1
# -*- coding: utf-8 -*-
3
# Copyright � 2006 Steven J. Bethard <steven.bethard@gmail.com>.
5
# Redistribution and use in source and binary forms, with or without
6
# modification, are permitted under the terms of the 3-clause BSD
7
# license. No warranty expressed or implied.
8
# For details, see the accompanying file LICENSE.txt.
10
"""Command-line parsing library
12
This module is an optparse-inspired command-line parsing library that:
14
* handles both optional and positional arguments
15
* produces highly informative usage messages
16
* supports parsers that dispatch to sub-parsers
18
The following is a simple usage example that sums integers from the
19
command-line and writes the result to a file:
21
parser = argparse.ArgumentParser(
22
description='sum the integers at the command line')
24
'integers', metavar='int', nargs='+', type=int,
25
help='an integer to be summed')
27
'--log', default=sys.stdout, type=argparse.FileType('w'),
28
help='the file where the sum should be written')
29
args = parser.parse_args()
30
args.log.write('%s' % sum(args.integers))
33
The module contains the following public classes:
35
ArgumentParser -- The main entry point for command-line parsing. As the
36
example above shows, the add_argument() method is used to populate
37
the parser with actions for optional and positional arguments. Then
38
the parse_args() method is invoked to convert the args at the
39
command-line into an object with attributes.
41
ArgumentError -- The exception raised by ArgumentParser objects when
42
there are errors with the parser's actions. Errors raised while
43
parsing the command-line are caught by ArgumentParser and emitted
44
as command-line messages.
46
FileType -- A factory for defining types of files to be created. As the
47
example above shows, instances of FileType are typically passed as
48
the type= argument of add_argument() calls.
50
Action -- The base class for parser actions. Typically actions are
51
selected by passing strings like 'store_true' or 'append_const' to
52
the action= argument of add_argument(). However, for greater
53
customization of ArgumentParser actions, subclasses of Action may
54
be defined and passed as the action= argument.
56
HelpFormatter, RawDescriptionHelpFormatter -- Formatter classes which
57
may be passed as the formatter_class= argument to the
58
ArgumentParser constructor. HelpFormatter is the default, while
59
RawDescriptionHelpFormatter tells the parser not to perform any
60
line-wrapping on description text.
62
All other classes in this module are considered implementation details.
63
(Also note that HelpFormatter and RawDescriptionHelpFormatter are only
64
considered public as object names -- the API of the formatter objects is
65
still considered an implementation detail.)
73
import textwrap as _textwrap
75
from gettext import gettext as _
77
SUPPRESS = '==SUPPRESS=='
84
# =============================
85
# Utility functions and classes
86
# =============================
88
class _AttributeHolder(object):
89
"""Abstract base class that provides __repr__.
91
The __repr__ method returns a string in the format:
92
ClassName(attr=name, attr=name, ...)
93
The attributes are determined either by a class-level attribute,
94
'_kwarg_names', or by inspecting the instance __dict__.
98
type_name = type(self).__name__
100
for arg in self._get_args():
101
arg_strings.append(repr(arg))
102
for name, value in self._get_kwargs():
103
arg_strings.append('%s=%r' % (name, value))
104
return '%s(%s)' % (type_name, ', '.join(arg_strings))
106
def _get_kwargs(self):
107
return sorted(self.__dict__.items())
112
def _ensure_value(namespace, name, value):
113
if getattr(namespace, name, None) is None:
114
setattr(namespace, name, value)
115
return getattr(namespace, name)
123
class HelpFormatter(object):
128
max_help_position=24,
131
# default setting for width
134
width = int(_os.environ['COLUMNS'])
135
except (KeyError, ValueError):
140
self._indent_increment = indent_increment
141
self._max_help_position = max_help_position
144
self._current_indent = 0
146
self._action_max_length = 0
148
self._root_section = self._Section(self, None)
149
self._current_section = self._root_section
151
self._whitespace_matcher = _re.compile(r'\s+')
152
self._long_break_matcher = _re.compile(r'\n\n\n+')
154
# ===============================
155
# Section and indentation methods
156
# ===============================
159
self._current_indent += self._indent_increment
163
self._current_indent -= self._indent_increment
164
assert self._current_indent >= 0, 'Indent decreased below 0.'
167
class _Section(object):
168
def __init__(self, formatter, parent, heading=None):
169
self.formatter = formatter
171
self.heading = heading
174
def format_help(self):
175
# format the indented section
176
if self.parent is not None:
177
self.formatter._indent()
178
join = self.formatter._join_parts
179
for func, args in self.items:
181
item_help = join(func(*args) for func, args in self.items)
182
if self.parent is not None:
183
self.formatter._dedent()
185
# return nothing if the section was empty
189
# add the heading if the section was non-empty
190
if self.heading is not SUPPRESS and self.heading is not None:
191
current_indent = self.formatter._current_indent
192
heading = '%*s%s:\n' % (current_indent, '', self.heading)
196
# join the section-initial newline, the heading and the help
197
return join(['\n', heading, item_help, '\n'])
199
def _add_item(self, func, args):
200
self._current_section.items.append((func, args))
202
# ========================
203
# Message building methods
204
# ========================
206
def start_section(self, heading):
208
section = self._Section(self, self._current_section, heading)
209
self._add_item(section.format_help, [])
210
self._current_section = section
212
def end_section(self):
213
self._current_section = self._current_section.parent
216
def add_text(self, text):
217
if text is not SUPPRESS and text is not None:
218
self._add_item(self._format_text, [text])
220
def add_usage(self, usage, optionals, positionals, prefix=None):
221
if usage is not SUPPRESS:
222
args = usage, optionals, positionals, prefix
223
self._add_item(self._format_usage, args)
225
def add_argument(self, action):
226
if action.help is not SUPPRESS:
228
# find all invocations
229
get_invocation = self._format_action_invocation
230
invocations = [get_invocation(action)]
231
for subaction in self._iter_indented_subactions(action):
232
invocations.append(get_invocation(subaction))
234
# update the maximum item length
235
invocation_length = max(len(s) for s in invocations)
236
action_length = invocation_length + self._current_indent
237
self._action_max_length = max(self._action_max_length,
240
# add the item to the list
241
self._add_item(self._format_action, [action])
243
def add_arguments(self, actions):
244
for action in actions:
245
self.add_argument(action)
247
# =======================
248
# Help-formatting methods
249
# =======================
251
def format_help(self):
252
help = self._root_section.format_help() % dict(prog=self._prog)
254
help = self._long_break_matcher.sub('\n\n', help)
255
help = help.strip('\n') + '\n'
258
def _join_parts(self, part_strings):
260
for part in part_strings
261
if part and part is not SUPPRESS)
263
def _format_usage(self, usage, optionals, positionals, prefix):
265
prefix = _('usage: ')
267
# if no optionals or positionals are available, usage is just prog
268
if usage is None and not optionals and not positionals:
271
# if optionals and positionals are available, calculate usage
273
usage = '%(prog)s' % dict(prog=self._prog)
275
# determine width of "usage: PROG" and width of text
276
prefix_width = len(prefix) + len(usage) + 1
277
prefix_indent = self._current_indent + prefix_width
278
text_width = self._width - self._current_indent
280
# put them on one line if they're short enough
281
format = self._format_actions_usage
282
action_usage = format(optionals + positionals)
283
if prefix_width + len(action_usage) + 1 < text_width:
284
usage = '%s %s' % (usage, action_usage)
286
# if they're long, wrap optionals and positionals individually
288
optional_usage = format(optionals)
289
positional_usage = format(positionals)
290
indent = ' ' * prefix_indent
292
# usage is made of PROG, optionals and positionals
295
# options always get added right after PROG
297
parts.append(_textwrap.fill(
298
optional_usage, text_width,
299
initial_indent=indent,
300
subsequent_indent=indent).lstrip())
302
# if there were options, put arguments on the next line
303
# otherwise, start them right after PROG
305
part = _textwrap.fill(
306
positional_usage, text_width,
307
initial_indent=indent,
308
subsequent_indent=indent).lstrip()
310
part = '\n' + indent + part
312
usage = ''.join(parts)
314
# prefix with 'usage:'
315
return '%s%s\n\n' % (prefix, usage)
317
def _format_actions_usage(self, actions):
319
for action in actions:
320
if action.help is SUPPRESS:
323
# produce all arg strings
324
if not action.option_strings:
325
parts.append(self._format_args(action, action.dest))
327
# produce the first way to invoke the option in brackets
329
option_string = action.option_strings[0]
331
# if the Optional doesn't take a value, format is:
333
if action.nargs == 0:
334
part = '%s' % option_string
336
# if the Optional takes a value, format is:
337
# -s ARGS or --long ARGS
339
default = action.dest.upper()
340
args_string = self._format_args(action, default)
341
part = '%s %s' % (option_string, args_string)
343
# make it look optional if it's not required
344
if not action.required:
348
return ' '.join(parts)
350
def _format_text(self, text):
351
text_width = self._width - self._current_indent
352
indent = ' ' * self._current_indent
353
return self._fill_text(text, text_width, indent) + '\n\n'
355
def _format_action(self, action):
356
# determine the required width and the entry label
357
help_position = min(self._action_max_length + 2,
358
self._max_help_position)
359
help_width = self._width - help_position
360
action_width = help_position - self._current_indent - 2
361
action_header = self._format_action_invocation(action)
363
# ho nelp; start on same line and add a final newline
365
tup = self._current_indent, '', action_header
366
action_header = '%*s%s\n' % tup
368
# short action name; start on the same line and pad two spaces
369
elif len(action_header) <= action_width:
370
tup = self._current_indent, '', action_width, action_header
371
action_header = '%*s%-*s ' % tup
374
# long action name; start on the next line
376
tup = self._current_indent, '', action_header
377
action_header = '%*s%s\n' % tup
378
indent_first = help_position
380
# collect the pieces of the action help
381
parts = [action_header]
383
# if there was help for the action, add lines of help text
385
help_text = self._expand_help(action)
386
help_lines = self._split_lines(help_text, help_width)
387
parts.append('%*s%s\n' % (indent_first, '', help_lines[0]))
388
for line in help_lines[1:]:
389
parts.append('%*s%s\n' % (help_position, '', line))
391
# or add a newline if the description doesn't end with one
392
elif not action_header.endswith('\n'):
395
# if there are any sub-actions, add their help as well
396
for subaction in self._iter_indented_subactions(action):
397
parts.append(self._format_action(subaction))
399
# return a single string
400
return self._join_parts(parts)
402
def _format_action_invocation(self, action):
403
if not action.option_strings:
404
return self._format_metavar(action, action.dest)
409
# if the Optional doesn't take a value, format is:
411
if action.nargs == 0:
412
parts.extend(action.option_strings)
414
# if the Optional takes a value, format is:
415
# -s ARGS, --long ARGS
417
default = action.dest.upper()
418
args_string = self._format_args(action, default)
419
for option_string in action.option_strings:
420
parts.append('%s %s' % (option_string, args_string))
422
return ', '.join(parts)
424
def _format_metavar(self, action, default_metavar):
425
if action.metavar is not None:
426
name = action.metavar
427
elif action.choices is not None:
428
choice_strs = (str(choice) for choice in action.choices)
429
name = '{%s}' % ','.join(choice_strs)
431
name = default_metavar
434
def _format_args(self, action, default_metavar):
435
name = self._format_metavar(action, default_metavar)
436
if action.nargs is None:
438
elif action.nargs == OPTIONAL:
439
result = '[%s]' % name
440
elif action.nargs == ZERO_OR_MORE:
441
result = '[%s [%s ...]]' % (name, name)
442
elif action.nargs == ONE_OR_MORE:
443
result = '%s [%s ...]' % (name, name)
444
elif action.nargs is PARSER:
445
result = '%s ...' % name
447
result = ' '.join([name] * action.nargs)
450
def _expand_help(self, action):
451
params = dict(vars(action), prog=self._prog)
452
for name, value in params.items():
453
if value is SUPPRESS:
455
if params.get('choices') is not None:
456
choices_str = ', '.join(str(c) for c in params['choices'])
457
params['choices'] = choices_str
458
return action.help % params
460
def _iter_indented_subactions(self, action):
462
get_subactions = action._get_subactions
463
except AttributeError:
467
for subaction in get_subactions():
471
def _split_lines(self, text, width):
472
text = self._whitespace_matcher.sub(' ', text).strip()
473
return _textwrap.wrap(text, width)
475
def _fill_text(self, text, width, indent):
476
text = self._whitespace_matcher.sub(' ', text).strip()
477
return _textwrap.fill(text, width, initial_indent=indent,
478
subsequent_indent=indent)
480
class RawDescriptionHelpFormatter(HelpFormatter):
482
def _fill_text(self, text, width, indent):
483
return ''.join(indent + line for line in text.splitlines(True))
485
class RawTextHelpFormatter(RawDescriptionHelpFormatter):
487
def _split_lines(self, text, width):
488
return text.splitlines()
490
# =====================
491
# Options and Arguments
492
# =====================
494
class ArgumentError(Exception):
495
"""ArgumentError(message, argument)
497
Raised whenever there was an error creating or using an argument
498
(optional or positional).
500
The string value of this exception is the message, augmented with
501
information about the argument that caused it.
504
def __init__(self, argument, message):
505
if argument.option_strings:
506
self.argument_name = '/'.join(argument.option_strings)
507
elif argument.metavar not in (None, SUPPRESS):
508
self.argument_name = argument.metavar
509
elif argument.dest not in (None, SUPPRESS):
510
self.argument_name = argument.dest
512
self.argument_name = None
513
self.message = message
516
if self.argument_name is None:
517
format = '%(message)s'
519
format = 'argument %(argument_name)s: %(message)s'
520
return format % dict(message=self.message,
521
argument_name=self.argument_name)
527
class Action(_AttributeHolder):
528
"""Action(*strings, **options)
530
Action objects hold the information necessary to convert a
531
set of command-line arguments (possibly including an initial option
532
string) into the desired Python object(s).
536
option_strings -- A list of command-line option strings which
537
should be associated with this action.
539
dest -- The name of the attribute to hold the created object(s)
541
nargs -- The number of command-line arguments that should be consumed.
542
By default, one argument will be consumed and a single value will
543
be produced. Other values include:
544
* N (an integer) consumes N arguments (and produces a list)
545
* '?' consumes zero or one arguments
546
* '*' consumes zero or more arguments (and produces a list)
547
* '+' consumes one or more arguments (and produces a list)
548
Note that the difference between the default and nargs=1 is that
549
with the default, a single value will be produced, while with
550
nargs=1, a list containing a single value will be produced.
552
const -- The value to be produced if the option is specified and the
553
option uses an action that takes no values.
555
default -- The value to be produced if the option is not specified.
557
type -- The type which the command-line arguments should be converted
558
to, should be one of 'string', 'int', 'float', 'complex' or a
559
callable object that accepts a single string argument. If None,
562
choices -- A container of values that should be allowed. If not None,
563
after a command-line argument has been converted to the appropriate
564
type, an exception will be raised if it is not a member of this
567
required -- True if the action must always be specified at the command
568
line. This is only meaningful for optional command-line arguments.
570
help -- The help string describing the argument.
572
metavar -- The name to be used for the option's argument with the help
573
string. If None, the 'dest' value will be used as the name.
588
self.option_strings = option_strings
592
self.default = default
594
self.choices = choices
595
self.required = required
597
self.metavar = metavar
599
def _get_kwargs(self):
611
return [(name, getattr(self, name)) for name in names]
613
def __call__(self, parser, namespace, values, option_string=None):
614
raise NotImplementedError(_('.__call__() not defined'))
616
class _StoreAction(Action):
629
raise ValueError('nargs must be > 0')
630
if const is not None and nargs != OPTIONAL:
631
raise ValueError('nargs must be %r to supply const' % OPTIONAL)
632
super(_StoreAction, self).__init__(
633
option_strings=option_strings,
644
def __call__(self, parser, namespace, values, option_string=None):
645
setattr(namespace, self.dest, values)
647
class _StoreConstAction(Action):
656
super(_StoreConstAction, self).__init__(
657
option_strings=option_strings,
665
def __call__(self, parser, namespace, values, option_string=None):
666
setattr(namespace, self.dest, self.const)
668
class _StoreTrueAction(_StoreConstAction):
675
super(_StoreTrueAction, self).__init__(
676
option_strings=option_strings,
683
class _StoreFalseAction(_StoreConstAction):
690
super(_StoreFalseAction, self).__init__(
691
option_strings=option_strings,
698
class _AppendAction(Action):
711
raise ValueError('nargs must be > 0')
712
if const is not None and nargs != OPTIONAL:
713
raise ValueError('nargs must be %r to supply const' % OPTIONAL)
714
super(_AppendAction, self).__init__(
715
option_strings=option_strings,
726
def __call__(self, parser, namespace, values, option_string=None):
727
_ensure_value(namespace, self.dest, []).append(values)
729
class _AppendConstAction(Action):
738
super(_AppendConstAction, self).__init__(
739
option_strings=option_strings,
748
def __call__(self, parser, namespace, values, option_string=None):
749
_ensure_value(namespace, self.dest, []).append(self.const)
751
class _CountAction(Action):
758
super(_CountAction, self).__init__(
759
option_strings=option_strings,
766
def __call__(self, parser, namespace, values, option_string=None):
767
new_count = _ensure_value(namespace, self.dest, 0) + 1
768
setattr(namespace, self.dest, new_count)
770
class _HelpAction(Action):
776
super(_HelpAction, self).__init__(
777
option_strings=option_strings,
783
def __call__(self, parser, namespace, values, option_string=None):
787
class _VersionAction(Action):
793
super(_VersionAction, self).__init__(
794
option_strings=option_strings,
800
def __call__(self, parser, namespace, values, option_string=None):
801
parser.print_version()
804
class _SubParsersAction(Action):
806
class _ChoicesPseudoAction(Action):
807
def __init__(self, name, help):
808
sup = super(_SubParsersAction._ChoicesPseudoAction, self)
809
sup.__init__(option_strings=[], dest=name, help=help)
820
self._prog_prefix = prog
821
self._parser_class = parser_class
822
self._name_parser_map = {}
823
self._choices_actions = []
825
super(_SubParsersAction, self).__init__(
826
option_strings=option_strings,
829
choices=self._name_parser_map,
833
def add_parser(self, name, **kwargs):
834
# set prog from the existing prefix
835
if kwargs.get('prog') is None:
836
kwargs['prog'] = '%s %s' % (self._prog_prefix, name)
838
# create a pseudo-action to hold the choice help
840
help = kwargs.pop('help')
841
choice_action = self._ChoicesPseudoAction(name, help)
842
self._choices_actions.append(choice_action)
844
# create the parser and add it to the map
845
parser = self._parser_class(**kwargs)
846
self._name_parser_map[name] = parser
849
def _get_subactions(self):
850
return self._choices_actions
852
def __call__(self, parser, namespace, values, option_string=None):
853
parser_name = values[0]
854
arg_strings = values[1:]
856
# set the parser name if requested
857
if self.dest is not SUPPRESS:
858
setattr(namespace, self.dest, parser_name)
862
parser = self._name_parser_map[parser_name]
864
tup = parser_name, ', '.join(self._name_parser_map)
865
msg = _('unknown parser %r (choices: %s)' % tup)
866
raise ArgumentError(self, msg)
868
# parse all the remaining options into the namespace
869
parser.parse_args(arg_strings, namespace)
876
class FileType(object):
877
"""Factory for creating file object types
879
Instances of FileType are typically passed as type= arguments to the
880
ArgumentParser add_argument() method.
883
mode -- A string indicating how the file is to be opened. Accepts the
884
same values as the builtin open() function.
885
bufsize -- The file's desired buffer size. Accepts the same values as
886
the builtin open() function.
888
def __init__(self, mode='r', bufsize=None):
890
self._bufsize = bufsize
892
def __call__(self, string):
893
# the special argument "-" means sys.std{in,out}
895
if self._mode == 'r':
897
elif self._mode == 'w':
900
msg = _('argument "-" with mode %r' % self._mode)
901
raise ValueError(msg)
903
# all other arguments are used as file names
905
return open(string, self._mode, self._bufsize)
907
return open(string, self._mode)
910
# ===========================
911
# Optional and Positional Parsing
912
# ===========================
914
class Namespace(_AttributeHolder):
916
def __init__(self, **kwargs):
917
for name, value in kwargs.iteritems():
918
setattr(self, name, value)
920
def __eq__(self, other):
921
return vars(self) == vars(other)
923
def __ne__(self, other):
924
return not (self == other)
927
class _ActionsContainer(object):
933
super(_ActionsContainer, self).__init__()
935
self.description = description
936
self.argument_default = argument_default
937
self.prefix_chars = prefix_chars
938
self.conflict_handler = conflict_handler
941
self._registries = {}
944
self.register('action', None, _StoreAction)
945
self.register('action', 'store', _StoreAction)
946
self.register('action', 'store_const', _StoreConstAction)
947
self.register('action', 'store_true', _StoreTrueAction)
948
self.register('action', 'store_false', _StoreFalseAction)
949
self.register('action', 'append', _AppendAction)
950
self.register('action', 'append_const', _AppendConstAction)
951
self.register('action', 'count', _CountAction)
952
self.register('action', 'help', _HelpAction)
953
self.register('action', 'version', _VersionAction)
954
self.register('action', 'parsers', _SubParsersAction)
956
# raise an exception if the conflict handler is invalid
960
self._optional_actions_list = []
961
self._positional_actions_list = []
962
self._positional_actions_full_list = []
963
self._option_strings = {}
968
# ====================
969
# Registration methods
970
# ====================
972
def register(self, registry_name, value, object):
973
registry = self._registries.setdefault(registry_name, {})
974
registry[value] = object
976
def _registry_get(self, registry_name, value, default=None):
977
return self._registries[registry_name].get(value, default)
979
# ==================================
980
# Namespace default settings methods
981
# ==================================
983
def set_defaults(self, **kwargs):
984
self._defaults.update(kwargs)
986
# if these defaults match any existing arguments, replace
987
# the previous default on the object with the new one
988
for action_list in [self._option_strings.values(),
989
self._positional_actions_full_list]:
990
for action in action_list:
991
if action.dest in kwargs:
992
action.default = kwargs[action.dest]
994
# =======================
995
# Adding argument actions
996
# =======================
998
def add_argument(self, *args, **kwargs):
1000
add_argument(dest, ..., name=value, ...)
1001
add_argument(option_string, option_string, ..., name=value, ...)
1004
# if no positional args are supplied or only one is supplied and
1005
# it doesn't look like an option string, parse a positional
1007
chars = self.prefix_chars
1008
if not args or len(args) == 1 and args[0][0] not in chars:
1009
kwargs = self._get_positional_kwargs(*args, **kwargs)
1011
# otherwise, we're adding an optional argument
1013
kwargs = self._get_optional_kwargs(*args, **kwargs)
1015
# if no default was supplied, use the parser-level default
1016
if 'default' not in kwargs:
1017
dest = kwargs['dest']
1018
if dest in self._defaults:
1019
kwargs['default'] = self._defaults[dest]
1020
elif self.argument_default is not None:
1021
kwargs['default'] = self.argument_default
1023
# create the action object, and add it to the parser
1024
action_class = self._pop_action_class(kwargs)
1025
action = action_class(**kwargs)
1026
return self._add_action(action)
1028
def _add_action(self, action):
1029
# resolve any conflicts
1030
self._check_conflict(action)
1032
# add to optional or positional list
1033
if action.option_strings:
1034
self._optional_actions_list.append(action)
1036
self._positional_actions_list.append(action)
1037
self._positional_actions_full_list.append(action)
1038
action.container = self
1040
# index the action by any option strings it has
1041
for option_string in action.option_strings:
1042
self._option_strings[option_string] = action
1044
# return the created action
1047
def _add_container_actions(self, container):
1048
for action in container._optional_actions_list:
1049
self._add_action(action)
1050
for action in container._positional_actions_list:
1051
self._add_action(action)
1053
def _get_positional_kwargs(self, dest, **kwargs):
1054
# make sure required is not specified
1055
if 'required' in kwargs:
1056
msg = _("'required' is an invalid argument for positionals")
1057
raise TypeError(msg)
1059
# return the keyword arguments with no option strings
1060
return dict(kwargs, dest=dest, option_strings=[])
1062
def _get_optional_kwargs(self, *args, **kwargs):
1063
# determine short and long option strings
1065
long_option_strings = []
1066
for option_string in args:
1067
# error on one-or-fewer-character option strings
1068
if len(option_string) < 2:
1069
msg = _('invalid option string %r: '
1070
'must be at least two characters long')
1071
raise ValueError(msg % option_string)
1073
# error on strings that don't start with an appropriate prefix
1074
if not option_string[0] in self.prefix_chars:
1075
msg = _('invalid option string %r: '
1076
'must start with a character %r')
1077
tup = option_string, self.prefix_chars
1078
raise ValueError(msg % tup)
1080
# error on strings that are all prefix characters
1081
if not (set(option_string) - set(self.prefix_chars)):
1082
msg = _('invalid option string %r: '
1083
'must contain characters other than %r')
1084
tup = option_string, self.prefix_chars
1085
raise ValueError(msg % tup)
1087
# strings starting with two prefix characters are long options
1088
option_strings.append(option_string)
1089
if option_string[0] in self.prefix_chars:
1090
if option_string[1] in self.prefix_chars:
1091
long_option_strings.append(option_string)
1093
# infer destination, '--foo-bar' -> 'foo_bar' and '-x' -> 'x'
1094
dest = kwargs.pop('dest', None)
1096
if long_option_strings:
1097
dest_option_string = long_option_strings[0]
1099
dest_option_string = option_strings[0]
1100
dest = dest_option_string.lstrip(self.prefix_chars)
1101
dest = dest.replace('-', '_')
1103
# return the updated keyword arguments
1104
return dict(kwargs, dest=dest, option_strings=option_strings)
1106
def _pop_action_class(self, kwargs, default=None):
1107
action = kwargs.pop('action', default)
1108
return self._registry_get('action', action, action)
1110
def _get_handler(self):
1111
# determine function from conflict handler string
1112
handler_func_name = '_handle_conflict_%s' % self.conflict_handler
1114
return getattr(self, handler_func_name)
1115
except AttributeError:
1116
msg = _('invalid conflict_resolution value: %r')
1117
raise ValueError(msg % self.conflict_handler)
1119
def _check_conflict(self, action):
1121
# find all options that conflict with this option
1122
confl_optionals = []
1123
for option_string in action.option_strings:
1124
if option_string in self._option_strings:
1125
confl_optional = self._option_strings[option_string]
1126
confl_optionals.append((option_string, confl_optional))
1128
# resolve any conflicts
1130
conflict_handler = self._get_handler()
1131
conflict_handler(action, confl_optionals)
1133
def _handle_conflict_error(self, action, conflicting_actions):
1134
message = _('conflicting option string(s): %s')
1135
conflict_string = ', '.join(option_string
1136
for option_string, action
1137
in conflicting_actions)
1138
raise ArgumentError(action, message % conflict_string)
1140
def _handle_conflict_resolve(self, action, conflicting_actions):
1142
# remove all conflicting options
1143
for option_string, action in conflicting_actions:
1145
# remove the conflicting option
1146
action.option_strings.remove(option_string)
1147
self._option_strings.pop(option_string, None)
1149
# if the option now has no option string, remove it from the
1150
# container holding it
1151
if not action.option_strings:
1152
action.container._optional_actions_list.remove(action)
1155
class _ArgumentGroup(_ActionsContainer):
1157
def __init__(self, container, title=None, description=None, **kwargs):
1158
# add any missing keyword arguments by checking the container
1159
update = kwargs.setdefault
1160
update('conflict_handler', container.conflict_handler)
1161
update('prefix_chars', container.prefix_chars)
1162
update('argument_default', container.argument_default)
1163
super_init = super(_ArgumentGroup, self).__init__
1164
super_init(description=description, **kwargs)
1167
self._registries = container._registries
1168
self._positional_actions_full_list = container._positional_actions_full_list
1169
self._option_strings = container._option_strings
1170
self._defaults = container._defaults
1173
class ArgumentParser(_AttributeHolder, _ActionsContainer):
1182
formatter_class=HelpFormatter,
1184
argument_default=None,
1185
conflict_handler='error',
1188
superinit = super(ArgumentParser, self).__init__
1189
superinit(description=description,
1190
prefix_chars=prefix_chars,
1191
argument_default=argument_default,
1192
conflict_handler=conflict_handler)
1194
# default setting for prog
1196
prog = _os.path.basename(_sys.argv[0])
1200
self.epilog = epilog
1201
self.version = version
1202
self.formatter_class = formatter_class
1203
self.add_help = add_help
1205
self._argument_group_class = _ArgumentGroup
1206
self._has_subparsers = False
1207
self._argument_groups = []
1210
def identity(string):
1212
self.register('type', None, identity)
1214
# add help and version arguments if necessary
1215
# (using explicit default to override global argument_default)
1218
'-h', '--help', action='help', default=SUPPRESS,
1219
help=_('show this help message and exit'))
1222
'-v', '--version', action='version', default=SUPPRESS,
1223
help=_("show program's version number and exit"))
1225
# add parent arguments and defaults
1226
for parent in parents:
1227
self._add_container_actions(parent)
1229
defaults = parent._defaults
1230
except AttributeError:
1233
self._defaults.update(defaults)
1235
# determines whether an "option" looks like a negative number
1236
self._negative_number_matcher = _re.compile(r'^-\d+|-\d*.\d+$')
1239
# =======================
1240
# Pretty __repr__ methods
1241
# =======================
1243
def _get_kwargs(self):
1253
return [(name, getattr(self, name)) for name in names]
1255
# ==================================
1256
# Optional/Positional adding methods
1257
# ==================================
1259
def add_argument_group(self, *args, **kwargs):
1260
group = self._argument_group_class(self, *args, **kwargs)
1261
self._argument_groups.append(group)
1264
def add_subparsers(self, **kwargs):
1265
if self._has_subparsers:
1266
self.error(_('cannot have multiple subparser arguments'))
1268
# add the parser class to the arguments if it's not present
1269
kwargs.setdefault('parser_class', type(self))
1271
# prog defaults to the usage message of this parser, skipping
1272
# optional arguments and with no "usage:" prefix
1273
if kwargs.get('prog') is None:
1274
formatter = self._get_formatter()
1275
formatter.add_usage(self.usage, [],
1276
self._get_positional_actions(), '')
1277
kwargs['prog'] = formatter.format_help().strip()
1279
# create the parsers action and add it to the positionals list
1280
parsers_class = self._pop_action_class(kwargs, 'parsers')
1281
action = parsers_class(option_strings=[], **kwargs)
1282
self._positional_actions_list.append(action)
1283
self._positional_actions_full_list.append(action)
1284
self._has_subparsers = True
1286
# return the created parsers action
1289
def _add_container_actions(self, container):
1290
super(ArgumentParser, self)._add_container_actions(container)
1292
groups = container._argument_groups
1293
except AttributeError:
1296
for group in groups:
1297
new_group = self.add_argument_group(
1299
description=group.description,
1300
conflict_handler=group.conflict_handler)
1301
new_group._add_container_actions(group)
1303
def _get_optional_actions(self):
1305
actions.extend(self._optional_actions_list)
1306
for argument_group in self._argument_groups:
1307
actions.extend(argument_group._optional_actions_list)
1310
def _get_positional_actions(self):
1311
return list(self._positional_actions_full_list)
1314
# =====================================
1315
# Command line argument parsing methods
1316
# =====================================
1318
def parse_args(self, args=None, namespace=None):
1319
# args default to the system args
1321
args = _sys.argv[1:]
1323
# default Namespace built from parser defaults
1324
if namespace is None:
1325
namespace = Namespace()
1327
# add any action defaults that aren't present
1328
optional_actions = self._get_optional_actions()
1329
positional_actions = self._get_positional_actions()
1330
for action in optional_actions + positional_actions:
1331
if action.dest is not SUPPRESS:
1332
if not hasattr(namespace, action.dest):
1333
if action.default is not SUPPRESS:
1334
default = action.default
1335
if isinstance(action.default, basestring):
1336
default = self._get_value(action, default)
1337
setattr(namespace, action.dest, default)
1339
# add any parser defaults that aren't present
1340
for dest, value in self._defaults.iteritems():
1341
if not hasattr(namespace, dest):
1342
setattr(namespace, dest, value)
1344
# parse the arguments and exit if there are any errors
1346
result = self._parse_args(args, namespace)
1347
except ArgumentError, err:
1348
self.error(str(err))
1350
# make sure all required optionals are present
1351
for action in self._get_optional_actions():
1353
if getattr(result, action.dest, None) is None:
1354
opt_strs = '/'.join(action.option_strings)
1355
msg = _('option %s is required' % opt_strs)
1358
# return the parsed arguments
1361
def _parse_args(self, arg_strings, namespace):
1363
# find all option indices, and determine the arg_string_pattern
1364
# which has an 'O' if there is an option at an index,
1365
# an 'A' if there is an argument, or a '-' if there is a '--'
1366
option_string_indices = {}
1367
arg_string_pattern_parts = []
1368
arg_strings_iter = iter(arg_strings)
1369
for i, arg_string in enumerate(arg_strings_iter):
1371
# all args after -- are non-options
1372
if arg_string == '--':
1373
arg_string_pattern_parts.append('-')
1374
for arg_string in arg_strings_iter:
1375
arg_string_pattern_parts.append('A')
1377
# otherwise, add the arg to the arg strings
1378
# and note the index if it was an option
1380
option_tuple = self._parse_optional(arg_string)
1381
if option_tuple is None:
1384
option_string_indices[i] = option_tuple
1386
arg_string_pattern_parts.append(pattern)
1388
# join the pieces together to form the pattern
1389
arg_strings_pattern = ''.join(arg_string_pattern_parts)
1391
# converts arg strings to the appropriate and then takes the action
1392
def take_action(action, argument_strings, option_string=None):
1393
argument_values = self._get_values(action, argument_strings)
1394
# take the action if we didn't receive a SUPPRESS value
1395
# (e.g. from a default)
1396
if argument_values is not SUPPRESS:
1397
action(self, namespace, argument_values, option_string)
1399
# function to convert arg_strings into an optional action
1400
def consume_optional(start_index):
1402
# determine the optional action and parse any explicit
1403
# argument out of the option string
1404
option_tuple = option_string_indices[start_index]
1405
action, option_string, explicit_arg = option_tuple
1407
# loop because single-dash options can be chained
1408
# (e.g. -xyz is the same as -x -y -z if no args are required)
1409
match_argument = self._match_argument
1413
# if we found no optional action, raise an error
1415
self.error(_('no such option: %s') % option_string)
1417
# if there is an explicit argument, try to match the
1418
# optional's string arguments to only this
1419
if explicit_arg is not None:
1420
arg_count = match_argument(action, 'A')
1422
# if the action is a single-dash option and takes no
1423
# arguments, try to parse more single-dash options out
1424
# of the tail of the option string
1425
chars = self.prefix_chars
1426
if arg_count == 0 and option_string[1] not in chars:
1427
action_tuples.append((action, [], option_string))
1428
parse_optional = self._parse_optional
1429
for char in self.prefix_chars:
1430
option_string = char + explicit_arg
1431
option_tuple = parse_optional(option_string)
1432
if option_tuple[0] is not None:
1435
msg = _('ignored explicit argument %r')
1436
raise ArgumentError(action, msg % explicit_arg)
1438
# set the action, etc. for the next loop iteration
1439
action, option_string, explicit_arg = option_tuple
1441
# if the action expect exactly one argument, we've
1442
# successfully matched the option; exit the loop
1443
elif arg_count == 1:
1444
stop = start_index + 1
1445
args = [explicit_arg]
1446
action_tuples.append((action, args, option_string))
1449
# error if a double-dash option did not use the
1452
msg = _('ignored explicit argument %r')
1453
raise ArgumentError(action, msg % explicit_arg)
1455
# if there is no explicit argument, try to match the
1456
# optional's string arguments with the following strings
1457
# if successful, exit the loop
1459
start = start_index + 1
1460
selected_patterns = arg_strings_pattern[start:]
1461
arg_count = match_argument(action, selected_patterns)
1462
stop = start + arg_count
1463
args = arg_strings[start:stop]
1464
action_tuples.append((action, args, option_string))
1467
# add the Optional to the list and return the index at which
1468
# the Optional's string args stopped
1469
assert action_tuples
1470
for action, args, option_string in action_tuples:
1471
take_action(action, args, option_string)
1474
# the list of Positionals left to be parsed; this is modified
1475
# by consume_positionals()
1476
positionals = self._get_positional_actions()
1478
# function to convert arg_strings into positional actions
1479
def consume_positionals(start_index):
1480
# match as many Positionals as possible
1481
match_partial = self._match_arguments_partial
1482
selected_pattern = arg_strings_pattern[start_index:]
1483
arg_counts = match_partial(positionals, selected_pattern)
1485
# slice off the appropriate arg strings for each Positional
1486
# and add the Positional and its args to the list
1487
for action, arg_count in zip(positionals, arg_counts):
1488
args = arg_strings[start_index: start_index + arg_count]
1489
start_index += arg_count
1490
take_action(action, args)
1492
# slice off the Positionals that we just parsed and return the
1493
# index at which the Positionals' string args stopped
1494
positionals[:] = positionals[len(arg_counts):]
1497
# consume Positionals and Optionals alternately, until we have
1498
# passed the last option string
1500
if option_string_indices:
1501
max_option_string_index = max(option_string_indices)
1503
max_option_string_index = -1
1504
while start_index <= max_option_string_index:
1506
# consume any Positionals preceding the next option
1507
next_option_string_index = min(
1509
for index in option_string_indices
1510
if index >= start_index)
1511
if start_index != next_option_string_index:
1512
positionals_end_index = consume_positionals(start_index)
1514
# only try to parse the next optional if we didn't consume
1515
# the option string during the positionals parsing
1516
if positionals_end_index > start_index:
1517
start_index = positionals_end_index
1520
start_index = positionals_end_index
1522
# if we consumed all the positionals we could and we're not
1523
# at the index of an option string, there were unparseable
1525
if start_index not in option_string_indices:
1526
msg = _('extra arguments found: %s')
1527
extras = arg_strings[start_index:next_option_string_index]
1528
self.error(msg % ' '.join(extras))
1530
# consume the next optional and any arguments for it
1531
start_index = consume_optional(start_index)
1533
# consume any positionals following the last Optional
1534
stop_index = consume_positionals(start_index)
1536
# if we didn't consume all the argument strings, there were too
1538
if stop_index != len(arg_strings):
1539
extras = arg_strings[stop_index:]
1540
self.error(_('extra arguments found: %s') % ' '.join(extras))
1542
# if we didn't use all the Positional objects, there were too few
1543
# arg strings supplied.
1545
self.error(_('too few arguments'))
1547
# return the updated namespace
1550
def _match_argument(self, action, arg_strings_pattern):
1551
# match the pattern for this action to the arg strings
1552
nargs_pattern = self._get_nargs_pattern(action)
1553
match = _re.match(nargs_pattern, arg_strings_pattern)
1555
# raise an exception if we weren't able to find a match
1558
None:_('expected one argument'),
1559
OPTIONAL:_('expected at most one argument'),
1560
ONE_OR_MORE:_('expected at least one argument')
1562
default = _('expected %s argument(s)') % action.nargs
1563
msg = nargs_errors.get(action.nargs, default)
1564
raise ArgumentError(action, msg)
1566
# return the number of arguments matched
1567
return len(match.group(1))
1569
def _match_arguments_partial(self, actions, arg_strings_pattern):
1570
# progressively shorten the actions list by slicing off the
1571
# final actions until we find a match
1573
for i in xrange(len(actions), 0, -1):
1574
actions_slice = actions[:i]
1575
pattern = ''.join(self._get_nargs_pattern(action)
1576
for action in actions_slice)
1577
match = _re.match(pattern, arg_strings_pattern)
1578
if match is not None:
1579
result.extend(len(string) for string in match.groups())
1582
# return the list of arg string counts
1585
def _parse_optional(self, arg_string):
1586
# if it doesn't start with a prefix, it was meant to be positional
1587
if not arg_string[0] in self.prefix_chars:
1590
# if it's just dashes, it was meant to be positional
1591
if not arg_string.strip('-'):
1594
# if the option string is present in the parser, return the action
1595
if arg_string in self._option_strings:
1596
action = self._option_strings[arg_string]
1597
return action, arg_string, None
1599
# search through all possible prefixes of the option string
1600
# and all actions in the parser for possible interpretations
1602
prefix_tuples = self._get_option_prefix_tuples(arg_string)
1603
for option_string in self._option_strings:
1604
for option_prefix, explicit_arg in prefix_tuples:
1605
if option_string.startswith(option_prefix):
1606
action = self._option_strings[option_string]
1607
tup = action, option_string, explicit_arg
1608
option_tuples.append(tup)
1611
# if multiple actions match, the option string was ambiguous
1612
if len(option_tuples) > 1:
1613
options = ', '.join(opt_str for _, opt_str, _ in option_tuples)
1614
tup = arg_string, options
1615
self.error(_('ambiguous option: %s could match %s') % tup)
1617
# if exactly one action matched, this segmentation is good,
1618
# so return the parsed action
1619
elif len(option_tuples) == 1:
1620
option_tuple, = option_tuples
1623
# if it was not found as an option, but it looks like a negative
1624
# number, it was meant to be positional
1625
if self._negative_number_matcher.match(arg_string):
1628
# it was meant to be an optional but there is no such option
1629
# in this parser (though it might be a valid option in a subparser)
1630
return None, arg_string, None
1632
def _get_option_prefix_tuples(self, option_string):
1635
# option strings starting with two prefix characters are only
1637
chars = self.prefix_chars
1638
if option_string[0] in chars and option_string[1] in chars:
1639
if '=' in option_string:
1640
option_prefix, explicit_arg = option_string.split('=', 1)
1642
option_prefix = option_string
1644
tup = option_prefix, explicit_arg
1647
# option strings starting with a single prefix character are
1648
# split at all indices
1650
for first_index, char in enumerate(option_string):
1651
if char not in self.prefix_chars:
1653
for i in xrange(len(option_string), first_index, -1):
1654
tup = option_string[:i], option_string[i:] or None
1657
# return the collected prefix tuples
1660
def _get_nargs_pattern(self, action):
1661
# in all examples below, we have to allow for '--' args
1662
# which are represented as '-' in the pattern
1663
nargs = action.nargs
1665
# the default (None) is assumed to be a single argument
1667
nargs_pattern = '(-*A-*)'
1669
# allow zero or one arguments
1670
elif nargs == OPTIONAL:
1671
nargs_pattern = '(-*A?-*)'
1673
# allow zero or more arguments
1674
elif nargs == ZERO_OR_MORE:
1675
nargs_pattern = '(-*[A-]*)'
1677
# allow one or more arguments
1678
elif nargs == ONE_OR_MORE:
1679
nargs_pattern = '(-*A[A-]*)'
1681
# allow one argument followed by any number of options or arguments
1682
elif nargs is PARSER:
1683
nargs_pattern = '(-*A[-AO]*)'
1685
# all others should be integers
1687
nargs_pattern = '(-*%s-*)' % '-*'.join('A' * nargs)
1689
# if this is an optional action, -- is not allowed
1690
if action.option_strings:
1691
nargs_pattern = nargs_pattern.replace('-*', '')
1692
nargs_pattern = nargs_pattern.replace('-', '')
1694
# return the pattern
1695
return nargs_pattern
1697
# ========================
1698
# Value conversion methods
1699
# ========================
1701
def _get_values(self, action, arg_strings):
1702
# for everything but PARSER args, strip out '--'
1703
if action.nargs is not PARSER:
1704
arg_strings = [s for s in arg_strings if s != '--']
1706
# optional argument produces a default when not present
1707
if not arg_strings and action.nargs == OPTIONAL:
1708
if action.option_strings:
1709
value = action.const
1711
value = action.default
1712
if isinstance(value, basestring):
1713
value = self._get_value(action, value)
1714
self._check_value(action, value)
1716
# when nargs='*' on a positional, if there were no command-line
1717
# args, use the default if it is anything other than None
1718
elif (not arg_strings and action.nargs == ZERO_OR_MORE and
1719
not action.option_strings):
1720
if action.default is not None:
1721
value = action.default
1724
self._check_value(action, value)
1726
# single argument or optional argument produces a single value
1727
elif len(arg_strings) == 1 and action.nargs in [None, OPTIONAL]:
1728
arg_string, = arg_strings
1729
value = self._get_value(action, arg_string)
1730
self._check_value(action, value)
1732
# PARSER arguments convert all values, but check only the first
1733
elif action.nargs is PARSER:
1734
value = list(self._get_value(action, v) for v in arg_strings)
1735
self._check_value(action, value[0])
1737
# all other types of nargs produce a list
1739
value = list(self._get_value(action, v) for v in arg_strings)
1741
self._check_value(action, v)
1743
# return the converted value
1746
def _get_value(self, action, arg_string):
1747
type_func = self._registry_get('type', action.type, action.type)
1748
if not callable(type_func):
1749
msg = _('%r is not callable')
1750
raise ArgumentError(action, msg % type_func)
1752
# convert the value to the appropriate type
1754
result = type_func(arg_string)
1756
# TypeErrors or ValueErrors indicate errors
1757
except (TypeError, ValueError):
1758
name = getattr(action.type, '__name__', repr(action.type))
1759
msg = _('invalid %s value: %r')
1760
raise ArgumentError(action, msg % (name, arg_string))
1762
# return the converted value
1765
def _check_value(self, action, value):
1766
# converted value must be one of the choices (if specified)
1767
if action.choices is not None and value not in action.choices:
1768
tup = value, ', '.join(map(repr, action.choices))
1769
msg = _('invalid choice: %r (choose from %s)') % tup
1770
raise ArgumentError(action, msg)
1774
# =======================
1775
# Help-formatting methods
1776
# =======================
1778
def format_usage(self):
1779
formatter = self._get_formatter()
1780
formatter.add_usage(self.usage,
1781
self._get_optional_actions(),
1782
self._get_positional_actions())
1783
return formatter.format_help()
1785
def format_help(self):
1786
formatter = self._get_formatter()
1789
formatter.add_usage(self.usage,
1790
self._get_optional_actions(),
1791
self._get_positional_actions())
1794
formatter.add_text(self.description)
1797
formatter.start_section(_('positional arguments'))
1798
formatter.add_arguments(self._positional_actions_list)
1799
formatter.end_section()
1802
formatter.start_section(_('optional arguments'))
1803
formatter.add_arguments(self._optional_actions_list)
1804
formatter.end_section()
1806
# user-defined groups
1807
for argument_group in self._argument_groups:
1808
formatter.start_section(argument_group.title)
1809
formatter.add_text(argument_group.description)
1810
formatter.add_arguments(argument_group._positional_actions_list)
1811
formatter.add_arguments(argument_group._optional_actions_list)
1812
formatter.end_section()
1815
formatter.add_text(self.epilog)
1817
# determine help from format above
1818
return formatter.format_help()
1820
def format_version(self):
1821
formatter = self._get_formatter()
1822
formatter.add_text(self.version)
1823
return formatter.format_help()
1825
def _get_formatter(self):
1826
return self.formatter_class(prog=self.prog)
1828
# =====================
1829
# Help-printing methods
1830
# =====================
1832
def print_usage(self, file=None):
1833
self._print_message(self.format_usage(), file)
1835
def print_help(self, file=None):
1836
self._print_message(self.format_help(), file)
1838
def print_version(self, file=None):
1839
self._print_message(self.format_version(), file)
1841
def _print_message(self, message, file=None):
1852
def exit(self, status=0, message=None):
1854
_sys.stderr.write(message)
1857
def error(self, message):
1858
"""error(message: string)
1860
Prints a usage message incorporating the message to stderr and
1863
If you override this in a subclass, it should not return -- it
1864
should either exit or raise an exception.
1866
self.print_usage(_sys.stderr)
1867
self.exit(2, _('%s: error: %s\n') % (self.prog, message))