Back to index

python3.2  3.2.2
Classes | Functions | Variables
optparse Namespace Reference

Classes

class  OptParseError
class  OptionError
class  OptionConflictError
class  OptionValueError
class  BadOptionError
class  AmbiguousOptionError
class  HelpFormatter
class  IndentedHelpFormatter
class  TitledHelpFormatter
class  Option
class  Values
class  OptionContainer
class  OptionGroup
class  OptionParser

Functions

def _repr
def gettext
def _parse_num
def _parse_int
def _parse_long
def check_builtin
def check_choice
def _match_abbrev

Variables

string __version__ = "1.5.3"
list __all__
string __copyright__
 _ = gettext
dictionary _builtin_cvt
tuple NO_DEFAULT = ("NO", "DEFAULT")
string SUPPRESS_HELP = "SUPPRESS"
string SUPPRESS_USAGE = "SUPPRESS"
 make_option = Option

Detailed Description

A powerful, extensible, and easy-to-use option parser.

By Greg Ward <gward@python.net>

Originally distributed as Optik.

For support, use the optik-users@lists.sourceforge.net mailing list
(http://lists.sourceforge.net/lists/listinfo/optik-users).

Simple usage example:

   from optparse import OptionParser

   parser = OptionParser()
   parser.add_option("-f", "--file", dest="filename",
                 help="write report to FILE", metavar="FILE")
   parser.add_option("-q", "--quiet",
                 action="store_false", dest="verbose", default=True,
                 help="don't print status messages to stdout")

   (options, args) = parser.parse_args()

Function Documentation

def optparse._match_abbrev (   s,
  wordmap 
) [private]
_match_abbrev(s : string, wordmap : {string : Option}) -> string

Return the string key in 'wordmap' for which 's' is an unambiguous
abbreviation.  If 's' is found to be ambiguous or doesn't match any of
'words', raise BadOptionError.

Definition at line 1655 of file optparse.py.

01655 
01656 def _match_abbrev(s, wordmap):
01657     """_match_abbrev(s : string, wordmap : {string : Option}) -> string
01658 
01659     Return the string key in 'wordmap' for which 's' is an unambiguous
01660     abbreviation.  If 's' is found to be ambiguous or doesn't match any of
01661     'words', raise BadOptionError.
01662     """
01663     # Is there an exact match?
01664     if s in wordmap:
01665         return s
01666     else:
01667         # Isolate all words with s as a prefix.
01668         possibilities = [word for word in wordmap.keys()
01669                          if word.startswith(s)]
01670         # No exact match, so there had better be just one possibility.
01671         if len(possibilities) == 1:
01672             return possibilities[0]
01673         elif not possibilities:
01674             raise BadOptionError(s)
01675         else:
01676             # More than one possible completion: ambiguous prefix.
01677             possibilities.sort()
01678             raise AmbiguousOptionError(s, possibilities)
01679 
01680 
01681 # Some day, there might be many Option classes.  As of Optik 1.3, the
01682 # preferred way to instantiate Options is indirectly, via make_option(),
01683 # which will become a factory function when there are many Option
# classes.

Here is the caller graph for this function:

def optparse._parse_int (   val) [private]

Definition at line 411 of file optparse.py.

00411 
00412 def _parse_int(val):
00413     return _parse_num(val, int)

Here is the call graph for this function:

def optparse._parse_long (   val) [private]

Definition at line 414 of file optparse.py.

00414 
00415 def _parse_long(val):
00416     return _parse_num(val, int)

Here is the call graph for this function:

def optparse._parse_num (   val,
  type 
) [private]

Definition at line 398 of file optparse.py.

00398 
00399 def _parse_num(val, type):
00400     if val[:2].lower() == "0x":         # hexadecimal
00401         radix = 16
00402     elif val[:2].lower() == "0b":       # binary
00403         radix = 2
00404         val = val[2:] or "0"            # have to remove "0b" prefix
00405     elif val[:1] == "0":                # octal
00406         radix = 8
00407     else:                               # decimal
00408         radix = 10
00409 
00410     return type(val, radix)

Here is the caller graph for this function:

def optparse._repr (   self) [private]

Definition at line 78 of file optparse.py.

00078 
00079 def _repr(self):
00080     return "<%s at 0x%x: %s>" % (self.__class__.__name__, id(self), self)
00081 
00082 
00083 # This file was generated from:
00084 #   Id: option_parser.py 527 2006-07-23 15:21:30Z greg
00085 #   Id: option.py 522 2006-06-11 16:22:03Z gward
00086 #   Id: help.py 527 2006-07-23 15:21:30Z greg
00087 #   Id: errors.py 509 2006-04-20 00:58:24Z gward
00088 
00089 try:
    from gettext import gettext

Here is the caller graph for this function:

def optparse.check_builtin (   option,
  opt,
  value 
)

Definition at line 422 of file optparse.py.

00422 
00423 def check_builtin(option, opt, value):
00424     (cvt, what) = _builtin_cvt[option.type]
00425     try:
00426         return cvt(value)
00427     except ValueError:
00428         raise OptionValueError(
00429             _("option %s: invalid %s value: %r") % (opt, what, value))

def optparse.check_choice (   option,
  opt,
  value 
)

Definition at line 430 of file optparse.py.

00430 
00431 def check_choice(option, opt, value):
00432     if value in option.choices:
00433         return value
00434     else:
00435         choices = ", ".join(map(repr, option.choices))
00436         raise OptionValueError(
00437             _("option %s: invalid choice: %r (choose from %s)")
00438             % (opt, value, choices))
00439 
00440 # Not supplying a default is different from a default of None,
# so we need an explicit "not supplied" value.
def optparse.gettext (   message)

Definition at line 91 of file optparse.py.

00091 
00092     def gettext(message):
        return message

Variable Documentation

Definition at line 93 of file optparse.py.

Initial value:
00001 ['Option',
00002            'make_option',
00003            'SUPPRESS_HELP',
00004            'SUPPRESS_USAGE',
00005            'Values',
00006            'OptionContainer',
00007            'OptionGroup',
00008            'OptionParser',
00009            'HelpFormatter',
00010            'IndentedHelpFormatter',
00011            'TitledHelpFormatter',
00012            'OptParseError',
00013            'OptionError',
00014            'OptionConflictError',
00015            'OptionValueError',
00016            'BadOptionError']

Definition at line 26 of file optparse.py.

Definition at line 43 of file optparse.py.

Definition at line 24 of file optparse.py.

Initial value:
00001 { "int" : (_parse_int, _("integer")),
00002                  "long" : (_parse_long, _("long integer")),
00003                  "float" : (float, _("floating-point")),
00004                  "complex" : (complex, _("complex")) }

Definition at line 417 of file optparse.py.

Definition at line 1684 of file optparse.py.

tuple optparse.NO_DEFAULT = ("NO", "DEFAULT")

Definition at line 441 of file optparse.py.

Definition at line 821 of file optparse.py.

Definition at line 822 of file optparse.py.