Back to index

python3.2  3.2.2
Public Member Functions | Public Attributes | Private Member Functions | Private Attributes
argparse._ActionsContainer Class Reference
Inheritance diagram for argparse._ActionsContainer:
Inheritance graph
[legend]
Collaboration diagram for argparse._ActionsContainer:
Collaboration graph
[legend]

List of all members.

Public Member Functions

def __init__
def register
def set_defaults
def get_default
def add_argument
def add_argument_group
def add_mutually_exclusive_group

Public Attributes

 description
 argument_default
 prefix_chars
 conflict_handler
_PyObject_HEAD_EXTRA Py_ssize_t ob_refcnt
struct _typeobjectob_type

Private Member Functions

def _registry_get
def _add_action
def _remove_action
def _add_container_actions
def _get_positional_kwargs
def _get_optional_kwargs
def _pop_action_class
def _get_handler
def _check_conflict
def _handle_conflict_error
def _handle_conflict_resolve

Private Attributes

 _registries
 _actions
 _option_string_actions
 _action_groups
 _mutually_exclusive_groups
 _defaults
 _negative_number_matcher
 _has_negative_number_optionals

Detailed Description

Definition at line 1177 of file argparse.py.


Constructor & Destructor Documentation

def argparse._ActionsContainer.__init__ (   self,
  description,
  prefix_chars,
  argument_default,
  conflict_handler 
)

Reimplemented in argparse._ArgumentGroup.

Definition at line 1183 of file argparse.py.

01183 
01184                  conflict_handler):
01185         super(_ActionsContainer, self).__init__()
01186 
01187         self.description = description
01188         self.argument_default = argument_default
01189         self.prefix_chars = prefix_chars
01190         self.conflict_handler = conflict_handler
01191 
01192         # set up registries
01193         self._registries = {}
01194 
01195         # register actions
01196         self.register('action', None, _StoreAction)
01197         self.register('action', 'store', _StoreAction)
01198         self.register('action', 'store_const', _StoreConstAction)
01199         self.register('action', 'store_true', _StoreTrueAction)
01200         self.register('action', 'store_false', _StoreFalseAction)
01201         self.register('action', 'append', _AppendAction)
01202         self.register('action', 'append_const', _AppendConstAction)
01203         self.register('action', 'count', _CountAction)
01204         self.register('action', 'help', _HelpAction)
01205         self.register('action', 'version', _VersionAction)
01206         self.register('action', 'parsers', _SubParsersAction)
01207 
01208         # raise an exception if the conflict handler is invalid
01209         self._get_handler()
01210 
01211         # action storage
01212         self._actions = []
01213         self._option_string_actions = {}
01214 
01215         # groups
01216         self._action_groups = []
01217         self._mutually_exclusive_groups = []
01218 
01219         # defaults storage
01220         self._defaults = {}
01221 
01222         # determines whether an "option" looks like a negative number
01223         self._negative_number_matcher = _re.compile(r'^-\d+$|^-\d*\.\d+$')
01224 
01225         # whether or not there are any optionals that look like negative
01226         # numbers -- uses a list so it can be shared and edited
01227         self._has_negative_number_optionals = []

Here is the caller graph for this function:


Member Function Documentation

def argparse._ActionsContainer._add_action (   self,
  action 
) [private]

Reimplemented in argparse.ArgumentParser, argparse._MutuallyExclusiveGroup, and argparse._ArgumentGroup.

Definition at line 1317 of file argparse.py.

01317 
01318     def _add_action(self, action):
01319         # resolve any conflicts
01320         self._check_conflict(action)
01321 
01322         # add to actions list
01323         self._actions.append(action)
01324         action.container = self
01325 
01326         # index the action by any option strings it has
01327         for option_string in action.option_strings:
01328             self._option_string_actions[option_string] = action
01329 
01330         # set the flag if any option strings look like negative numbers
01331         for option_string in action.option_strings:
01332             if self._negative_number_matcher.match(option_string):
01333                 if not self._has_negative_number_optionals:
01334                     self._has_negative_number_optionals.append(True)
01335 
01336         # return the created action
01337         return action

Here is the call graph for this function:

Here is the caller graph for this function:

