Object for parsing command line strings into Python objects. Keyword Arguments: - prog -- The name of the program (default: sys.argv[0]) - usage -- A usage message (default: auto-generated from arguments) - description -- A description of what the program does - epilog -- Text following the argument descriptions - parents -- Parsers whose arguments should be copied into this one - formatter_class -- HelpFormatter class for printing help messages - prefix_chars -- Characters that prefix optional arguments - fromfile_prefix_chars -- Characters that prefix files containing additional arguments - argument_default -- The default value for all arguments - conflict_handler -- String indicating how to handle conflicts - add_help -- Add a -h/-help option
Object for parsing command line strings into Python objects. Keyword Arguments: - prog -- The name of the program (default: sys.argv[0]) - usage -- A usage message (default: auto-generated from arguments) - description -- A description of what the program does - epilog -- Text following the argument descriptions - version -- Add a -v/--version option with the given version string - parents -- Parsers whose arguments should be copied into this one - formatter_class -- HelpFormatter class for printing help messages - prefix_chars -- Characters that prefix optional arguments - fromfile_prefix_chars -- Characters that prefix files containing additional arguments - argument_default -- The default value for all arguments - conflict_handler -- String indicating how to handle conflicts - add_help -- Add a -h/-help option
Definition at line 1515 of file argparse.py.
def argparse::ArgumentParser::__init__ | ( | self, | |
prog = None , |
|||
usage = None , |
|||
description = None , |
|||
epilog = None , |
|||
version = None , |
|||
parents = [] , |
|||
formatter_class = HelpFormatter , |
|||
prefix_chars = '-' , |
|||
fromfile_prefix_chars = None , |
|||
argument_default = None , |
|||
conflict_handler = 'error' , |
|||
add_help = True |
|||
) |
Definition at line 1533 of file argparse.py.
01546 : 01547 01548 if version is not None: 01549 import warnings 01550 warnings.warn( 01551 """The "version" argument to ArgumentParser is deprecated. """ 01552 """Please use """ 01553 """"add_argument(..., action='version', version="N", ...)" """ 01554 """instead""", DeprecationWarning) 01555 01556 superinit = super(ArgumentParser, self).__init__ 01557 superinit(description=description, 01558 prefix_chars=prefix_chars, 01559 argument_default=argument_default, 01560 conflict_handler=conflict_handler) 01561 01562 # default setting for prog 01563 if prog is None: 01564 prog = _os.path.basename(_sys.argv[0]) 01565 01566 self.prog = prog 01567 self.usage = usage 01568 self.epilog = epilog 01569 self.version = version 01570 self.formatter_class = formatter_class 01571 self.fromfile_prefix_chars = fromfile_prefix_chars 01572 self.add_help = add_help 01573 01574 add_group = self.add_argument_group 01575 self._positionals = add_group(_('positional arguments')) 01576 self._optionals = add_group(_('optional arguments')) 01577 self._subparsers = None 01578 01579 # register types 01580 def identity(string): 01581 return string 01582 self.register('type', None, identity) 01583 01584 # add help and version arguments if necessary 01585 # (using explicit default to override global argument_default) 01586 default_prefix = '-' if '-' in prefix_chars else prefix_chars[0] 01587 if self.add_help: 01588 self.add_argument( 01589 default_prefix+'h', default_prefix*2+'help', 01590 action='help', default=SUPPRESS, 01591 help=_('show this help message and exit')) 01592 if self.version: 01593 self.add_argument( 01594 default_prefix+'v', default_prefix*2+'version', 01595 action='version', default=SUPPRESS, 01596 version=self.version, 01597 help=_("show program's version number and exit")) 01598 01599 # add parent arguments and defaults 01600 for parent in parents: 01601 self._add_container_actions(parent) 01602 try: 01603 defaults = parent._defaults 01604 except AttributeError: 01605 pass 01606 else: 01607 self._defaults.update(defaults)
def argparse::ArgumentParser::__init__ | ( | self, | |
prog = None , |
|||
usage = None , |
|||
description = None , |
|||
epilog = None , |
|||
version = None , |
|||
parents = [] , |
|||
formatter_class = HelpFormatter , |
|||
prefix_chars = '-' , |
|||
fromfile_prefix_chars = None , |
|||
argument_default = None , |
|||
conflict_handler = 'error' , |
|||
add_help = True |
|||
) |
Definition at line 1522 of file argparse.py.
01534 : 01535 01536 superinit = super(ArgumentParser, self).__init__ 01537 superinit(description=description, 01538 prefix_chars=prefix_chars, 01539 argument_default=argument_default, 01540 conflict_handler=conflict_handler) 01541 01542 # default setting for prog 01543 if prog is None: 01544 prog = _os.path.basename(_sys.argv[0]) 01545 01546 self.prog = prog 01547 self.usage = usage 01548 self.epilog = epilog 01549 self.version = version 01550 self.formatter_class = formatter_class 01551 self.fromfile_prefix_chars = fromfile_prefix_chars 01552 self.add_help = add_help 01553 01554 add_group = self.add_argument_group 01555 self._positionals = add_group(_('positional arguments')) 01556 self._optionals = add_group(_('optional arguments')) 01557 self._subparsers = None 01558 01559 # register types 01560 def identity(string): 01561 return string 01562 self.register('type', None, identity) 01563 01564 # add help and version arguments if necessary 01565 # (using explicit default to override global argument_default) 01566 if self.add_help: 01567 self.add_argument( 01568 '-h', '--help', action='help', default=SUPPRESS, 01569 help=_('show this help message and exit')) 01570 if self.version: 01571 self.add_argument( 01572 '-v', '--version', action='version', default=SUPPRESS, 01573 help=_("show program's version number and exit")) 01574 01575 # add parent arguments and defaults 01576 for parent in parents: 01577 self._add_container_actions(parent) 01578 try: 01579 defaults = parent._defaults 01580 except AttributeError: 01581 pass 01582 else: 01583 self._defaults.update(defaults) 01584
def argparse::ArgumentParser::_add_action | ( | self, | |
action | |||
) | [private] |
Reimplemented from argparse::_ActionsContainer.
Definition at line 1657 of file argparse.py.
def argparse::ArgumentParser::_add_action | ( | self, | |
action | |||
) | [private] |
Reimplemented from argparse::_ActionsContainer.
Definition at line 1634 of file argparse.py.
01634 : 01635 if action.option_strings: 01636 self._optionals._add_action(action) 01637 else: 01638 self._positionals._add_action(action) 01639 return action 01640
def argparse::ArgumentParser::_check_value | ( | self, | |
action, | |||
value | |||
) | [private] |
Definition at line 2240 of file argparse.py.
def argparse::ArgumentParser::_check_value | ( | self, | |
action, | |||
value | |||
) | [private] |
Definition at line 2187 of file argparse.py.
02187 : 02188 # converted value must be one of the choices (if specified) 02189 if action.choices is not None and value not in action.choices: 02190 tup = value, ', '.join(map(repr, action.choices)) 02191 msg = _('invalid choice: %r (choose from %s)') % tup 02192 raise ArgumentError(action, msg) 02193
def argparse::ArgumentParser::_get_formatter | ( | self | ) | [private] |
Definition at line 2289 of file argparse.py.
def argparse::ArgumentParser::_get_formatter | ( | self | ) | [private] |
Definition at line 2231 of file argparse.py.
02231 : 02232 return self.formatter_class(prog=self.prog) 02233
def argparse::ArgumentParser::_get_kwargs | ( | self | ) | [private] |
Reimplemented from argparse::_AttributeHolder.
Definition at line 1611 of file argparse.py.
def argparse::ArgumentParser::_get_kwargs | ( | self | ) | [private] |
Reimplemented from argparse::_AttributeHolder.
Definition at line 1588 of file argparse.py.
01588 : 01589 names = [ 01590 'prog', 01591 'usage', 01592 'description', 01593 'version', 01594 'formatter_class', 01595 'conflict_handler', 01596 'add_help', 01597 ] 01598 return [(name, getattr(self, name)) for name in names] 01599
def argparse::ArgumentParser::_get_nargs_pattern | ( | self, | |
action | |||
) | [private] |
Definition at line 2082 of file argparse.py.
02082 : 02083 # in all examples below, we have to allow for '--' args 02084 # which are represented as '-' in the pattern 02085 nargs = action.nargs 02086 02087 # the default (None) is assumed to be a single argument 02088 if nargs is None: 02089 nargs_pattern = '(-*A-*)' 02090 02091 # allow zero or one arguments 02092 elif nargs == OPTIONAL: 02093 nargs_pattern = '(-*A?-*)' 02094 02095 # allow zero or more arguments 02096 elif nargs == ZERO_OR_MORE: 02097 nargs_pattern = '(-*[A-]*)' 02098 02099 # allow one or more arguments 02100 elif nargs == ONE_OR_MORE: 02101 nargs_pattern = '(-*A[A-]*)' 02102 02103 # allow one argument followed by any number of options or arguments 02104 elif nargs is PARSER: 02105 nargs_pattern = '(-*A[-AO]*)' 02106 02107 # all others should be integers 02108 else: 02109 nargs_pattern = '(-*%s-*)' % '-*'.join('A' * nargs) 02110 02111 # if this is an optional action, -- is not allowed 02112 if action.option_strings: 02113 nargs_pattern = nargs_pattern.replace('-*', '') 02114 nargs_pattern = nargs_pattern.replace('-', '') 02115 02116 # return the pattern 02117 return nargs_pattern 02118
def argparse::ArgumentParser::_get_nargs_pattern | ( | self, | |
action | |||
) | [private] |
Definition at line 2122 of file argparse.py.
02123 : 02124 # in all examples below, we have to allow for '--' args 02125 # which are represented as '-' in the pattern 02126 nargs = action.nargs 02127 02128 # the default (None) is assumed to be a single argument 02129 if nargs is None: 02130 nargs_pattern = '(-*A-*)' 02131 02132 # allow zero or one arguments 02133 elif nargs == OPTIONAL: 02134 nargs_pattern = '(-*A?-*)' 02135 02136 # allow zero or more arguments 02137 elif nargs == ZERO_OR_MORE: 02138 nargs_pattern = '(-*[A-]*)' 02139 02140 # allow one or more arguments 02141 elif nargs == ONE_OR_MORE: 02142 nargs_pattern = '(-*A[A-]*)' 02143 02144 # allow any number of options or arguments 02145 elif nargs == REMAINDER: 02146 nargs_pattern = '([-AO]*)' 02147 02148 # allow one argument followed by any number of options or arguments 02149 elif nargs == PARSER: 02150 nargs_pattern = '(-*A[-AO]*)' 02151 02152 # all others should be integers 02153 else: 02154 nargs_pattern = '(-*%s-*)' % '-*'.join('A' * nargs) 02155 02156 # if this is an optional action, -- is not allowed 02157 if action.option_strings: 02158 nargs_pattern = nargs_pattern.replace('-*', '') 02159 nargs_pattern = nargs_pattern.replace('-', '') 02160 02161 # return the pattern 02162 return nargs_pattern
def argparse::ArgumentParser::_get_option_tuples | ( | self, | |
option_string | |||
) | [private] |
Definition at line 2038 of file argparse.py.
02038 : 02039 result = [] 02040 02041 # option strings starting with two prefix characters are only 02042 # split at the '=' 02043 chars = self.prefix_chars 02044 if option_string[0] in chars and option_string[1] in chars: 02045 if '=' in option_string: 02046 option_prefix, explicit_arg = option_string.split('=', 1) 02047 else: 02048 option_prefix = option_string 02049 explicit_arg = None 02050 for option_string in self._option_string_actions: 02051 if option_string.startswith(option_prefix): 02052 action = self._option_string_actions[option_string] 02053 tup = action, option_string, explicit_arg 02054 result.append(tup) 02055 02056 # single character options can be concatenated with their arguments 02057 # but multiple character options always have to have their argument 02058 # separate 02059 elif option_string[0] in chars and option_string[1] not in chars: 02060 option_prefix = option_string 02061 explicit_arg = None 02062 short_option_prefix = option_string[:2] 02063 short_explicit_arg = option_string[2:] 02064 02065 for option_string in self._option_string_actions: 02066 if option_string == short_option_prefix: 02067 action = self._option_string_actions[option_string] 02068 tup = action, option_string, short_explicit_arg 02069 result.append(tup) 02070 elif option_string.startswith(option_prefix): 02071 action = self._option_string_actions[option_string] 02072 tup = action, option_string, explicit_arg 02073 result.append(tup) 02074 02075 # shouldn't ever get here 02076 else: 02077 self.error(_('unexpected option string: %s') % option_string) 02078 02079 # return the collected option tuples 02080 return result 02081
def argparse::ArgumentParser::_get_option_tuples | ( | self, | |
option_string | |||
) | [private] |
Definition at line 2078 of file argparse.py.
02079 : 02080 result = [] 02081 02082 # option strings starting with two prefix characters are only 02083 # split at the '=' 02084 chars = self.prefix_chars 02085 if option_string[0] in chars and option_string[1] in chars: 02086 if '=' in option_string: 02087 option_prefix, explicit_arg = option_string.split('=', 1) 02088 else: 02089 option_prefix = option_string 02090 explicit_arg = None 02091 for option_string in self._option_string_actions: 02092 if option_string.startswith(option_prefix): 02093 action = self._option_string_actions[option_string] 02094 tup = action, option_string, explicit_arg 02095 result.append(tup) 02096 02097 # single character options can be concatenated with their arguments 02098 # but multiple character options always have to have their argument 02099 # separate 02100 elif option_string[0] in chars and option_string[1] not in chars: 02101 option_prefix = option_string 02102 explicit_arg = None 02103 short_option_prefix = option_string[:2] 02104 short_explicit_arg = option_string[2:] 02105 02106 for option_string in self._option_string_actions: 02107 if option_string == short_option_prefix: 02108 action = self._option_string_actions[option_string] 02109 tup = action, option_string, short_explicit_arg 02110 result.append(tup) 02111 elif option_string.startswith(option_prefix): 02112 action = self._option_string_actions[option_string] 02113 tup = action, option_string, explicit_arg 02114 result.append(tup) 02115 02116 # shouldn't ever get here 02117 else: 02118 self.error(_('unexpected option string: %s') % option_string) 02119 02120 # return the collected option tuples 02121 return result
def argparse::ArgumentParser::_get_optional_actions | ( | self | ) | [private] |
Definition at line 1664 of file argparse.py.
def argparse::ArgumentParser::_get_optional_actions | ( | self | ) | [private] |
Definition at line 1641 of file argparse.py.
01641 : 01642 return [action 01643 for action in self._actions 01644 if action.option_strings] 01645
def argparse::ArgumentParser::_get_positional_actions | ( | self | ) | [private] |
Definition at line 1646 of file argparse.py.
01646 : 01647 return [action 01648 for action in self._actions 01649 if not action.option_strings] 01650
def argparse::ArgumentParser::_get_positional_actions | ( | self | ) | [private] |
Definition at line 1669 of file argparse.py.
def argparse::ArgumentParser::_get_value | ( | self, | |
action, | |||
arg_string | |||
) | [private] |
Definition at line 2215 of file argparse.py.
02216 : 02217 type_func = self._registry_get('type', action.type, action.type) 02218 if not _callable(type_func): 02219 msg = _('%r is not callable') 02220 raise ArgumentError(action, msg % type_func) 02221 02222 # convert the value to the appropriate type 02223 try: 02224 result = type_func(arg_string) 02225 02226 # ArgumentTypeErrors indicate errors 02227 except ArgumentTypeError: 02228 name = getattr(action.type, '__name__', repr(action.type)) 02229 msg = str(_sys.exc_info()[1]) 02230 raise ArgumentError(action, msg) 02231 02232 # TypeErrors or ValueErrors also indicate errors 02233 except (TypeError, ValueError): 02234 name = getattr(action.type, '__name__', repr(action.type)) 02235 msg = _('invalid %s value: %r') 02236 raise ArgumentError(action, msg % (name, arg_string)) 02237 02238 # return the converted value 02239 return result
def argparse::ArgumentParser::_get_value | ( | self, | |
action, | |||
arg_string | |||
) | [private] |
Definition at line 2167 of file argparse.py.
02167 : 02168 type_func = self._registry_get('type', action.type, action.type) 02169 if not hasattr(type_func, '__call__'): 02170 if not hasattr(type_func, '__bases__'): # classic classes 02171 msg = _('%r is not callable') 02172 raise ArgumentError(action, msg % type_func) 02173 02174 # convert the value to the appropriate type 02175 try: 02176 result = type_func(arg_string) 02177 02178 # TypeErrors or ValueErrors indicate errors 02179 except (TypeError, ValueError): 02180 name = getattr(action.type, '__name__', repr(action.type)) 02181 msg = _('invalid %s value: %r') 02182 raise ArgumentError(action, msg % (name, arg_string)) 02183 02184 # return the converted value 02185 return result 02186
def argparse::ArgumentParser::_get_values | ( | self, | |
action, | |||
arg_strings | |||
) | [private] |
Definition at line 2166 of file argparse.py.
02167 : 02168 # for everything but PARSER args, strip out '--' 02169 if action.nargs not in [PARSER, REMAINDER]: 02170 arg_strings = [s for s in arg_strings if s != '--'] 02171 02172 # optional argument produces a default when not present 02173 if not arg_strings and action.nargs == OPTIONAL: 02174 if action.option_strings: 02175 value = action.const 02176 else: 02177 value = action.default 02178 if isinstance(value, basestring): 02179 value = self._get_value(action, value) 02180 self._check_value(action, value) 02181 02182 # when nargs='*' on a positional, if there were no command-line 02183 # args, use the default if it is anything other than None 02184 elif (not arg_strings and action.nargs == ZERO_OR_MORE and 02185 not action.option_strings): 02186 if action.default is not None: 02187 value = action.default 02188 else: 02189 value = arg_strings 02190 self._check_value(action, value) 02191 02192 # single argument or optional argument produces a single value 02193 elif len(arg_strings) == 1 and action.nargs in [None, OPTIONAL]: 02194 arg_string, = arg_strings 02195 value = self._get_value(action, arg_string) 02196 self._check_value(action, value) 02197 02198 # REMAINDER arguments convert all values, checking none 02199 elif action.nargs == REMAINDER: 02200 value = [self._get_value(action, v) for v in arg_strings] 02201 02202 # PARSER arguments convert all values, but check only the first 02203 elif action.nargs == PARSER: 02204 value = [self._get_value(action, v) for v in arg_strings] 02205 self._check_value(action, value[0]) 02206 02207 # all other types of nargs produce a list 02208 else: 02209 value = [self._get_value(action, v) for v in arg_strings] 02210 for v in value: 02211 self._check_value(action, v) 02212 02213 # return the converted value 02214 return value
def argparse::ArgumentParser::_get_values | ( | self, | |
action, | |||
arg_strings | |||
) | [private] |
Definition at line 2122 of file argparse.py.
02122 : 02123 # for everything but PARSER args, strip out '--' 02124 if action.nargs is not PARSER: 02125 arg_strings = [s for s in arg_strings if s != '--'] 02126 02127 # optional argument produces a default when not present 02128 if not arg_strings and action.nargs == OPTIONAL: 02129 if action.option_strings: 02130 value = action.const 02131 else: 02132 value = action.default 02133 if isinstance(value, _basestring): 02134 value = self._get_value(action, value) 02135 self._check_value(action, value) 02136 02137 # when nargs='*' on a positional, if there were no command-line 02138 # args, use the default if it is anything other than None 02139 elif (not arg_strings and action.nargs == ZERO_OR_MORE and 02140 not action.option_strings): 02141 if action.default is not None: 02142 value = action.default 02143 else: 02144 value = arg_strings 02145 self._check_value(action, value) 02146 02147 # single argument or optional argument produces a single value 02148 elif len(arg_strings) == 1 and action.nargs in [None, OPTIONAL]: 02149 arg_string, = arg_strings 02150 value = self._get_value(action, arg_string) 02151 self._check_value(action, value) 02152 02153 # PARSER arguments convert all values, but check only the first 02154 elif action.nargs is PARSER: 02155 value = [self._get_value(action, v) for v in arg_strings] 02156 self._check_value(action, value[0]) 02157 02158 # all other types of nargs produce a list 02159 else: 02160 value = [self._get_value(action, v) for v in arg_strings] 02161 for v in value: 02162 self._check_value(action, v) 02163 02164 # return the converted value 02165 return value 02166
def argparse::ArgumentParser::_match_argument | ( | self, | |
action, | |||
arg_strings_pattern | |||
) | [private] |
Definition at line 1953 of file argparse.py.
01953 : 01954 # match the pattern for this action to the arg strings 01955 nargs_pattern = self._get_nargs_pattern(action) 01956 match = _re.match(nargs_pattern, arg_strings_pattern) 01957 01958 # raise an exception if we weren't able to find a match 01959 if match is None: 01960 nargs_errors = { 01961 None: _('expected one argument'), 01962 OPTIONAL: _('expected at most one argument'), 01963 ONE_OR_MORE: _('expected at least one argument'), 01964 } 01965 default = _('expected %s argument(s)') % action.nargs 01966 msg = nargs_errors.get(action.nargs, default) 01967 raise ArgumentError(action, msg) 01968 01969 # return the number of arguments matched 01970 return len(match.group(1)) 01971
def argparse::ArgumentParser::_match_argument | ( | self, | |
action, | |||
arg_strings_pattern | |||
) | [private] |
Definition at line 1986 of file argparse.py.
01987 : 01988 # match the pattern for this action to the arg strings 01989 nargs_pattern = self._get_nargs_pattern(action) 01990 match = _re.match(nargs_pattern, arg_strings_pattern) 01991 01992 # raise an exception if we weren't able to find a match 01993 if match is None: 01994 nargs_errors = { 01995 None: _('expected one argument'), 01996 OPTIONAL: _('expected at most one argument'), 01997 ONE_OR_MORE: _('expected at least one argument'), 01998 } 01999 default = _('expected %s argument(s)') % action.nargs 02000 msg = nargs_errors.get(action.nargs, default) 02001 raise ArgumentError(action, msg) 02002 02003 # return the number of arguments matched 02004 return len(match.group(1))
def argparse::ArgumentParser::_match_arguments_partial | ( | self, | |
actions, | |||
arg_strings_pattern | |||
) | [private] |
Definition at line 1972 of file argparse.py.
01972 : 01973 # progressively shorten the actions list by slicing off the 01974 # final actions until we find a match 01975 result = [] 01976 for i in range(len(actions), 0, -1): 01977 actions_slice = actions[:i] 01978 pattern = ''.join([self._get_nargs_pattern(action) 01979 for action in actions_slice]) 01980 match = _re.match(pattern, arg_strings_pattern) 01981 if match is not None: 01982 result.extend([len(string) for string in match.groups()]) 01983 break 01984 01985 # return the list of arg string counts 01986 return result 01987
def argparse::ArgumentParser::_match_arguments_partial | ( | self, | |
actions, | |||
arg_strings_pattern | |||
) | [private] |
Definition at line 2005 of file argparse.py.
02006 : 02007 # progressively shorten the actions list by slicing off the 02008 # final actions until we find a match 02009 result = [] 02010 for i in range(len(actions), 0, -1): 02011 actions_slice = actions[:i] 02012 pattern = ''.join([self._get_nargs_pattern(action) 02013 for action in actions_slice]) 02014 match = _re.match(pattern, arg_strings_pattern) 02015 if match is not None: 02016 result.extend([len(string) for string in match.groups()]) 02017 break 02018 02019 # return the list of arg string counts 02020 return result
def argparse::ArgumentParser::_parse_known_args | ( | self, | |
arg_strings, | |||
namespace | |||
) | [private] |
Definition at line 1719 of file argparse.py.
01720 : 01721 # replace arg strings that are file references 01722 if self.fromfile_prefix_chars is not None: 01723 arg_strings = self._read_args_from_files(arg_strings) 01724 01725 # map all mutually exclusive arguments to the other arguments 01726 # they can't occur with 01727 action_conflicts = {} 01728 for mutex_group in self._mutually_exclusive_groups: 01729 group_actions = mutex_group._group_actions 01730 for i, mutex_action in enumerate(mutex_group._group_actions): 01731 conflicts = action_conflicts.setdefault(mutex_action, []) 01732 conflicts.extend(group_actions[:i]) 01733 conflicts.extend(group_actions[i + 1:]) 01734 01735 # find all option indices, and determine the arg_string_pattern 01736 # which has an 'O' if there is an option at an index, 01737 # an 'A' if there is an argument, or a '-' if there is a '--' 01738 option_string_indices = {} 01739 arg_string_pattern_parts = [] 01740 arg_strings_iter = iter(arg_strings) 01741 for i, arg_string in enumerate(arg_strings_iter): 01742 01743 # all args after -- are non-options 01744 if arg_string == '--': 01745 arg_string_pattern_parts.append('-') 01746 for arg_string in arg_strings_iter: 01747 arg_string_pattern_parts.append('A') 01748 01749 # otherwise, add the arg to the arg strings 01750 # and note the index if it was an option 01751 else: 01752 option_tuple = self._parse_optional(arg_string) 01753 if option_tuple is None: 01754 pattern = 'A' 01755 else: 01756 option_string_indices[i] = option_tuple 01757 pattern = 'O' 01758 arg_string_pattern_parts.append(pattern) 01759 01760 # join the pieces together to form the pattern 01761 arg_strings_pattern = ''.join(arg_string_pattern_parts) 01762 01763 # converts arg strings to the appropriate and then takes the action 01764 seen_actions = set() 01765 seen_non_default_actions = set() 01766 01767 def take_action(action, argument_strings, option_string=None): 01768 seen_actions.add(action) 01769 argument_values = self._get_values(action, argument_strings) 01770 01771 # error if this argument is not allowed with other previously 01772 # seen arguments, assuming that actions that use the default 01773 # value don't really count as "present" 01774 if argument_values is not action.default: 01775 seen_non_default_actions.add(action) 01776 for conflict_action in action_conflicts.get(action, []): 01777 if conflict_action in seen_non_default_actions: 01778 msg = _('not allowed with argument %s') 01779 action_name = _get_action_name(conflict_action) 01780 raise ArgumentError(action, msg % action_name) 01781 01782 # take the action if we didn't receive a SUPPRESS value 01783 # (e.g. from a default) 01784 if argument_values is not SUPPRESS: 01785 action(self, namespace, argument_values, option_string) 01786 01787 # function to convert arg_strings into an optional action 01788 def consume_optional(start_index): 01789 01790 # get the optional identified at this index 01791 option_tuple = option_string_indices[start_index] 01792 action, option_string, explicit_arg = option_tuple 01793 01794 # identify additional optionals in the same arg string 01795 # (e.g. -xyz is the same as -x -y -z if no args are required) 01796 match_argument = self._match_argument 01797 action_tuples = [] 01798 while True: 01799 01800 # if we found no optional action, skip it 01801 if action is None: 01802 extras.append(arg_strings[start_index]) 01803 return start_index + 1 01804 01805 # if there is an explicit argument, try to match the 01806 # optional's string arguments to only this 01807 if explicit_arg is not None: 01808 arg_count = match_argument(action, 'A') 01809 01810 # if the action is a single-dash option and takes no 01811 # arguments, try to parse more single-dash options out 01812 # of the tail of the option string 01813 chars = self.prefix_chars 01814 if arg_count == 0 and option_string[1] not in chars: 01815 action_tuples.append((action, [], option_string)) 01816 char = option_string[0] 01817 option_string = char + explicit_arg[0] 01818 new_explicit_arg = explicit_arg[1:] or None 01819 optionals_map = self._option_string_actions 01820 if option_string in optionals_map: 01821 action = optionals_map[option_string] 01822 explicit_arg = new_explicit_arg 01823 else: 01824 msg = _('ignored explicit argument %r') 01825 raise ArgumentError(action, msg % explicit_arg) 01826 01827 # if the action expect exactly one argument, we've 01828 # successfully matched the option; exit the loop 01829 elif arg_count == 1: 01830 stop = start_index + 1 01831 args = [explicit_arg] 01832 action_tuples.append((action, args, option_string)) 01833 break 01834 01835 # error if a double-dash option did not use the 01836 # explicit argument 01837 else: 01838 msg = _('ignored explicit argument %r') 01839 raise ArgumentError(action, msg % explicit_arg) 01840 01841 # if there is no explicit argument, try to match the 01842 # optional's string arguments with the following strings 01843 # if successful, exit the loop 01844 else: 01845 start = start_index + 1 01846 selected_patterns = arg_strings_pattern[start:] 01847 arg_count = match_argument(action, selected_patterns) 01848 stop = start + arg_count 01849 args = arg_strings[start:stop] 01850 action_tuples.append((action, args, option_string)) 01851 break 01852 01853 # add the Optional to the list and return the index at which 01854 # the Optional's string args stopped 01855 assert action_tuples 01856 for action, args, option_string in action_tuples: 01857 take_action(action, args, option_string) 01858 return stop 01859 01860 # the list of Positionals left to be parsed; this is modified 01861 # by consume_positionals() 01862 positionals = self._get_positional_actions() 01863 01864 # function to convert arg_strings into positional actions 01865 def consume_positionals(start_index): 01866 # match as many Positionals as possible 01867 match_partial = self._match_arguments_partial 01868 selected_pattern = arg_strings_pattern[start_index:] 01869 arg_counts = match_partial(positionals, selected_pattern) 01870 01871 # slice off the appropriate arg strings for each Positional 01872 # and add the Positional and its args to the list 01873 for action, arg_count in zip(positionals, arg_counts): 01874 args = arg_strings[start_index: start_index + arg_count] 01875 start_index += arg_count 01876 take_action(action, args) 01877 01878 # slice off the Positionals that we just parsed and return the 01879 # index at which the Positionals' string args stopped 01880 positionals[:] = positionals[len(arg_counts):] 01881 return start_index 01882 01883 # consume Positionals and Optionals alternately, until we have 01884 # passed the last option string 01885 extras = [] 01886 start_index = 0 01887 if option_string_indices: 01888 max_option_string_index = max(option_string_indices) 01889 else: 01890 max_option_string_index = -1 01891 while start_index <= max_option_string_index: 01892 01893 # consume any Positionals preceding the next option 01894 next_option_string_index = min([ 01895 index 01896 for index in option_string_indices 01897 if index >= start_index]) 01898 if start_index != next_option_string_index: 01899 positionals_end_index = consume_positionals(start_index) 01900 01901 # only try to parse the next optional if we didn't consume 01902 # the option string during the positionals parsing 01903 if positionals_end_index > start_index: 01904 start_index = positionals_end_index 01905 continue 01906 else: 01907 start_index = positionals_end_index 01908 01909 # if we consumed all the positionals we could and we're not 01910 # at the index of an option string, there were extra arguments 01911 if start_index not in option_string_indices: 01912 strings = arg_strings[start_index:next_option_string_index] 01913 extras.extend(strings) 01914 start_index = next_option_string_index 01915 01916 # consume the next optional and any arguments for it 01917 start_index = consume_optional(start_index) 01918 01919 # consume any positionals following the last Optional 01920 stop_index = consume_positionals(start_index) 01921 01922 # if we didn't consume all the argument strings, there were extras 01923 extras.extend(arg_strings[stop_index:]) 01924 01925 # if we didn't use all the Positional objects, there were too few 01926 # arg strings supplied. 01927 if positionals: 01928 self.error(_('too few arguments')) 01929 01930 # make sure all required actions were present 01931 for action in self._actions: 01932 if action.required: 01933 if action not in seen_actions: 01934 name = _get_action_name(action) 01935 self.error(_('argument %s is required') % name) 01936 01937 # make sure all required groups had one option present 01938 for group in self._mutually_exclusive_groups: 01939 if group.required: 01940 for action in group._group_actions: 01941 if action in seen_non_default_actions: 01942 break 01943 01944 # if no actions were used, report the error 01945 else: 01946 names = [_get_action_name(action) 01947 for action in group._group_actions 01948 if action.help is not SUPPRESS] 01949 msg = _('one of the arguments %s is required') 01950 self.error(msg % ' '.join(names)) 01951 01952 # return the updated namespace and the extra arguments 01953 return namespace, extras
def argparse::ArgumentParser::_parse_known_args | ( | self, | |
arg_strings, | |||
namespace | |||
) | [private] |
Definition at line 1692 of file argparse.py.
01692 : 01693 # replace arg strings that are file references 01694 if self.fromfile_prefix_chars is not None: 01695 arg_strings = self._read_args_from_files(arg_strings) 01696 01697 # map all mutually exclusive arguments to the other arguments 01698 # they can't occur with 01699 action_conflicts = {} 01700 for mutex_group in self._mutually_exclusive_groups: 01701 group_actions = mutex_group._group_actions 01702 for i, mutex_action in enumerate(mutex_group._group_actions): 01703 conflicts = action_conflicts.setdefault(mutex_action, []) 01704 conflicts.extend(group_actions[:i]) 01705 conflicts.extend(group_actions[i + 1:]) 01706 01707 # find all option indices, and determine the arg_string_pattern 01708 # which has an 'O' if there is an option at an index, 01709 # an 'A' if there is an argument, or a '-' if there is a '--' 01710 option_string_indices = {} 01711 arg_string_pattern_parts = [] 01712 arg_strings_iter = iter(arg_strings) 01713 for i, arg_string in enumerate(arg_strings_iter): 01714 01715 # all args after -- are non-options 01716 if arg_string == '--': 01717 arg_string_pattern_parts.append('-') 01718 for arg_string in arg_strings_iter: 01719 arg_string_pattern_parts.append('A') 01720 01721 # otherwise, add the arg to the arg strings 01722 # and note the index if it was an option 01723 else: 01724 option_tuple = self._parse_optional(arg_string) 01725 if option_tuple is None: 01726 pattern = 'A' 01727 else: 01728 option_string_indices[i] = option_tuple 01729 pattern = 'O' 01730 arg_string_pattern_parts.append(pattern) 01731 01732 # join the pieces together to form the pattern 01733 arg_strings_pattern = ''.join(arg_string_pattern_parts) 01734 01735 # converts arg strings to the appropriate and then takes the action 01736 seen_actions = _set() 01737 seen_non_default_actions = _set() 01738 01739 def take_action(action, argument_strings, option_string=None): 01740 seen_actions.add(action) 01741 argument_values = self._get_values(action, argument_strings) 01742 01743 # error if this argument is not allowed with other previously 01744 # seen arguments, assuming that actions that use the default 01745 # value don't really count as "present" 01746 if argument_values is not action.default: 01747 seen_non_default_actions.add(action) 01748 for conflict_action in action_conflicts.get(action, []): 01749 if conflict_action in seen_non_default_actions: 01750 msg = _('not allowed with argument %s') 01751 action_name = _get_action_name(conflict_action) 01752 raise ArgumentError(action, msg % action_name) 01753 01754 # take the action if we didn't receive a SUPPRESS value 01755 # (e.g. from a default) 01756 if argument_values is not SUPPRESS: 01757 action(self, namespace, argument_values, option_string) 01758 01759 # function to convert arg_strings into an optional action 01760 def consume_optional(start_index): 01761 01762 # get the optional identified at this index 01763 option_tuple = option_string_indices[start_index] 01764 action, option_string, explicit_arg = option_tuple 01765 01766 # identify additional optionals in the same arg string 01767 # (e.g. -xyz is the same as -x -y -z if no args are required) 01768 match_argument = self._match_argument 01769 action_tuples = [] 01770 while True: 01771 01772 # if we found no optional action, skip it 01773 if action is None: 01774 extras.append(arg_strings[start_index]) 01775 return start_index + 1 01776 01777 # if there is an explicit argument, try to match the 01778 # optional's string arguments to only this 01779 if explicit_arg is not None: 01780 arg_count = match_argument(action, 'A') 01781 01782 # if the action is a single-dash option and takes no 01783 # arguments, try to parse more single-dash options out 01784 # of the tail of the option string 01785 chars = self.prefix_chars 01786 if arg_count == 0 and option_string[1] not in chars: 01787 action_tuples.append((action, [], option_string)) 01788 for char in self.prefix_chars: 01789 option_string = char + explicit_arg[0] 01790 explicit_arg = explicit_arg[1:] or None 01791 optionals_map = self._option_string_actions 01792 if option_string in optionals_map: 01793 action = optionals_map[option_string] 01794 break 01795 else: 01796 msg = _('ignored explicit argument %r') 01797 raise ArgumentError(action, msg % explicit_arg) 01798 01799 # if the action expect exactly one argument, we've 01800 # successfully matched the option; exit the loop 01801 elif arg_count == 1: 01802 stop = start_index + 1 01803 args = [explicit_arg] 01804 action_tuples.append((action, args, option_string)) 01805 break 01806 01807 # error if a double-dash option did not use the 01808 # explicit argument 01809 else: 01810 msg = _('ignored explicit argument %r') 01811 raise ArgumentError(action, msg % explicit_arg) 01812 01813 # if there is no explicit argument, try to match the 01814 # optional's string arguments with the following strings 01815 # if successful, exit the loop 01816 else: 01817 start = start_index + 1 01818 selected_patterns = arg_strings_pattern[start:] 01819 arg_count = match_argument(action, selected_patterns) 01820 stop = start + arg_count 01821 args = arg_strings[start:stop] 01822 action_tuples.append((action, args, option_string)) 01823 break 01824 01825 # add the Optional to the list and return the index at which 01826 # the Optional's string args stopped 01827 assert action_tuples 01828 for action, args, option_string in action_tuples: 01829 take_action(action, args, option_string) 01830 return stop 01831 01832 # the list of Positionals left to be parsed; this is modified 01833 # by consume_positionals() 01834 positionals = self._get_positional_actions() 01835 01836 # function to convert arg_strings into positional actions 01837 def consume_positionals(start_index): 01838 # match as many Positionals as possible 01839 match_partial = self._match_arguments_partial 01840 selected_pattern = arg_strings_pattern[start_index:] 01841 arg_counts = match_partial(positionals, selected_pattern) 01842 01843 # slice off the appropriate arg strings for each Positional 01844 # and add the Positional and its args to the list 01845 for action, arg_count in zip(positionals, arg_counts): 01846 args = arg_strings[start_index: start_index + arg_count] 01847 start_index += arg_count 01848 take_action(action, args) 01849 01850 # slice off the Positionals that we just parsed and return the 01851 # index at which the Positionals' string args stopped 01852 positionals[:] = positionals[len(arg_counts):] 01853 return start_index 01854 01855 # consume Positionals and Optionals alternately, until we have 01856 # passed the last option string 01857 extras = [] 01858 start_index = 0 01859 if option_string_indices: 01860 max_option_string_index = max(option_string_indices) 01861 else: 01862 max_option_string_index = -1 01863 while start_index <= max_option_string_index: 01864 01865 # consume any Positionals preceding the next option 01866 next_option_string_index = min([ 01867 index 01868 for index in option_string_indices 01869 if index >= start_index]) 01870 if start_index != next_option_string_index: 01871 positionals_end_index = consume_positionals(start_index) 01872 01873 # only try to parse the next optional if we didn't consume 01874 # the option string during the positionals parsing 01875 if positionals_end_index > start_index: 01876 start_index = positionals_end_index 01877 continue 01878 else: 01879 start_index = positionals_end_index 01880 01881 # if we consumed all the positionals we could and we're not 01882 # at the index of an option string, there were extra arguments 01883 if start_index not in option_string_indices: 01884 strings = arg_strings[start_index:next_option_string_index] 01885 extras.extend(strings) 01886 start_index = next_option_string_index 01887 01888 # consume the next optional and any arguments for it 01889 start_index = consume_optional(start_index) 01890 01891 # consume any positionals following the last Optional 01892 stop_index = consume_positionals(start_index) 01893 01894 # if we didn't consume all the argument strings, there were extras 01895 extras.extend(arg_strings[stop_index:]) 01896 01897 # if we didn't use all the Positional objects, there were too few 01898 # arg strings supplied. 01899 if positionals: 01900 self.error(_('too few arguments')) 01901 01902 # make sure all required actions were present 01903 for action in self._actions: 01904 if action.required: 01905 if action not in seen_actions: 01906 name = _get_action_name(action) 01907 self.error(_('argument %s is required') % name) 01908 01909 # make sure all required groups had one option present 01910 for group in self._mutually_exclusive_groups: 01911 if group.required: 01912 for action in group._group_actions: 01913 if action in seen_non_default_actions: 01914 break 01915 01916 # if no actions were used, report the error 01917 else: 01918 names = [_get_action_name(action) 01919 for action in group._group_actions 01920 if action.help is not SUPPRESS] 01921 msg = _('one of the arguments %s is required') 01922 self.error(msg % ' '.join(names)) 01923 01924 # return the updated namespace and the extra arguments 01925 return namespace, extras 01926
def argparse::ArgumentParser::_parse_optional | ( | self, | |
arg_string | |||
) | [private] |
Definition at line 1988 of file argparse.py.
01988 : 01989 # if it's an empty string, it was meant to be a positional 01990 if not arg_string: 01991 return None 01992 01993 # if it doesn't start with a prefix, it was meant to be positional 01994 if not arg_string[0] in self.prefix_chars: 01995 return None 01996 01997 # if it's just dashes, it was meant to be positional 01998 if not arg_string.strip('-'): 01999 return None 02000 02001 # if the option string is present in the parser, return the action 02002 if arg_string in self._option_string_actions: 02003 action = self._option_string_actions[arg_string] 02004 return action, arg_string, None 02005 02006 # search through all possible prefixes of the option string 02007 # and all actions in the parser for possible interpretations 02008 option_tuples = self._get_option_tuples(arg_string) 02009 02010 # if multiple actions match, the option string was ambiguous 02011 if len(option_tuples) > 1: 02012 options = ', '.join([option_string 02013 for action, option_string, explicit_arg in option_tuples]) 02014 tup = arg_string, options 02015 self.error(_('ambiguous option: %s could match %s') % tup) 02016 02017 # if exactly one action matched, this segmentation is good, 02018 # so return the parsed action 02019 elif len(option_tuples) == 1: 02020 option_tuple, = option_tuples 02021 return option_tuple 02022 02023 # if it was not found as an option, but it looks like a negative 02024 # number, it was meant to be positional 02025 # unless there are negative-number-like options 02026 if self._negative_number_matcher.match(arg_string): 02027 if not self._has_negative_number_optionals: 02028 return None 02029 02030 # if it contains a space, it was meant to be a positional 02031 if ' ' in arg_string: 02032 return None 02033 02034 # it was meant to be an optional but there is no such option 02035 # in this parser (though it might be a valid option in a subparser) 02036 return None, arg_string, None 02037
def argparse::ArgumentParser::_parse_optional | ( | self, | |
arg_string | |||
) | [private] |
Definition at line 2021 of file argparse.py.
02022 : 02023 # if it's an empty string, it was meant to be a positional 02024 if not arg_string: 02025 return None 02026 02027 # if it doesn't start with a prefix, it was meant to be positional 02028 if not arg_string[0] in self.prefix_chars: 02029 return None 02030 02031 # if the option string is present in the parser, return the action 02032 if arg_string in self._option_string_actions: 02033 action = self._option_string_actions[arg_string] 02034 return action, arg_string, None 02035 02036 # if it's just a single character, it was meant to be positional 02037 if len(arg_string) == 1: 02038 return None 02039 02040 # if the option string before the "=" is present, return the action 02041 if '=' in arg_string: 02042 option_string, explicit_arg = arg_string.split('=', 1) 02043 if option_string in self._option_string_actions: 02044 action = self._option_string_actions[option_string] 02045 return action, option_string, explicit_arg 02046 02047 # search through all possible prefixes of the option string 02048 # and all actions in the parser for possible interpretations 02049 option_tuples = self._get_option_tuples(arg_string) 02050 02051 # if multiple actions match, the option string was ambiguous 02052 if len(option_tuples) > 1: 02053 options = ', '.join([option_string 02054 for action, option_string, explicit_arg in option_tuples]) 02055 tup = arg_string, options 02056 self.error(_('ambiguous option: %s could match %s') % tup) 02057 02058 # if exactly one action matched, this segmentation is good, 02059 # so return the parsed action 02060 elif len(option_tuples) == 1: 02061 option_tuple, = option_tuples 02062 return option_tuple 02063 02064 # if it was not found as an option, but it looks like a negative 02065 # number, it was meant to be positional 02066 # unless there are negative-number-like options 02067 if self._negative_number_matcher.match(arg_string): 02068 if not self._has_negative_number_optionals: 02069 return None 02070 02071 # if it contains a space, it was meant to be a positional 02072 if ' ' in arg_string: 02073 return None 02074 02075 # it was meant to be an optional but there is no such option 02076 # in this parser (though it might be a valid option in a subparser) 02077 return None, arg_string, None
def argparse::ArgumentParser::_print_message | ( | self, | |
message, | |||
file = None |
|||
) | [private] |
Definition at line 2246 of file argparse.py.
02246 : 02247 if message: 02248 if file is None: 02249 file = _sys.stderr 02250 file.write(message) 02251
def argparse::ArgumentParser::_print_message | ( | self, | |
message, | |||
file = None |
|||
) | [private] |
Definition at line 2313 of file argparse.py.
def argparse::ArgumentParser::_read_args_from_files | ( | self, | |
arg_strings | |||
) | [private] |
Definition at line 1927 of file argparse.py.
01927 : 01928 # expand arguments referencing files 01929 new_arg_strings = [] 01930 for arg_string in arg_strings: 01931 01932 # for regular arguments, just add them back into the list 01933 if arg_string[0] not in self.fromfile_prefix_chars: 01934 new_arg_strings.append(arg_string) 01935 01936 # replace arguments referencing files with the file content 01937 else: 01938 try: 01939 args_file = open(arg_string[1:]) 01940 try: 01941 arg_strings = args_file.read().splitlines() 01942 arg_strings = self._read_args_from_files(arg_strings) 01943 new_arg_strings.extend(arg_strings) 01944 finally: 01945 args_file.close() 01946 except IOError: 01947 err = _sys.exc_info()[1] 01948 self.error(str(err)) 01949 01950 # return the modified argument list 01951 return new_arg_strings 01952
def argparse::ArgumentParser::_read_args_from_files | ( | self, | |
arg_strings | |||
) | [private] |
Definition at line 1954 of file argparse.py.
01955 : 01956 # expand arguments referencing files 01957 new_arg_strings = [] 01958 for arg_string in arg_strings: 01959 01960 # for regular arguments, just add them back into the list 01961 if arg_string[0] not in self.fromfile_prefix_chars: 01962 new_arg_strings.append(arg_string) 01963 01964 # replace arguments referencing files with the file content 01965 else: 01966 try: 01967 args_file = open(arg_string[1:]) 01968 try: 01969 arg_strings = [] 01970 for arg_line in args_file.read().splitlines(): 01971 for arg in self.convert_arg_line_to_args(arg_line): 01972 arg_strings.append(arg) 01973 arg_strings = self._read_args_from_files(arg_strings) 01974 new_arg_strings.extend(arg_strings) 01975 finally: 01976 args_file.close() 01977 except IOError: 01978 err = _sys.exc_info()[1] 01979 self.error(str(err)) 01980 01981 # return the modified argument list 01982 return new_arg_strings
def argparse::ArgumentParser::add_subparsers | ( | self, | |
kwargs | |||
) |
Definition at line 1626 of file argparse.py.
01627 : 01628 if self._subparsers is not None: 01629 self.error(_('cannot have multiple subparser arguments')) 01630 01631 # add the parser class to the arguments if it's not present 01632 kwargs.setdefault('parser_class', type(self)) 01633 01634 if 'title' in kwargs or 'description' in kwargs: 01635 title = _(kwargs.pop('title', 'subcommands')) 01636 description = _(kwargs.pop('description', None)) 01637 self._subparsers = self.add_argument_group(title, description) 01638 else: 01639 self._subparsers = self._positionals 01640 01641 # prog defaults to the usage message of this parser, skipping 01642 # optional arguments and with no "usage:" prefix 01643 if kwargs.get('prog') is None: 01644 formatter = self._get_formatter() 01645 positionals = self._get_positional_actions() 01646 groups = self._mutually_exclusive_groups 01647 formatter.add_usage(self.usage, positionals, groups, '') 01648 kwargs['prog'] = formatter.format_help().strip() 01649 01650 # create the parsers action and add it to the positionals list 01651 parsers_class = self._pop_action_class(kwargs, 'parsers') 01652 action = parsers_class(option_strings=[], **kwargs) 01653 self._subparsers._add_action(action) 01654 01655 # return the created parsers action 01656 return action
def argparse::ArgumentParser::add_subparsers | ( | self, | |
kwargs | |||
) |
Definition at line 1603 of file argparse.py.
01603 : 01604 if self._subparsers is not None: 01605 self.error(_('cannot have multiple subparser arguments')) 01606 01607 # add the parser class to the arguments if it's not present 01608 kwargs.setdefault('parser_class', type(self)) 01609 01610 if 'title' in kwargs or 'description' in kwargs: 01611 title = _(kwargs.pop('title', 'subcommands')) 01612 description = _(kwargs.pop('description', None)) 01613 self._subparsers = self.add_argument_group(title, description) 01614 else: 01615 self._subparsers = self._positionals 01616 01617 # prog defaults to the usage message of this parser, skipping 01618 # optional arguments and with no "usage:" prefix 01619 if kwargs.get('prog') is None: 01620 formatter = self._get_formatter() 01621 positionals = self._get_positional_actions() 01622 groups = self._mutually_exclusive_groups 01623 formatter.add_usage(self.usage, positionals, groups, '') 01624 kwargs['prog'] = formatter.format_help().strip() 01625 01626 # create the parsers action and add it to the positionals list 01627 parsers_class = self._pop_action_class(kwargs, 'parsers') 01628 action = parsers_class(option_strings=[], **kwargs) 01629 self._subparsers._add_action(action) 01630 01631 # return the created parsers action 01632 return action 01633
def argparse::ArgumentParser::convert_arg_line_to_args | ( | self, | |
arg_line | |||
) |
Definition at line 1983 of file argparse.py.
def argparse::ArgumentParser::error | ( | self, | |
message | |||
) |
error(message: string) Prints a usage message incorporating the message to stderr and exits. If you override this in a subclass, it should not return -- it should either exit or raise an exception.
Definition at line 2260 of file argparse.py.
02260 : 02261 """error(message: string) 02262 02263 Prints a usage message incorporating the message to stderr and 02264 exits. 02265 02266 If you override this in a subclass, it should not return -- it 02267 should either exit or raise an exception. 02268 """ 02269 self.print_usage(_sys.stderr) 02270 self.exit(2, _('%s: error: %s\n') % (self.prog, message)) 02271
def argparse::ArgumentParser::error | ( | self, | |
message | |||
) |
error(message: string) Prints a usage message incorporating the message to stderr and exits. If you override this in a subclass, it should not return -- it should either exit or raise an exception.
Definition at line 2327 of file argparse.py.
02328 : 02329 """error(message: string) 02330 02331 Prints a usage message incorporating the message to stderr and 02332 exits. 02333 02334 If you override this in a subclass, it should not return -- it 02335 should either exit or raise an exception. 02336 """ 02337 self.print_usage(_sys.stderr) 02338 self.exit(2, _('%s: error: %s\n') % (self.prog, message))
def argparse::ArgumentParser::exit | ( | self, | |
status = 0 , |
|||
message = None |
|||
) |
Definition at line 2322 of file argparse.py.
def argparse::ArgumentParser::exit | ( | self, | |
status = 0 , |
|||
message = None |
|||
) |
Definition at line 2255 of file argparse.py.
02255 : 02256 if message: 02257 _sys.stderr.write(message) 02258 _sys.exit(status) 02259
def argparse::ArgumentParser::format_help | ( | self | ) |
Definition at line 2256 of file argparse.py.
02257 : 02258 formatter = self._get_formatter() 02259 02260 # usage 02261 formatter.add_usage(self.usage, self._actions, 02262 self._mutually_exclusive_groups) 02263 02264 # description 02265 formatter.add_text(self.description) 02266 02267 # positionals, optionals and user-defined groups 02268 for action_group in self._action_groups: 02269 formatter.start_section(action_group.title) 02270 formatter.add_text(action_group.description) 02271 formatter.add_arguments(action_group._group_actions) 02272 formatter.end_section() 02273 02274 # epilog 02275 formatter.add_text(self.epilog) 02276 02277 # determine help from format above 02278 return formatter.format_help()
def argparse::ArgumentParser::format_help | ( | self | ) |
Definition at line 2203 of file argparse.py.
02203 : 02204 formatter = self._get_formatter() 02205 02206 # usage 02207 formatter.add_usage(self.usage, self._actions, 02208 self._mutually_exclusive_groups) 02209 02210 # description 02211 formatter.add_text(self.description) 02212 02213 # positionals, optionals and user-defined groups 02214 for action_group in self._action_groups: 02215 formatter.start_section(action_group.title) 02216 formatter.add_text(action_group.description) 02217 formatter.add_arguments(action_group._group_actions) 02218 formatter.end_section() 02219 02220 # epilog 02221 formatter.add_text(self.epilog) 02222 02223 # determine help from format above 02224 return formatter.format_help() 02225
def argparse::ArgumentParser::format_usage | ( | self | ) |
Definition at line 2250 of file argparse.py.
def argparse::ArgumentParser::format_usage | ( | self | ) |
Definition at line 2197 of file argparse.py.
02197 : 02198 formatter = self._get_formatter() 02199 formatter.add_usage(self.usage, self._actions, 02200 self._mutually_exclusive_groups) 02201 return formatter.format_help() 02202
def argparse::ArgumentParser::format_version | ( | self | ) |
Definition at line 2226 of file argparse.py.
02226 : 02227 formatter = self._get_formatter() 02228 formatter.add_text(self.version) 02229 return formatter.format_help() 02230
def argparse::ArgumentParser::format_version | ( | self | ) |
Definition at line 2279 of file argparse.py.
02280 : 02281 import warnings 02282 warnings.warn( 02283 'The format_version method is deprecated -- the "version" ' 02284 'argument to ArgumentParser is no longer supported.', 02285 DeprecationWarning) 02286 formatter = self._get_formatter() 02287 formatter.add_text(self.version) 02288 return formatter.format_help()
def argparse::ArgumentParser::parse_args | ( | self, | |
args = None , |
|||
namespace = None |
|||
) |
Definition at line 1677 of file argparse.py.
def argparse::ArgumentParser::parse_args | ( | self, | |
args = None , |
|||
namespace = None |
|||
) |
Definition at line 1654 of file argparse.py.
01654 : 01655 args, argv = self.parse_known_args(args, namespace) 01656 if argv: 01657 msg = _('unrecognized arguments: %s') 01658 self.error(msg % ' '.join(argv)) 01659 return args 01660
def argparse::ArgumentParser::parse_known_args | ( | self, | |
args = None , |
|||
namespace = None |
|||
) |
Definition at line 1661 of file argparse.py.
01661 : 01662 # args default to the system args 01663 if args is None: 01664 args = _sys.argv[1:] 01665 01666 # default Namespace built from parser defaults 01667 if namespace is None: 01668 namespace = Namespace() 01669 01670 # add any action defaults that aren't present 01671 for action in self._actions: 01672 if action.dest is not SUPPRESS: 01673 if not hasattr(namespace, action.dest): 01674 if action.default is not SUPPRESS: 01675 default = action.default 01676 if isinstance(action.default, _basestring): 01677 default = self._get_value(action, default) 01678 setattr(namespace, action.dest, default) 01679 01680 # add any parser defaults that aren't present 01681 for dest in self._defaults: 01682 if not hasattr(namespace, dest): 01683 setattr(namespace, dest, self._defaults[dest]) 01684 01685 # parse the arguments and exit if there are any errors 01686 try: 01687 return self._parse_known_args(args, namespace) 01688 except ArgumentError: 01689 err = _sys.exc_info()[1] 01690 self.error(str(err)) 01691
def argparse::ArgumentParser::parse_known_args | ( | self, | |
args = None , |
|||
namespace = None |
|||
) |
Definition at line 1684 of file argparse.py.
01685 : 01686 # args default to the system args 01687 if args is None: 01688 args = _sys.argv[1:] 01689 01690 # default Namespace built from parser defaults 01691 if namespace is None: 01692 namespace = Namespace() 01693 01694 # add any action defaults that aren't present 01695 for action in self._actions: 01696 if action.dest is not SUPPRESS: 01697 if not hasattr(namespace, action.dest): 01698 if action.default is not SUPPRESS: 01699 default = action.default 01700 if isinstance(action.default, basestring): 01701 default = self._get_value(action, default) 01702 setattr(namespace, action.dest, default) 01703 01704 # add any parser defaults that aren't present 01705 for dest in self._defaults: 01706 if not hasattr(namespace, dest): 01707 setattr(namespace, dest, self._defaults[dest]) 01708 01709 # parse the arguments and exit if there are any errors 01710 try: 01711 namespace, args = self._parse_known_args(args, namespace) 01712 if hasattr(namespace, _UNRECOGNIZED_ARGS_ATTR): 01713 args.extend(getattr(namespace, _UNRECOGNIZED_ARGS_ATTR)) 01714 delattr(namespace, _UNRECOGNIZED_ARGS_ATTR) 01715 return namespace, args 01716 except ArgumentError: 01717 err = _sys.exc_info()[1] 01718 self.error(str(err))
def argparse::ArgumentParser::print_help | ( | self, | |
file = None |
|||
) |
Definition at line 2240 of file argparse.py.
02240 : 02241 self._print_message(self.format_help(), file) 02242
def argparse::ArgumentParser::print_help | ( | self, | |
file = None |
|||
) |
Definition at line 2300 of file argparse.py.
def argparse::ArgumentParser::print_usage | ( | self, | |
file = None |
|||
) |
Definition at line 2295 of file argparse.py.
def argparse::ArgumentParser::print_usage | ( | self, | |
file = None |
|||
) |
Definition at line 2237 of file argparse.py.
02237 : 02238 self._print_message(self.format_usage(), file) 02239
def argparse::ArgumentParser::print_version | ( | self, | |
file = None |
|||
) |
Definition at line 2305 of file argparse.py.
def argparse::ArgumentParser::print_version | ( | self, | |
file = None |
|||
) |
Definition at line 2243 of file argparse.py.
02243 : 02244 self._print_message(self.format_version(), file) 02245
argparse::ArgumentParser::_optionals [private] |
Definition at line 1533 of file argparse.py.
argparse::ArgumentParser::_positionals [private] |
Definition at line 1533 of file argparse.py.
argparse::ArgumentParser::_subparsers [private] |
Definition at line 1533 of file argparse.py.
Definition at line 1533 of file argparse.py.
Definition at line 1533 of file argparse.py.
Definition at line 1533 of file argparse.py.
Definition at line 1533 of file argparse.py.
Definition at line 1533 of file argparse.py.
Definition at line 1533 of file argparse.py.
Definition at line 1533 of file argparse.py.