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