def argparse._ActionsContainer._add_container_actions (   self,
  container 
) [private]

Definition at line 1341 of file argparse.py.

01341 
01342     def _add_container_actions(self, container):
01343         # collect groups by titles
01344         title_group_map = {}
01345         for group in self._action_groups:
01346             if group.title in title_group_map:
01347                 msg = _('cannot merge actions - two groups are named %r')
01348                 raise ValueError(msg % (group.title))
01349             title_group_map[group.title] = group
01350 
01351         # map each action to its group
01352         group_map = {}
01353         for group in container._action_groups:
01354 
01355             # if a group with the title exists, use that, otherwise
01356             # create a new group matching the container's group
01357             if group.title not in title_group_map:
01358                 title_group_map[group.title] = self.add_argument_group(
01359                     title=group.title,
01360                     description=group.description,
01361                     conflict_handler=group.conflict_handler)
01362 
01363             # map the actions to their new group
01364             for action in group._group_actions:
01365                 group_map[action] = title_group_map[group.title]
01366 
01367         # add container's mutually exclusive groups
01368         # NOTE: if add_mutually_exclusive_group ever gains title= and
01369         # description= then this code will need to be expanded as above
01370         for group in container._mutually_exclusive_groups:
01371             mutex_group = self.add_mutually_exclusive_group(
01372                 required=group.required)
01373 
01374             # map the actions to their new mutex group
01375             for action in group._group_actions:
01376                 group_map[action] = mutex_group
01377 
01378         # add all actions to this container or their group
01379         for action in container._actions:
01380             group_map.get(action, self)._add_action(action)

Here is the call graph for this function:

def argparse._ActionsContainer._check_conflict (   self,
  action 
) [private]

Definition at line 1446 of file argparse.py.

01446 
01447     def _check_conflict(self, action):
01448 
01449         # find all options that conflict with this option
01450         confl_optionals = []
01451         for option_string in action.option_strings:
01452             if option_string in self._option_string_actions:
01453                 confl_optional = self._option_string_actions[option_string]
01454                 confl_optionals.append((option_string, confl_optional))
01455 
01456         # resolve any conflicts
01457         if confl_optionals:
01458             conflict_handler = self._get_handler()
01459             conflict_handler(action, confl_optionals)

Here is the call graph for this function:

Here is the caller graph for this function:

def argparse._ActionsContainer._get_handler (   self) [private]

Definition at line 1437 of file argparse.py.

01437 
01438     def _get_handler(self):
01439         # determine function from conflict handler string
01440         handler_func_name = '_handle_conflict_%s' % self.conflict_handler
01441         try:
01442             return getattr(self, handler_func_name)
01443         except AttributeError:
01444             msg = _('invalid conflict_resolution value: %r')
01445             raise ValueError(msg % self.conflict_handler)

Here is the call graph for this function:

Here is the caller graph for this function:

def argparse._ActionsContainer._get_optional_kwargs (   self,
  args,
  kwargs 
) [private]

Definition at line 1397 of file argparse.py.

01397 
01398     def _get_optional_kwargs(self, *args, **kwargs):
01399         # determine short and long option strings
01400         option_strings = []
01401         long_option_strings = []
01402         for option_string in args:
01403             # error on strings that don't start with an appropriate prefix
01404             if not option_string[0] in self.prefix_chars:
01405                 args = {'option': option_string,
01406                         'prefix_chars': self.prefix_chars}
01407                 msg = _('invalid option string %(option)r: '
01408                         'must start with a character %(prefix_chars)r')
01409                 raise ValueError(msg % args)
01410 
01411             # strings starting with two prefix characters are long options
01412             option_strings.append(option_string)
01413             if option_string[0] in self.prefix_chars:
01414                 if len(option_string) > 1:
01415                     if option_string[1] in self.prefix_chars:
01416                         long_option_strings.append(option_string)
01417 
01418         # infer destination, '--foo-bar' -> 'foo_bar' and '-x' -> 'x'
01419         dest = kwargs.pop('dest', None)
01420         if dest is None:
01421             if long_option_strings:
01422                 dest_option_string = long_option_strings[0]
01423             else:
01424                 dest_option_string = option_strings[0]
01425             dest = dest_option_string.lstrip(self.prefix_chars)
01426             if not dest:
01427                 msg = _('dest= is required for options like %r')
01428                 raise ValueError(msg % option_string)
01429             dest = dest.replace('-', '_')
01430 
01431         # return the updated keyword arguments
01432         return dict(kwargs, dest=dest, option_strings=option_strings)

