Definition at line 1165 of file argparse.py.
def argparse::_ActionsContainer::__init__ | ( | self, | |
description, | |||
prefix_chars, | |||
argument_default, | |||
conflict_handler | |||
) |
Reimplemented in argparse::_ArgumentGroup, and argparse::_ArgumentGroup.
Definition at line 1167 of file argparse.py.
01172 : 01173 super(_ActionsContainer, self).__init__() 01174 01175 self.description = description 01176 self.argument_default = argument_default 01177 self.prefix_chars = prefix_chars 01178 self.conflict_handler = conflict_handler 01179 01180 # set up registries 01181 self._registries = {} 01182 01183 # register actions 01184 self.register('action', None, _StoreAction) 01185 self.register('action', 'store', _StoreAction) 01186 self.register('action', 'store_const', _StoreConstAction) 01187 self.register('action', 'store_true', _StoreTrueAction) 01188 self.register('action', 'store_false', _StoreFalseAction) 01189 self.register('action', 'append', _AppendAction) 01190 self.register('action', 'append_const', _AppendConstAction) 01191 self.register('action', 'count', _CountAction) 01192 self.register('action', 'help', _HelpAction) 01193 self.register('action', 'version', _VersionAction) 01194 self.register('action', 'parsers', _SubParsersAction) 01195 01196 # raise an exception if the conflict handler is invalid 01197 self._get_handler() 01198 01199 # action storage 01200 self._actions = [] 01201 self._option_string_actions = {} 01202 01203 # groups 01204 self._action_groups = [] 01205 self._mutually_exclusive_groups = [] 01206 01207 # defaults storage 01208 self._defaults = {} 01209 01210 # determines whether an "option" looks like a negative number 01211 self._negative_number_matcher = _re.compile(r'^-\d+$|^-\d*\.\d+$') 01212 01213 # whether or not there are any optionals that look like negative 01214 # numbers -- uses a list so it can be shared and edited 01215 self._has_negative_number_optionals = []
def argparse::_ActionsContainer::__init__ | ( | self, | |
description, | |||
prefix_chars, | |||
argument_default, | |||
conflict_handler | |||
) |
Reimplemented in argparse::_ArgumentGroup, and argparse::_ArgumentGroup.
Definition at line 1163 of file argparse.py.
01167 : 01168 super(_ActionsContainer, self).__init__() 01169 01170 self.description = description 01171 self.argument_default = argument_default 01172 self.prefix_chars = prefix_chars 01173 self.conflict_handler = conflict_handler 01174 01175 # set up registries 01176 self._registries = {} 01177 01178 # register actions 01179 self.register('action', None, _StoreAction) 01180 self.register('action', 'store', _StoreAction) 01181 self.register('action', 'store_const', _StoreConstAction) 01182 self.register('action', 'store_true', _StoreTrueAction) 01183 self.register('action', 'store_false', _StoreFalseAction) 01184 self.register('action', 'append', _AppendAction) 01185 self.register('action', 'append_const', _AppendConstAction) 01186 self.register('action', 'count', _CountAction) 01187 self.register('action', 'help', _HelpAction) 01188 self.register('action', 'version', _VersionAction) 01189 self.register('action', 'parsers', _SubParsersAction) 01190 01191 # raise an exception if the conflict handler is invalid 01192 self._get_handler() 01193 01194 # action storage 01195 self._actions = [] 01196 self._option_string_actions = {} 01197 01198 # groups 01199 self._action_groups = [] 01200 self._mutually_exclusive_groups = [] 01201 01202 # defaults storage 01203 self._defaults = {} 01204 01205 # determines whether an "option" looks like a negative number 01206 self._negative_number_matcher = _re.compile(r'^-\d+|-\d*.\d+$') 01207 01208 # whether or not there are any optionals that look like negative 01209 # numbers -- uses a list so it can be shared and edited 01210 self._has_negative_number_optionals = [] 01211
def argparse::_ActionsContainer::_add_action | ( | self, | |
action | |||
) | [private] |
Reimplemented in argparse::_ArgumentGroup, argparse::_MutuallyExclusiveGroup, argparse::ArgumentParser, argparse::_ArgumentGroup, argparse::_MutuallyExclusiveGroup, and argparse::ArgumentParser.
Definition at line 1298 of file argparse.py.
01299 : 01300 # resolve any conflicts 01301 self._check_conflict(action) 01302 01303 # add to actions list 01304 self._actions.append(action) 01305 action.container = self 01306 01307 # index the action by any option strings it has 01308 for option_string in action.option_strings: 01309 self._option_string_actions[option_string] = action 01310 01311 # set the flag if any option strings look like negative numbers 01312 for option_string in action.option_strings: 01313 if self._negative_number_matcher.match(option_string): 01314 if not self._has_negative_number_optionals: 01315 self._has_negative_number_optionals.append(True) 01316 01317 # return the created action 01318 return action
def argparse::_ActionsContainer::_add_action | ( | self, | |
action | |||
) | [private] |
Reimplemented in argparse::_ArgumentGroup, argparse::_MutuallyExclusiveGroup, argparse::ArgumentParser, argparse::_ArgumentGroup, argparse::_MutuallyExclusiveGroup, and argparse::ArgumentParser.
Definition at line 1277 of file argparse.py.
01277 : 01278 # resolve any conflicts 01279 self._check_conflict(action) 01280 01281 # add to actions list 01282 self._actions.append(action) 01283 action.container = self 01284 01285 # index the action by any option strings it has 01286 for option_string in action.option_strings: 01287 self._option_string_actions[option_string] = action 01288 01289 # set the flag if any option strings look like negative numbers 01290 for option_string in action.option_strings: 01291 if self._negative_number_matcher.match(option_string): 01292 if not self._has_negative_number_optionals: 01293 self._has_negative_number_optionals.append(True) 01294 01295 # return the created action 01296 return action 01297
def argparse::_ActionsContainer::_add_container_actions | ( | self, | |
container | |||
) | [private] |
Definition at line 1322 of file argparse.py.
01323 : 01324 # collect groups by titles 01325 title_group_map = {} 01326 for group in self._action_groups: 01327 if group.title in title_group_map: 01328 msg = _('cannot merge actions - two groups are named %r') 01329 raise ValueError(msg % (group.title)) 01330 title_group_map[group.title] = group 01331 01332 # map each action to its group 01333 group_map = {} 01334 for group in container._action_groups: 01335 01336 # if a group with the title exists, use that, otherwise 01337 # create a new group matching the container's group 01338 if group.title not in title_group_map: 01339 title_group_map[group.title] = self.add_argument_group( 01340 title=group.title, 01341 description=group.description, 01342 conflict_handler=group.conflict_handler) 01343 01344 # map the actions to their new group 01345 for action in group._group_actions: 01346 group_map[action] = title_group_map[group.title] 01347 01348 # add container's mutually exclusive groups 01349 # NOTE: if add_mutually_exclusive_group ever gains title= and 01350 # description= then this code will need to be expanded as above 01351 for group in container._mutually_exclusive_groups: 01352 mutex_group = self.add_mutually_exclusive_group( 01353 required=group.required) 01354 01355 # map the actions to their new mutex group 01356 for action in group._group_actions: 01357 group_map[action] = mutex_group 01358 01359 # add all actions to this container or their group 01360 for action in container._actions: 01361 group_map.get(action, self)._add_action(action)
def argparse::_ActionsContainer::_add_container_actions | ( | self, | |
container | |||
) | [private] |
Definition at line 1301 of file argparse.py.
01301 : 01302 # collect groups by titles 01303 title_group_map = {} 01304 for group in self._action_groups: 01305 if group.title in title_group_map: 01306 msg = _('cannot merge actions - two groups are named %r') 01307 raise ValueError(msg % (group.title)) 01308 title_group_map[group.title] = group 01309 01310 # map each action to its group 01311 group_map = {} 01312 for group in container._action_groups: 01313 01314 # if a group with the title exists, use that, otherwise 01315 # create a new group matching the container's group 01316 if group.title not in title_group_map: 01317 title_group_map[group.title] = self.add_argument_group( 01318 title=group.title, 01319 description=group.description, 01320 conflict_handler=group.conflict_handler) 01321 01322 # map the actions to their new group 01323 for action in group._group_actions: 01324 group_map[action] = title_group_map[group.title] 01325 01326 # add container's mutually exclusive groups 01327 # NOTE: if add_mutually_exclusive_group ever gains title= and 01328 # description= then this code will need to be expanded as above 01329 for group in container._mutually_exclusive_groups: 01330 mutex_group = self.add_mutually_exclusive_group( 01331 required=group.required) 01332 01333 # map the actions to their new mutex group 01334 for action in group._group_actions: 01335 group_map[action] = mutex_group 01336 01337 # add all actions to this container or their group 01338 for action in container._actions: 01339 group_map.get(action, self)._add_action(action) 01340
def argparse::_ActionsContainer::_check_conflict | ( | self, | |
action | |||
) | [private] |
Definition at line 1426 of file argparse.py.
01427 : 01428 01429 # find all options that conflict with this option 01430 confl_optionals = [] 01431 for option_string in action.option_strings: 01432 if option_string in self._option_string_actions: 01433 confl_optional = self._option_string_actions[option_string] 01434 confl_optionals.append((option_string, confl_optional)) 01435 01436 # resolve any conflicts 01437 if confl_optionals: 01438 conflict_handler = self._get_handler() 01439 conflict_handler(action, confl_optionals)
def argparse::_ActionsContainer::_check_conflict | ( | self, | |
action | |||
) | [private] |
Definition at line 1414 of file argparse.py.
01414 : 01415 01416 # find all options that conflict with this option 01417 confl_optionals = [] 01418 for option_string in action.option_strings: 01419 if option_string in self._option_string_actions: 01420 confl_optional = self._option_string_actions[option_string] 01421 confl_optionals.append((option_string, confl_optional)) 01422 01423 # resolve any conflicts 01424 if confl_optionals: 01425 conflict_handler = self._get_handler() 01426 conflict_handler(action, confl_optionals) 01427
def argparse::_ActionsContainer::_get_handler | ( | self | ) | [private] |
Definition at line 1405 of file argparse.py.
01405 : 01406 # determine function from conflict handler string 01407 handler_func_name = '_handle_conflict_%s' % self.conflict_handler 01408 try: 01409 return getattr(self, handler_func_name) 01410 except AttributeError: 01411 msg = _('invalid conflict_resolution value: %r') 01412 raise ValueError(msg % self.conflict_handler) 01413
def argparse::_ActionsContainer::_get_handler | ( | self | ) | [private] |
Definition at line 1417 of file argparse.py.
01418 : 01419 # determine function from conflict handler string 01420 handler_func_name = '_handle_conflict_%s' % self.conflict_handler 01421 try: 01422 return getattr(self, handler_func_name) 01423 except AttributeError: 01424 msg = _('invalid conflict_resolution value: %r') 01425 raise ValueError(msg % self.conflict_handler)
def argparse::_ActionsContainer::_get_optional_kwargs | ( | self, | |
args, | |||
kwargs | |||
) | [private] |
Definition at line 1357 of file argparse.py.
01357 : 01358 # determine short and long option strings 01359 option_strings = [] 01360 long_option_strings = [] 01361 for option_string in args: 01362 # error on one-or-fewer-character option strings 01363 if len(option_string) < 2: 01364 msg = _('invalid option string %r: ' 01365 'must be at least two characters long') 01366 raise ValueError(msg % option_string) 01367 01368 # error on strings that don't start with an appropriate prefix 01369 if not option_string[0] in self.prefix_chars: 01370 msg = _('invalid option string %r: ' 01371 'must start with a character %r') 01372 tup = option_string, self.prefix_chars 01373 raise ValueError(msg % tup) 01374 01375 # error on strings that are all prefix characters 01376 if not (_set(option_string) - _set(self.prefix_chars)): 01377 msg = _('invalid option string %r: ' 01378 'must contain characters other than %r') 01379 tup = option_string, self.prefix_chars 01380 raise ValueError(msg % tup) 01381 01382 # strings starting with two prefix characters are long options 01383 option_strings.append(option_string) 01384 if option_string[0] in self.prefix_chars: 01385 if option_string[1] in self.prefix_chars: 01386 long_option_strings.append(option_string) 01387 01388 # infer destination, '--foo-bar' -> 'foo_bar' and '-x' -> 'x' 01389 dest = kwargs.pop('dest', None) 01390 if dest is None: 01391 if long_option_strings: 01392 dest_option_string = long_option_strings[0] 01393 else: 01394 dest_option_string = option_strings[0] 01395 dest = dest_option_string.lstrip(self.prefix_chars) 01396 dest = dest.replace('-', '_') 01397 01398 # return the updated keyword arguments 01399 return dict(kwargs, dest=dest, option_strings=option_strings) 01400
def argparse::_ActionsContainer::_get_optional_kwargs | ( | self, | |
args, | |||
kwargs | |||
) | [private] |
Definition at line 1378 of file argparse.py.
01379 : 01380 # determine short and long option strings 01381 option_strings = [] 01382 long_option_strings = [] 01383 for option_string in args: 01384 # error on strings that don't start with an appropriate prefix 01385 if not option_string[0] in self.prefix_chars: 01386 msg = _('invalid option string %r: ' 01387 'must start with a character %r') 01388 tup = option_string, self.prefix_chars 01389 raise ValueError(msg % tup) 01390 01391 # strings starting with two prefix characters are long options 01392 option_strings.append(option_string) 01393 if option_string[0] in self.prefix_chars: 01394 if len(option_string) > 1: 01395 if option_string[1] in self.prefix_chars: 01396 long_option_strings.append(option_string) 01397 01398 # infer destination, '--foo-bar' -> 'foo_bar' and '-x' -> 'x' 01399 dest = kwargs.pop('dest', None) 01400 if dest is None: 01401 if long_option_strings: 01402 dest_option_string = long_option_strings[0] 01403 else: 01404 dest_option_string = option_strings[0] 01405 dest = dest_option_string.lstrip(self.prefix_chars) 01406 if not dest: 01407 msg = _('dest= is required for options like %r') 01408 raise ValueError(msg % option_string) 01409 dest = dest.replace('-', '_') 01410 01411 # return the updated keyword arguments 01412 return dict(kwargs, dest=dest, option_strings=option_strings)
def argparse::_ActionsContainer::_get_positional_kwargs | ( | self, | |
dest, | |||
kwargs | |||
) | [private] |
Definition at line 1362 of file argparse.py.
01363 : 01364 # make sure required is not specified 01365 if 'required' in kwargs: 01366 msg = _("'required' is an invalid argument for positionals") 01367 raise TypeError(msg) 01368 01369 # mark positional arguments as required if at least one is 01370 # always required 01371 if kwargs.get('nargs') not in [OPTIONAL, ZERO_OR_MORE]: 01372 kwargs['required'] = True 01373 if kwargs.get('nargs') == ZERO_OR_MORE and 'default' not in kwargs: 01374 kwargs['required'] = True 01375 01376 # return the keyword arguments with no option strings 01377 return dict(kwargs, dest=dest, option_strings=[])
def argparse::_ActionsContainer::_get_positional_kwargs | ( | self, | |
dest, | |||
kwargs | |||
) | [private] |
Definition at line 1341 of file argparse.py.
01341 : 01342 # make sure required is not specified 01343 if 'required' in kwargs: 01344 msg = _("'required' is an invalid argument for positionals") 01345 raise TypeError(msg) 01346 01347 # mark positional arguments as required if at least one is 01348 # always required 01349 if kwargs.get('nargs') not in [OPTIONAL, ZERO_OR_MORE]: 01350 kwargs['required'] = True 01351 if kwargs.get('nargs') == ZERO_OR_MORE and 'default' not in kwargs: 01352 kwargs['required'] = True 01353 01354 # return the keyword arguments with no option strings 01355 return dict(kwargs, dest=dest, option_strings=[]) 01356
def argparse::_ActionsContainer::_handle_conflict_error | ( | self, | |
action, | |||
conflicting_actions | |||
) | [private] |
Definition at line 1440 of file argparse.py.
def argparse::_ActionsContainer::_handle_conflict_error | ( | self, | |
action, | |||
conflicting_actions | |||
) | [private] |
Definition at line 1428 of file argparse.py.
01428 : 01429 message = _('conflicting option string(s): %s') 01430 conflict_string = ', '.join([option_string 01431 for option_string, action 01432 in conflicting_actions]) 01433 raise ArgumentError(action, message % conflict_string) 01434
def argparse::_ActionsContainer::_handle_conflict_resolve | ( | self, | |
action, | |||
conflicting_actions | |||
) | [private] |
Definition at line 1447 of file argparse.py.
01448 : 01449 01450 # remove all conflicting options 01451 for option_string, action in conflicting_actions: 01452 01453 # remove the conflicting option 01454 action.option_strings.remove(option_string) 01455 self._option_string_actions.pop(option_string, None) 01456 01457 # if the option now has no option string, remove it from the 01458 # container holding it 01459 if not action.option_strings: 01460 action.container._remove_action(action) 01461
def argparse::_ActionsContainer::_handle_conflict_resolve | ( | self, | |
action, | |||
conflicting_actions | |||
) | [private] |
Definition at line 1435 of file argparse.py.
01435 : 01436 01437 # remove all conflicting options 01438 for option_string, action in conflicting_actions: 01439 01440 # remove the conflicting option 01441 action.option_strings.remove(option_string) 01442 self._option_string_actions.pop(option_string, None) 01443 01444 # if the option now has no option string, remove it from the 01445 # container holding it 01446 if not action.option_strings: 01447 action.container._remove_action(action) 01448 01449
def argparse::_ActionsContainer::_pop_action_class | ( | self, | |
kwargs, | |||
default = None |
|||
) | [private] |
Definition at line 1413 of file argparse.py.
def argparse::_ActionsContainer::_pop_action_class | ( | self, | |
kwargs, | |||
default = None |
|||
) | [private] |
Definition at line 1401 of file argparse.py.
01401 : 01402 action = kwargs.pop('action', default) 01403 return self._registry_get('action', action, action) 01404
def argparse::_ActionsContainer::_registry_get | ( | self, | |
registry_name, | |||
value, | |||
default = None |
|||
) | [private] |
Definition at line 1219 of file argparse.py.
01219 : 01220 return self._registries[registry_name].get(value, default) 01221
def argparse::_ActionsContainer::_registry_get | ( | self, | |
registry_name, | |||
value, | |||
default = None |
|||
) | [private] |
Definition at line 1223 of file argparse.py.
def argparse::_ActionsContainer::_remove_action | ( | self, | |
action | |||
) | [private] |
Reimplemented in argparse::_ArgumentGroup, argparse::_MutuallyExclusiveGroup, argparse::_ArgumentGroup, and argparse::_MutuallyExclusiveGroup.
Definition at line 1319 of file argparse.py.
def argparse::_ActionsContainer::_remove_action | ( | self, | |
action | |||
) | [private] |
Reimplemented in argparse::_ArgumentGroup, argparse::_MutuallyExclusiveGroup, argparse::_ArgumentGroup, and argparse::_MutuallyExclusiveGroup.
Definition at line 1298 of file argparse.py.
def argparse::_ActionsContainer::add_argument | ( | self, | |
args, | |||
kwargs | |||
) |
add_argument(dest, ..., name=value, ...) add_argument(option_string, option_string, ..., name=value, ...)
Definition at line 1248 of file argparse.py.
01249 : 01250 """ 01251 add_argument(dest, ..., name=value, ...) 01252 add_argument(option_string, option_string, ..., name=value, ...) 01253 """ 01254 01255 # if no positional args are supplied or only one is supplied and 01256 # it doesn't look like an option string, parse a positional 01257 # argument 01258 chars = self.prefix_chars 01259 if not args or len(args) == 1 and args[0][0] not in chars: 01260 if args and 'dest' in kwargs: 01261 raise ValueError('dest supplied twice for positional argument') 01262 kwargs = self._get_positional_kwargs(*args, **kwargs) 01263 01264 # otherwise, we're adding an optional argument 01265 else: 01266 kwargs = self._get_optional_kwargs(*args, **kwargs) 01267 01268 # if no default was supplied, use the parser-level default 01269 if 'default' not in kwargs: 01270 dest = kwargs['dest'] 01271 if dest in self._defaults: 01272 kwargs['default'] = self._defaults[dest] 01273 elif self.argument_default is not None: 01274 kwargs['default'] = self.argument_default 01275 01276 # create the action object, and add it to the parser 01277 action_class = self._pop_action_class(kwargs) 01278 if not _callable(action_class): 01279 raise ValueError('unknown action "%s"' % action_class) 01280 action = action_class(**kwargs) 01281 01282 # raise an error if the action type is not callable 01283 type_func = self._registry_get('type', action.type, action.type) 01284 if not _callable(type_func): 01285 raise ValueError('%r is not callable' % type_func) 01286 01287 return self._add_action(action)
def argparse::_ActionsContainer::add_argument | ( | self, | |
args, | |||
kwargs | |||
) |
add_argument(dest, ..., name=value, ...) add_argument(option_string, option_string, ..., name=value, ...)
Definition at line 1237 of file argparse.py.
01237 : 01238 """ 01239 add_argument(dest, ..., name=value, ...) 01240 add_argument(option_string, option_string, ..., name=value, ...) 01241 """ 01242 01243 # if no positional args are supplied or only one is supplied and 01244 # it doesn't look like an option string, parse a positional 01245 # argument 01246 chars = self.prefix_chars 01247 if not args or len(args) == 1 and args[0][0] not in chars: 01248 kwargs = self._get_positional_kwargs(*args, **kwargs) 01249 01250 # otherwise, we're adding an optional argument 01251 else: 01252 kwargs = self._get_optional_kwargs(*args, **kwargs) 01253 01254 # if no default was supplied, use the parser-level default 01255 if 'default' not in kwargs: 01256 dest = kwargs['dest'] 01257 if dest in self._defaults: 01258 kwargs['default'] = self._defaults[dest] 01259 elif self.argument_default is not None: 01260 kwargs['default'] = self.argument_default 01261 01262 # create the action object, and add it to the parser 01263 action_class = self._pop_action_class(kwargs) 01264 action = action_class(**kwargs) 01265 return self._add_action(action) 01266
def argparse::_ActionsContainer::add_argument_group | ( | self, | |
args, | |||
kwargs | |||
) |
Definition at line 1267 of file argparse.py.
01267 : 01268 group = _ArgumentGroup(self, *args, **kwargs) 01269 self._action_groups.append(group) 01270 return group 01271
def argparse::_ActionsContainer::add_argument_group | ( | self, | |
args, | |||
kwargs | |||
) |
Definition at line 1288 of file argparse.py.
def argparse::_ActionsContainer::add_mutually_exclusive_group | ( | self, | |
kwargs | |||
) |
Definition at line 1293 of file argparse.py.
def argparse::_ActionsContainer::add_mutually_exclusive_group | ( | self, | |
kwargs | |||
) |
Definition at line 1272 of file argparse.py.
01272 : 01273 group = _MutuallyExclusiveGroup(self, **kwargs) 01274 self._mutually_exclusive_groups.append(group) 01275 return group 01276
def argparse::_ActionsContainer::get_default | ( | self, | |
dest | |||
) |
Definition at line 1238 of file argparse.py.
def argparse::_ActionsContainer::register | ( | self, | |
registry_name, | |||
value, | |||
object | |||
) |
Definition at line 1219 of file argparse.py.
def argparse::_ActionsContainer::register | ( | self, | |
registry_name, | |||
value, | |||
object | |||
) |
Definition at line 1215 of file argparse.py.
01215 : 01216 registry = self._registries.setdefault(registry_name, {}) 01217 registry[value] = object 01218
def argparse::_ActionsContainer::set_defaults | ( | self, | |
kwargs | |||
) |
Definition at line 1229 of file argparse.py.
def argparse::_ActionsContainer::set_defaults | ( | self, | |
kwargs | |||
) |
Definition at line 1225 of file argparse.py.
Definition at line 1167 of file argparse.py.
argparse::_ActionsContainer::_actions [private] |
Reimplemented in argparse::_ArgumentGroup.
Definition at line 1167 of file argparse.py.
argparse::_ActionsContainer::_defaults [private] |
Reimplemented in argparse::_ArgumentGroup.
Definition at line 1167 of file argparse.py.
Reimplemented in argparse::_ArgumentGroup.
Definition at line 1167 of file argparse.py.
Definition at line 1167 of file argparse.py.
Definition at line 1167 of file argparse.py.
Reimplemented in argparse::_ArgumentGroup.
Definition at line 1167 of file argparse.py.
argparse::_ActionsContainer::_registries [private] |
Reimplemented in argparse::_ArgumentGroup.
Definition at line 1167 of file argparse.py.
Definition at line 1167 of file argparse.py.
Definition at line 1167 of file argparse.py.
Definition at line 1167 of file argparse.py.
Definition at line 1167 of file argparse.py.