Back to index

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

List of all members.

Public Member Functions

def __init__
def set_conflict_handler
def set_description
def get_description
def destroy
def add_option
def add_options
def get_option
def has_option
def remove_option
def format_option_help
def format_description
def format_help

Public Attributes

 option_class
 defaults
 conflict_handler
 description

Private Member Functions

def _create_option_mappings
def _share_option_mappings
def _check_conflict

Private Attributes

 _short_opt
 _long_opt

Detailed Description

Abstract base class.

Class attributes:
  standard_option_list : [Option]
    list of standard options that will be accepted by all instances
    of this parser class (intended to be overridden by subclasses).

Instance attributes:
  option_list : [Option]
    the list of Option objects contained by this OptionContainer
  _short_opt : { string : Option }
    dictionary mapping short option strings, eg. "-f" or "-X",
    to the Option instances that implement them.  If an Option
    has multiple short option strings, it will appears in this
    dictionary multiple times. [1]
  _long_opt : { string : Option }
    dictionary mapping long option strings, eg. "--file" or
    "--exclude", to the Option instances that implement them.
    Again, a given Option can occur multiple times in this
    dictionary. [1]
  defaults : { string : any }
    dictionary mapping option destination names to default
    values for each destination [1]

[1] These mappings are common to (shared by) all components of the
    controlling OptionParser, where they are initially created.

Definition at line 889 of file optparse.py.


Constructor & Destructor Documentation

def optparse.OptionContainer.__init__ (   self,
  option_class,
  conflict_handler,
  description 
)

Reimplemented in optparse.OptionGroup.

Definition at line 921 of file optparse.py.

00921 
00922     def __init__(self, option_class, conflict_handler, description):
00923         # Initialize the option list and related data structures.
00924         # This method must be provided by subclasses, and it must
00925         # initialize at least the following instance attributes:
00926         # option_list, _short_opt, _long_opt, defaults.
00927         self._create_option_list()
00928 
00929         self.option_class = option_class
00930         self.set_conflict_handler(conflict_handler)
00931         self.set_description(description)

Here is the call graph for this function:

Here is the caller graph for this function:


Member Function Documentation

def optparse.OptionContainer._check_conflict (   self,
  option 
) [private]

Definition at line 969 of file optparse.py.

00969 
00970     def _check_conflict(self, option):
00971         conflict_opts = []
00972         for opt in option._short_opts:
00973             if opt in self._short_opt:
00974                 conflict_opts.append((opt, self._short_opt[opt]))
00975         for opt in option._long_opts:
00976             if opt in self._long_opt:
00977                 conflict_opts.append((opt, self._long_opt[opt]))
00978 
00979         if conflict_opts:
00980             handler = self.conflict_handler
00981             if handler == "error":
00982                 raise OptionConflictError(
00983                     "conflicting option string(s): %s"
00984                     % ", ".join([co[0] for co in conflict_opts]),
00985                     option)
00986             elif handler == "resolve":
00987                 for (opt, c_option) in conflict_opts:
00988                     if opt.startswith("--"):
00989                         c_option._long_opts.remove(opt)
00990                         del self._long_opt[opt]
00991                     else:
00992                         c_option._short_opts.remove(opt)
00993                         del self._short_opt[opt]
00994                     if not (c_option._short_opts or c_option._long_opts):
00995                         c_option.container.option_list.remove(c_option)

Here is the caller graph for this function:

Definition at line 932 of file optparse.py.

00932 
00933     def _create_option_mappings(self):
00934         # For use by OptionParser constructor -- create the master
00935         # option mappings used by this OptionParser and all
00936         # OptionGroups that it owns.
00937         self._short_opt = {}            # single letter -> Option instance
00938         self._long_opt = {}             # long option -> Option instance
00939         self.defaults = {}              # maps option dest -> default value
00940 

def optparse.OptionContainer._share_option_mappings (   self,
  parser 
) [private]

Definition at line 941 of file optparse.py.

00941 
00942     def _share_option_mappings(self, parser):
00943         # For use by OptionGroup constructor -- use shared option
00944         # mappings from the OptionParser that owns this OptionGroup.
00945         self._short_opt = parser._short_opt
00946         self._long_opt = parser._long_opt
00947         self.defaults = parser.defaults

def optparse.OptionContainer.add_option (   self,
  args,
  kwargs 
)
add_option(Option)
   add_option(opt_str, ..., kwarg=val, ...)

Definition at line 996 of file optparse.py.

