Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(29682)

Side by Side Diff: Lib/argparse.py

Issue 9253: argparse: optional subparsers
Patch Set: Created 6 years, 5 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « Doc/library/argparse.rst ('k') | Lib/test/test_argparse.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 # Author: Steven J. Bethard <steven.bethard@gmail.com>. 1 # Author: Steven J. Bethard <steven.bethard@gmail.com>.
2 2
3 """Command-line parsing library 3 """Command-line parsing library
4 4
5 This module is an optparse-inspired command-line parsing library that: 5 This module is an optparse-inspired command-line parsing library that:
6 6
7 - handles both optional and positional arguments 7 - handles both optional and positional arguments
8 - produces highly informative usage messages 8 - produces highly informative usage messages
9 - supports parsers that dispatch to sub-parsers 9 - supports parsers that dispatch to sub-parsers
10 10
(...skipping 678 matching lines...) Expand 10 before | Expand all | Expand 10 after
689 689
690 def _get_action_name(argument): 690 def _get_action_name(argument):
691 if argument is None: 691 if argument is None:
692 return None 692 return None
693 elif argument.option_strings: 693 elif argument.option_strings:
694 return '/'.join(argument.option_strings) 694 return '/'.join(argument.option_strings)
695 elif argument.metavar not in (None, SUPPRESS): 695 elif argument.metavar not in (None, SUPPRESS):
696 return argument.metavar 696 return argument.metavar
697 elif argument.dest not in (None, SUPPRESS): 697 elif argument.dest not in (None, SUPPRESS):
698 return argument.dest 698 return argument.dest
699 elif hasattr(argument, 'name'):
700 return argument.name()
699 else: 701 else:
700 return None 702 return None
701 703
702 704
703 class ArgumentError(Exception): 705 class ArgumentError(Exception):
704 """An error from creating or using an argument (optional or positional). 706 """An error from creating or using an argument (optional or positional).
705 707
706 The string value of this exception is the message, augmented with 708 The string value of this exception is the message, augmented with
707 information about the argument that caused it. 709 information about the argument that caused it.
708 """ 710 """
(...skipping 341 matching lines...) Expand 10 before | Expand all | Expand 10 after
1050 metavar += ' (%s)' % ', '.join(aliases) 1052 metavar += ' (%s)' % ', '.join(aliases)
1051 sup = super(_SubParsersAction._ChoicesPseudoAction, self) 1053 sup = super(_SubParsersAction._ChoicesPseudoAction, self)
1052 sup.__init__(option_strings=[], dest=dest, help=help, 1054 sup.__init__(option_strings=[], dest=dest, help=help,
1053 metavar=metavar) 1055 metavar=metavar)
1054 1056
1055 def __init__(self, 1057 def __init__(self,
1056 option_strings, 1058 option_strings,
1057 prog, 1059 prog,
1058 parser_class, 1060 parser_class,
1059 dest=SUPPRESS, 1061 dest=SUPPRESS,
1062 required=True,
1060 help=None, 1063 help=None,
1061 metavar=None): 1064 metavar=None):
1062 1065
1063 self._prog_prefix = prog 1066 self._prog_prefix = prog
1064 self._parser_class = parser_class 1067 self._parser_class = parser_class
1065 self._name_parser_map = _collections.OrderedDict() 1068 self._name_parser_map = _collections.OrderedDict()
1066 self._choices_actions = [] 1069 self._choices_actions = []
1067 1070
1068 super(_SubParsersAction, self).__init__( 1071 super(_SubParsersAction, self).__init__(
1069 option_strings=option_strings, 1072 option_strings=option_strings,
1070 dest=dest, 1073 dest=dest,
1071 nargs=PARSER, 1074 nargs=PARSER,
1072 choices=self._name_parser_map, 1075 choices=self._name_parser_map,
1076 required=required,
1073 help=help, 1077 help=help,
1074 metavar=metavar) 1078 metavar=metavar)
1075 1079
1076 def add_parser(self, name, **kwargs): 1080 def add_parser(self, name, **kwargs):
1077 # set prog from the existing prefix 1081 # set prog from the existing prefix
1078 if kwargs.get('prog') is None: 1082 if kwargs.get('prog') is None:
1079 kwargs['prog'] = '%s %s' % (self._prog_prefix, name) 1083 kwargs['prog'] = '%s %s' % (self._prog_prefix, name)
1080 1084
1081 aliases = kwargs.pop('aliases', ()) 1085 aliases = kwargs.pop('aliases', ())
1082 1086
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
1117 raise ArgumentError(self, msg) 1121 raise ArgumentError(self, msg)
1118 1122
1119 # parse all the remaining options into the namespace 1123 # parse all the remaining options into the namespace
1120 # store any unrecognized options on the object, so that the top 1124 # store any unrecognized options on the object, so that the top
1121 # level parser can decide what to do with them 1125 # level parser can decide what to do with them
1122 namespace, arg_strings = parser.parse_known_args(arg_strings, namespace) 1126 namespace, arg_strings = parser.parse_known_args(arg_strings, namespace)
1123 if arg_strings: 1127 if arg_strings:
1124 vars(namespace).setdefault(_UNRECOGNIZED_ARGS_ATTR, []) 1128 vars(namespace).setdefault(_UNRECOGNIZED_ARGS_ATTR, [])
1125 getattr(namespace, _UNRECOGNIZED_ARGS_ATTR).extend(arg_strings) 1129 getattr(namespace, _UNRECOGNIZED_ARGS_ATTR).extend(arg_strings)
1126 1130
1131 def name(self):
1132 # custom name for _get_action_name()
1133 return "{%s}"%','.join(self._name_parser_map)
1127 1134
1128 # ============== 1135 # ==============
1129 # Type classes 1136 # Type classes
1130 # ============== 1137 # ==============
1131 1138
1132 class FileType(object): 1139 class FileType(object):
1133 """Factory for creating file object types 1140 """Factory for creating file object types
1134 1141
1135 Instances of FileType are typically passed as type= arguments to the 1142 Instances of FileType are typically passed as type= arguments to the
1136 ArgumentParser add_argument() method. 1143 ArgumentParser add_argument() method.
(...skipping 833 matching lines...) Expand 10 before | Expand all | Expand 10 after
1970 # twice (which may fail) if the argument was given, but 1977 # twice (which may fail) if the argument was given, but
1971 # only if it was defined already in the namespace 1978 # only if it was defined already in the namespace
1972 if (action.default is not None and 1979 if (action.default is not None and
1973 isinstance(action.default, str) and 1980 isinstance(action.default, str) and
1974 hasattr(namespace, action.dest) and 1981 hasattr(namespace, action.dest) and
1975 action.default is getattr(namespace, action.dest)): 1982 action.default is getattr(namespace, action.dest)):
1976 setattr(namespace, action.dest, 1983 setattr(namespace, action.dest,
1977 self._get_value(action, action.default)) 1984 self._get_value(action, action.default))
1978 1985
1979 if required_actions: 1986 if required_actions:
1987 required_actions = ['%s'%name for name in required_actions]
1980 self.error(_('the following arguments are required: %s') % 1988 self.error(_('the following arguments are required: %s') %
1981 ', '.join(required_actions)) 1989 ', '.join(required_actions))
1982 1990
1983 # make sure all required groups had one option present 1991 # make sure all required groups had one option present
1984 for group in self._mutually_exclusive_groups: 1992 for group in self._mutually_exclusive_groups:
1985 if group.required: 1993 if group.required:
1986 for action in group._group_actions: 1994 for action in group._group_actions:
1987 if action in seen_non_default_actions: 1995 if action in seen_non_default_actions:
1988 break 1996 break
1989 1997
1990 # if no actions were used, report the error 1998 # if no actions were used, report the error
1991 else: 1999 else:
1992 names = [_get_action_name(action) 2000 names = [_get_action_name(action)
1993 for action in group._group_actions 2001 for action in group._group_actions
1994 if action.help is not SUPPRESS] 2002 if action.help is not SUPPRESS]
2003 names = ['%s'%name for name in names]
1995 msg = _('one of the arguments %s is required') 2004 msg = _('one of the arguments %s is required')
1996 self.error(msg % ' '.join(names)) 2005 self.error(msg % ' '.join(names))
1997 2006
1998 # return the updated namespace and the extra arguments 2007 # return the updated namespace and the extra arguments
1999 return namespace, extras 2008 return namespace, extras
2000 2009
2001 def _read_args_from_files(self, arg_strings): 2010 def _read_args_from_files(self, arg_strings):
2002 # expand arguments referencing files 2011 # expand arguments referencing files
2003 new_arg_strings = [] 2012 new_arg_strings = []
2004 for arg_string in arg_strings: 2013 for arg_string in arg_strings:
(...skipping 358 matching lines...) Expand 10 before | Expand all | Expand 10 after
2363 2372
2364 Prints a usage message incorporating the message to stderr and 2373 Prints a usage message incorporating the message to stderr and
2365 exits. 2374 exits.
2366 2375
2367 If you override this in a subclass, it should not return -- it 2376 If you override this in a subclass, it should not return -- it
2368 should either exit or raise an exception. 2377 should either exit or raise an exception.
2369 """ 2378 """
2370 self.print_usage(_sys.stderr) 2379 self.print_usage(_sys.stderr)
2371 args = {'prog': self.prog, 'message': message} 2380 args = {'prog': self.prog, 'message': message}
2372 self.exit(2, _('%(prog)s: error: %(message)s\n') % args) 2381 self.exit(2, _('%(prog)s: error: %(message)s\n') % args)
OLDNEW
« no previous file with comments | « Doc/library/argparse.rst ('k') | Lib/test/test_argparse.py » ('j') | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+