Here is the caller graph for this function:

def argparse._ActionsContainer._get_positional_kwargs (   self,
  dest,
  kwargs 
) [private]

Definition at line 1381 of file argparse.py.

01381 
01382     def _get_positional_kwargs(self, dest, **kwargs):
01383         # make sure required is not specified
01384         if 'required' in kwargs:
01385             msg = _("'required' is an invalid argument for positionals")
01386             raise TypeError(msg)
01387 
01388         # mark positional arguments as required if at least one is
01389         # always required
01390         if kwargs.get('nargs') not in [OPTIONAL, ZERO_OR_MORE]:
01391             kwargs['required'] = True
01392         if kwargs.get('nargs') == ZERO_OR_MORE and 'default' not in kwargs:
01393             kwargs['required'] = True
01394 
01395         # return the keyword arguments with no option strings
01396         return dict(kwargs, dest=dest, option_strings=[])

Here is the caller graph for this function:

def argparse._ActionsContainer._handle_conflict_error (   self,
  action,
  conflicting_actions 
) [private]

Definition at line 1460 of file argparse.py.

01460 
01461     def _handle_conflict_error(self, action, conflicting_actions):
01462         message = ngettext('conflicting option string: %s',
01463                            'conflicting option strings: %s',
01464                            len(conflicting_actions))
01465         conflict_string = ', '.join([option_string
01466                                      for option_string, action
01467                                      in conflicting_actions])
01468         raise ArgumentError(action, message % conflict_string)

Here is the call graph for this function:

def argparse._ActionsContainer._handle_conflict_resolve (   self,
  action,
  conflicting_actions 
) [private]

Definition at line 1469 of file argparse.py.

01469 
01470     def _handle_conflict_resolve(self, action, conflicting_actions):
01471 
01472         # remove all conflicting options
01473         for option_string, action in conflicting_actions:
01474 
01475             # remove the conflicting option
01476             action.option_strings.remove(option_string)
01477             self._option_string_actions.pop(option_string, None)
01478 
01479             # if the option now has no option string, remove it from the
01480             # container holding it
01481             if not action.option_strings:
01482                 action.container._remove_action(action)
01483 

def argparse._ActionsContainer._pop_action_class (   self,
  kwargs,
  default = None 
) [private]

Definition at line 1433 of file argparse.py.

01433 
01434     def _pop_action_class(self, kwargs, default=None):
01435         action = kwargs.pop('action', default)
01436         return self._registry_get('action', action, action)

Here is the call graph for this function:

Here is the caller graph for this function:

def argparse._ActionsContainer._registry_get (   self,
  registry_name,
  value,
  default = None 
) [private]

Definition at line 1235 of file argparse.py.

01235 
01236     def _registry_get(self, registry_name, value, default=None):
01237         return self._registries[registry_name].get(value, default)

Here is the call graph for this function:

Here is the caller graph for this function:

def argparse._ActionsContainer._remove_action (   self,
  action 
) [private]

Reimplemented in argparse._MutuallyExclusiveGroup, and argparse._ArgumentGroup.

Definition at line 1338 of file argparse.py.

