Back to index

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

List of all members.

Public Member Functions

def __init__
def __str__
def takes_value
def get_opt_string
def check_value
def convert_value
def process
def take_action

Public Attributes

 action
 type
 dest
 nargs

Static Public Attributes

list ATTRS
tuple ACTIONS
tuple STORE_ACTIONS
tuple TYPED_ACTIONS
tuple ALWAYS_TYPED_ACTIONS
tuple CONST_ACTIONS
tuple TYPES = ("string", "int", "long", "float", "complex", "choice")
dictionary TYPE_CHECKER
 CHECK_METHODS = None
list CHECK_METHODS

Private Member Functions

def _check_opt_strings
def _set_opt_strings
def _set_attrs
def _check_action
def _check_type
def _check_choice
def _check_dest
def _check_const
def _check_nargs
def _check_callback

Private Attributes

 _short_opts
 _long_opts

Static Private Attributes

 __repr__ = _repr

Detailed Description

Instance attributes:
  _short_opts : [string]
  _long_opts : [string]

  action : string
  type : string
  dest : string
  default : any
  nargs : int
  const : any
  choices : [string]
  callback : function
  callback_args : (any*)
  callback_kwargs : { string : any }
  help : string
  metavar : string

Definition at line 444 of file optparse.py.


Constructor & Destructor Documentation

def optparse.Option.__init__ (   self,
  opts,
  attrs 
)

Definition at line 559 of file optparse.py.

00559 
00560     def __init__(self, *opts, **attrs):
00561         # Set _short_opts, _long_opts attrs from 'opts' tuple.
00562         # Have to be set now, in case no option strings are supplied.
00563         self._short_opts = []
00564         self._long_opts = []
00565         opts = self._check_opt_strings(opts)
00566         self._set_opt_strings(opts)
00567 
00568         # Set all other attrs (action, type, etc.) from 'attrs' dict
00569         self._set_attrs(attrs)
00570 
00571         # Check all the attributes we just set.  There are lots of
00572         # complicated interdependencies, but luckily they can be farmed
00573         # out to the _check_*() methods listed in CHECK_METHODS -- which
00574         # could be handy for subclasses!  The one thing these all share
00575         # is that they raise OptionError if they discover a problem.
00576         for checker in self.CHECK_METHODS:
00577             checker(self)

Here is the caller graph for this function:


Member Function Documentation

def optparse.Option.__str__ (   self)

Definition at line 745 of file optparse.py.

00745 
00746     def __str__(self):
00747         return "/".join(self._short_opts + self._long_opts)

def optparse.Option._check_action (   self) [private]

Definition at line 627 of file optparse.py.

00627 
00628     def _check_action(self):
00629         if self.action is None:
00630             self.action = "store"
00631         elif self.action not in self.ACTIONS:
00632             raise OptionError("invalid action: %r" % self.action, self)

def optparse.Option._check_callback (   self) [private]

Definition at line 706 of file optparse.py.

00706 
00707     def _check_callback(self):
00708         if self.action == "callback":
00709             if not hasattr(self.callback, '__call__'):
00710                 raise OptionError(
00711                     "callback not callable: %r" % self.callback, self)
00712             if (self.callback_args is not None and
00713                 not isinstance(self.callback_args, tuple)):
00714                 raise OptionError(
00715                     "callback_args, if supplied, must be a tuple: not %r"
00716                     % self.callback_args, self)
00717             if (self.callback_kwargs is not None and
00718                 not isinstance(self.callback_kwargs, dict)):
00719                 raise OptionError(
00720                     "callback_kwargs, if supplied, must be a dict: not %r"
00721                     % self.callback_kwargs, self)
00722         else:
00723             if self.callback is not None:
00724                 raise OptionError(
00725                     "callback supplied (%r) for non-callback option"
00726                     % self.callback, self)
00727             if self.callback_args is not None:
00728                 raise OptionError(
00729                     "callback_args supplied for non-callback option", self)
00730             if self.callback_kwargs is not None:
00731                 raise OptionError(
00732                     "callback_kwargs supplied for non-callback option", self)
00733 

Here is the call graph for this function:

def optparse.Option._check_choice (   self) [private]

Definition at line 663 of file optparse.py.

00663 
00664     def _check_choice(self):
00665         if self.type == "choice":
00666             if self.choices is None:
00667                 raise OptionError(
00668                     "must supply a list of choices for type 'choice'", self)
00669             elif not isinstance(self.choices, (tuple, list)):
00670                 raise OptionError(
00671                     "choices must be a list of strings ('%s' supplied)"
00672                     % str(type(self.choices)).split("'")[1], self)
00673         elif self.choices is not None:
00674             raise OptionError(
00675                 "must not supply choices for type %r" % self.type, self)

Here is the call graph for this function:

def optparse.Option._check_const (   self) [private]

Definition at line 691 of file optparse.py.

00691 
00692     def _check_const(self):
00693         if self.action not in self.CONST_ACTIONS and self.const is not None:
00694             raise OptionError(
00695                 "'const' must not be supplied for action %r" % self.action,
00696                 self)

def optparse.Option._check_dest (   self) [private]

Definition at line 676 of file optparse.py.

00676 
00677     def _check_dest(self):
00678         # No destination given, and we need one for this action.  The
00679         # self.type check is for callbacks that take a value.
00680         takes_value = (self.action in self.STORE_ACTIONS or
00681                        self.type is not None)
00682         if self.dest is None and takes_value:
00683 
00684             # Glean a destination from the first long option string,
00685             # or from the first short option string if no long options.
00686             if self._long_opts:
00687                 # eg. "--foo-bar" -> "foo_bar"
00688                 self.dest = self._long_opts[0][2:].replace('-', '_')
00689             else:
00690                 self.dest = self._short_opts[0][1]

def optparse.Option._check_nargs (   self) [private]

Definition at line 697 of file optparse.py.

00697 
00698     def _check_nargs(self):
00699         if self.action in self.TYPED_ACTIONS:
00700             if self.nargs is None:
00701                 self.nargs = 1
00702         elif self.nargs is not None:
00703             raise OptionError(
00704                 "'nargs' must not be supplied for action %r" % self.action,
00705                 self)

def optparse.Option._check_opt_strings (   self,
  opts 
) [private]

Definition at line 578 of file optparse.py.

00578 
00579     def _check_opt_strings(self, opts):
00580         # Filter out None because early versions of Optik had exactly
00581         # one short option and one long option, either of which
00582         # could be None.
00583         opts = [opt for opt in opts if opt]
00584         if not opts:
00585             raise TypeError("at least one option string must be supplied")
00586         return opts

def optparse.Option._check_type (   self) [private]

Definition at line 633 of file optparse.py.

00633 
00634     def _check_type(self):
00635         if self.type is None:
00636             if self.action in self.ALWAYS_TYPED_ACTIONS:
00637                 if self.choices is not None:
00638                     # The "choices" attribute implies "choice" type.
00639                     self.type = "choice"
00640                 else:
00641                     # No type given?  "string" is the most sensible default.
00642                     self.type = "string"
00643         else:
00644             # Allow type objects or builtin type conversion functions
00645             # (int, str, etc.) as an alternative to their names.  (The
00646             # complicated check of builtins is only necessary for
00647             # Python 2.1 and earlier, and is short-circuited by the
00648             # first check on modern Pythons.)
00649             import builtins
00650             if ( isinstance(self.type, type) or
00651                  (hasattr(self.type, "__name__") and
00652                   getattr(builtins, self.type.__name__, None) is self.type) ):
00653                 self.type = self.type.__name__
00654 
00655             if self.type == "str":
00656                 self.type = "string"
00657 
00658             if self.type not in self.TYPES:
00659                 raise OptionError("invalid option type: %r" % self.type, self)
00660             if self.action not in self.TYPED_ACTIONS:
00661                 raise OptionError(
00662                     "must not supply a type for action %r" % self.action, self)

def optparse.Option._set_attrs (   self,
  attrs 
) [private]

Definition at line 608 of file optparse.py.

00608 
00609     def _set_attrs(self, attrs):
00610         for attr in self.ATTRS:
00611             if attr in attrs:
00612                 setattr(self, attr, attrs[attr])
00613                 del attrs[attr]
00614             else:
00615                 if attr == 'default':
00616                     setattr(self, attr, NO_DEFAULT)
00617                 else:
00618                     setattr(self, attr, None)
00619         if attrs:
00620             attrs = sorted(attrs.keys())
00621             raise OptionError(
00622                 "invalid keyword arguments: %s" % ", ".join(attrs),
00623                 self)
00624 

Here is the call graph for this function:

def optparse.Option._set_opt_strings (   self,
  opts 
) [private]

Definition at line 587 of file optparse.py.

00587 
00588     def _set_opt_strings(self, opts):
00589         for opt in opts:
00590             if len(opt) < 2:
00591                 raise OptionError(
00592                     "invalid option string %r: "
00593                     "must be at least two characters long" % opt, self)
00594             elif len(opt) == 2:
00595                 if not (opt[0] == "-" and opt[1] != "-"):
00596                     raise OptionError(
00597                         "invalid short option string %r: "
00598                         "must be of the form -x, (x any non-dash char)" % opt,
00599                         self)
00600                 self._short_opts.append(opt)
00601             else:
00602                 if not (opt[0:2] == "--" and opt[2] != "-"):
00603                     raise OptionError(
00604                         "invalid long option string %r: "
00605                         "must start with --, followed by non-dash" % opt,
00606                         self)
00607                 self._long_opts.append(opt)

def optparse.Option.check_value (   self,
  opt,
  value 
)

Definition at line 762 of file optparse.py.

00762 
00763     def check_value(self, opt, value):
00764         checker = self.TYPE_CHECKER.get(self.type)
00765         if checker is None:
00766             return value
00767         else:
00768             return checker(self, opt, value)

Here is the call graph for this function:

Here is the caller graph for this function:

def optparse.Option.convert_value (   self,
  opt,
  value 
)

Definition at line 769 of file optparse.py.

00769 
00770     def convert_value(self, opt, value):
00771         if value is not None:
00772             if self.nargs == 1:
00773                 return self.check_value(opt, value)
00774             else:
00775                 return tuple([self.check_value(opt, v) for v in value])

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 753 of file optparse.py.

00753 
00754     def get_opt_string(self):
00755         if self._long_opts:
00756             return self._long_opts[0]
00757         else:
00758             return self._short_opts[0]
00759 

def optparse.Option.process (   self,
  opt,
  value,
  values,
  parser 
)

Definition at line 776 of file optparse.py.

00776 
00777     def process(self, opt, value, values, parser):
00778 
00779         # First, convert the value(s) to the right type.  Howl if any
00780         # value(s) are bogus.
00781         value = self.convert_value(opt, value)
00782 
00783         # And then take whatever action is expected of us.
00784         # This is a separate method to make life easier for
00785         # subclasses to add new actions.
00786         return self.take_action(
00787             self.action, self.dest, opt, value, values, parser)

Here is the call graph for this function:

def optparse.Option.take_action (   self,
  action,
  dest,
  opt,
  value,
  values,
  parser 
)

Reimplemented in test.test_optparse.TestExtendAddActions.MyOption.

Definition at line 788 of file optparse.py.

00788 
00789     def take_action(self, action, dest, opt, value, values, parser):
00790         if action == "store":
00791             setattr(values, dest, value)
00792         elif action == "store_const":
00793             setattr(values, dest, self.const)
00794         elif action == "store_true":
00795             setattr(values, dest, True)
00796         elif action == "store_false":
00797             setattr(values, dest, False)
00798         elif action == "append":
00799             values.ensure_value(dest, []).append(value)
00800         elif action == "append_const":
00801             values.ensure_value(dest, []).append(self.const)
00802         elif action == "count":
00803             setattr(values, dest, values.ensure_value(dest, 0) + 1)
00804         elif action == "callback":
00805             args = self.callback_args or ()
00806             kwargs = self.callback_kwargs or {}
00807             self.callback(self, opt, value, parser, *args, **kwargs)
00808         elif action == "help":
00809             parser.print_help()
00810             parser.exit()
00811         elif action == "version":
00812             parser.print_version()
00813             parser.exit()
00814         else:
00815             raise ValueError("unknown action %r" % self.action)
00816 
00817         return 1
00818 
00819 # class Option
00820 

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 750 of file optparse.py.

00750 
00751     def takes_value(self):
00752         return self.type is not None


Member Data Documentation

optparse.Option.__repr__ = _repr [static, private]

Definition at line 748 of file optparse.py.

Definition at line 563 of file optparse.py.

Definition at line 562 of file optparse.py.

Definition at line 629 of file optparse.py.

tuple optparse.Option.ACTIONS [static]
Initial value:
("store",
               "store_const",
               "store_true",
               "store_false",
               "append",
               "append_const",
               "count",
               "callback",
               "help",
               "version")

Reimplemented in test.test_optparse.TestExtendAddActions.MyOption.

Definition at line 481 of file optparse.py.

Initial value:
("store",
                            "append")

Definition at line 511 of file optparse.py.

list optparse.Option.ATTRS [static]
Initial value:
['action',
             'type',
             'dest',
             'default',
             'nargs',
             'const',
             'choices',
             'callback',
             'callback_args',
             'callback_kwargs',
             'help',
             'metavar']

Definition at line 466 of file optparse.py.

Definition at line 554 of file optparse.py.

Initial value:
[_check_action,
                     _check_type,
                     _check_choice,
                     _check_dest,
                     _check_const,
                     _check_nargs,
                     _check_callback]

Definition at line 734 of file optparse.py.

Initial value:
("store_const",
                     "append_const")

Definition at line 515 of file optparse.py.

Definition at line 687 of file optparse.py.

Definition at line 700 of file optparse.py.

Initial value:
("store",
                     "store_const",
                     "store_true",
                     "store_false",
                     "append",
                     "append_const",
                     "count")

Reimplemented in test.test_optparse.TestExtendAddActions.MyOption.

Definition at line 495 of file optparse.py.

Definition at line 638 of file optparse.py.

Initial value:
{ "int"    : check_builtin,
                     "long"   : check_builtin,
                     "float"  : check_builtin,
                     "complex": check_builtin,
                     "choice" : check_choice,
                   }

Reimplemented in test.test_optparse.TestExtendAddTypes.MyOption, and test.test_optparse.DurationOption.

Definition at line 538 of file optparse.py.

Initial value:
("store",
                     "append",
                     "callback")

Reimplemented in test.test_optparse.TestExtendAddActions.MyOption.

Definition at line 505 of file optparse.py.

tuple optparse.Option.TYPES = ("string", "int", "long", "float", "complex", "choice") [static]

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