00996 
00997     def add_option(self, *args, **kwargs):
00998         """add_option(Option)
00999            add_option(opt_str, ..., kwarg=val, ...)
01000         """
01001         if isinstance(args[0], str):
01002             option = self.option_class(*args, **kwargs)
01003         elif len(args) == 1 and not kwargs:
01004             option = args[0]
01005             if not isinstance(option, Option):
01006                 raise TypeError("not an Option instance: %r" % option)
01007         else:
01008             raise TypeError("invalid arguments")
01009 
01010         self._check_conflict(option)
01011 
01012         self.option_list.append(option)
01013         option.container = self
01014         for opt in option._short_opts:
01015             self._short_opt[opt] = option
01016         for opt in option._long_opts:
01017             self._long_opt[opt] = option
01018 
01019         if option.dest is not None:     # option has a dest, we need a default
01020             if option.default is not NO_DEFAULT:
01021                 self.defaults[option.dest] = option.default
01022             elif option.dest not in self.defaults:
01023                 self.defaults[option.dest] = None
01024 
01025         return option

Here is the call graph for this function:

Here is the caller graph for this function:

def optparse.OptionContainer.add_options (   self,
  option_list 
)

Definition at line 1026 of file optparse.py.

01026 
01027     def add_options(self, option_list):
01028         for option in option_list:
01029             self.add_option(option)

Here is the call graph for this function:

Here is the caller graph for this function:

see OptionParser.destroy().

Reimplemented in optparse.OptionParser, and optparse.OptionGroup.

Definition at line 960 of file optparse.py.

00960 
00961     def destroy(self):
00962         """see OptionParser.destroy()."""
00963         del self._short_opt
00964         del self._long_opt
00965         del self.defaults
00966 

Here is the caller graph for this function:

def optparse.OptionContainer.format_description (   self,
  formatter 
)

Definition at line 1065 of file optparse.py.

01065 
01066     def format_description(self, formatter):
01067         return formatter.format_description(self.get_description())

Here is the call graph for this function:

Here is the caller graph for this function:

def optparse.OptionContainer.format_help (   self,
  formatter 
)

Reimplemented in optparse.OptionParser, and optparse.OptionGroup.

Definition at line 1068 of file optparse.py.

01068 
01069     def format_help(self, formatter):
01070         result = []
01071         if self.description:
01072             result.append(self.format_description(formatter))
01073         if self.option_list:
01074             result.append(self.format_option_help(formatter))
01075         return "\n".join(result)
01076 

Here is the call graph for this function:

Here is the caller graph for this function:

def optparse.OptionContainer.format_option_help (   self,
  formatter 
)

Reimplemented in optparse.OptionParser.

Definition at line 1056 of file optparse.py.

01056 
01057     def format_option_help(self, formatter):
01058         if not self.option_list:
01059             return ""
01060         result = []
01061         for option in self.option_list:
01062             if not option.help is SUPPRESS_HELP:
01063                 result.append(formatter.format_option(option))
01064         return "".join(result)

Here is the caller graph for this function:

Reimplemented in optparse.OptionParser.

Definition at line 956 of file optparse.py.

00956 
00957     def get_description(self):
00958         return self.description
00959 

Here is the caller graph for this function:

def optparse.OptionContainer.get_option (   self,
  opt_str 
)

Definition at line 1032 of file optparse.py.

01032 
01033     def get_option(self, opt_str):
01034         return (self._short_opt.get(opt_str) or
01035                 self._long_opt.get(opt_str))

def optparse.OptionContainer.has_option (   self,
  opt_str 
)

Definition at line 1036 of file optparse.py.

01036 
01037     def has_option(self, opt_str):
01038         return (opt_str in self._short_opt or
01039                 opt_str in self._long_opt)

def optparse.OptionContainer.remove_option (   self,
  opt_str 
)

Definition at line 1040 of file optparse.py.

01040 
01041     def remove_option(self, opt_str):
01042         option = self._short_opt.get(opt_str)
01043         if option is None:
01044             option = self._long_opt.get(opt_str)
01045         if option is None:
01046             raise ValueError("no such option %r" % opt_str)
01047 
01048         for opt in option._short_opts:
01049             del self._short_opt[opt]
01050         for opt in option._long_opts:
01051             del self._long_opt[opt]
01052         option.container.option_list.remove(option)
01053 

def optparse.OptionContainer.set_conflict_handler (   self,
  handler 
)

Definition at line 948 of file optparse.py.

00948 
00949     def set_conflict_handler(self, handler):
00950         if handler not in ("error", "resolve"):
00951             raise ValueError("invalid conflict_resolution value %r" % handler)
00952         self.conflict_handler = handler

def optparse.OptionContainer.set_description (   self,
  description 
)

Definition at line 953 of file optparse.py.

00953 
00954     def set_description(self, description):
00955         self.description = description


Member Data Documentation

Definition at line 937 of file optparse.py.

Definition at line 936 of file optparse.py.

Definition at line 951 of file optparse.py.

Definition at line 938 of file optparse.py.

Definition at line 954 of file optparse.py.

Definition at line 928 of file optparse.py.


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