01338 
01339     def _remove_action(self, action):
01340         self._actions.remove(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 1260 of file argparse.py.

01260 
01261     def add_argument(self, *args, **kwargs):
01262         """
01263         add_argument(dest, ..., name=value, ...)
01264         add_argument(option_string, option_string, ..., name=value, ...)
01265         """
01266 
01267         # if no positional args are supplied or only one is supplied and
01268         # it doesn't look like an option string, parse a positional
01269         # argument
01270         chars = self.prefix_chars
01271         if not args or len(args) == 1 and args[0][0] not in chars:
01272             if args and 'dest' in kwargs:
01273                 raise ValueError('dest supplied twice for positional argument')
01274             kwargs = self._get_positional_kwargs(*args, **kwargs)
01275 
01276         # otherwise, we're adding an optional argument
01277         else:
01278             kwargs = self._get_optional_kwargs(*args, **kwargs)
01279 
01280         # if no default was supplied, use the parser-level default
01281         if 'default' not in kwargs:
01282             dest = kwargs['dest']
01283             if dest in self._defaults:
01284                 kwargs['default'] = self._defaults[dest]
01285             elif self.argument_default is not None:
01286                 kwargs['default'] = self.argument_default
01287 
01288         # create the action object, and add it to the parser
01289         action_class = self._pop_action_class(kwargs)
01290         if not _callable(action_class):
01291             raise ValueError('unknown action "%s"' % (action_class,))
01292         action = action_class(**kwargs)
01293 
01294         # raise an error if the action type is not callable
01295         type_func = self._registry_get('type', action.type, action.type)
01296         if not _callable(type_func):
01297             raise ValueError('%r is not callable' % (type_func,))
01298 
01299         # raise an error if the metavar does not match the type
01300         if hasattr(self, "_get_formatter"):
01301             try:
01302                 self._get_formatter()._format_args(action, None)
01303             except TypeError:
01304                 raise ValueError("length of metavar tuple does not match nargs")
01305 
01306         return self._add_action(action)

Here is the call graph for this function:

def argparse._ActionsContainer.add_argument_group (   self,
  args,
  kwargs 
)

Definition at line 1307 of file argparse.py.

01307 
01308     def add_argument_group(self, *args, **kwargs):
01309         group = _ArgumentGroup(self, *args, **kwargs)
01310         self._action_groups.append(group)
01311         return group

Here is the caller graph for this function:

Definition at line 1312 of file argparse.py.

01312 
01313     def add_mutually_exclusive_group(self, **kwargs):
01314         group = _MutuallyExclusiveGroup(self, **kwargs)
01315         self._mutually_exclusive_groups.append(group)
01316         return group

Here is the caller graph for this function:

def argparse._ActionsContainer.get_default (   self,
  dest 
)

Definition at line 1250 of file argparse.py.

01250 
01251     def get_default(self, dest):
01252         for action in self._actions:
01253             if action.dest == dest and action.default is not None:
01254                 return action.default
01255         return self._defaults.get(dest, None)
01256 

def argparse._ActionsContainer.register (   self,
  registry_name,
  value,
  object 
)

Definition at line 1231 of file argparse.py.

01231 
01232     def register(self, registry_name, value, object):
01233         registry = self._registries.setdefault(registry_name, {})
01234         registry[value] = object

Here is the caller graph for this function:

def argparse._ActionsContainer.set_defaults (   self,
  kwargs 
)

Definition at line 1241 of file argparse.py.

01241 
01242     def set_defaults(self, **kwargs):
01243         self._defaults.update(kwargs)
01244 
01245         # if these defaults match any existing arguments, replace
01246         # the previous default on the object with the new one
01247         for action in self._actions:
01248             if action.dest in kwargs:
01249                 action.default = kwargs[action.dest]


Member Data Documentation

Definition at line 1215 of file argparse.py.

Reimplemented in argparse._ArgumentGroup.

Definition at line 1211 of file argparse.py.

Reimplemented in argparse._ArgumentGroup.

Definition at line 1219 of file argparse.py.

Reimplemented in argparse._ArgumentGroup.

Definition at line 1226 of file argparse.py.

Reimplemented in argparse._ArgumentGroup.

Definition at line 1216 of file argparse.py.

Definition at line 1222 of file argparse.py.

Reimplemented in argparse._ArgumentGroup.

Definition at line 1212 of file argparse.py.

Reimplemented in argparse._ArgumentGroup.

Definition at line 1192 of file argparse.py.

Definition at line 1187 of file argparse.py.

Definition at line 1189 of file argparse.py.

Definition at line 1186 of file argparse.py.

Definition at line 107 of file object.h.

struct _typeobject* _object::ob_type [inherited]

Definition at line 108 of file object.h.

Definition at line 1188 of file argparse.py.


The documentation for this class was generated from the following file: