Table of Contents

Class: Options Twisted-0.19.0/twisted/python/usage.py

optFlags and optParameters are lists of available parameters which your program can handle. The difference between the two is the flags have an on(1) or off(0) state (off by default) whereas parameters have an assigned value, with an optional default. (Compare --verbose and --verbosity=2)

optFlags is assigned a list of lists. Each list represents a flag parameter, as so:

| optFlags = [[verbose, v, 'Makes it tell you what it doing.'], | [quiet, q, 'Be vewy vewy quiet.']]

As you can see, the first item is the long option name (prefixed with -- on the command line), followed by the short option name (prefixed with -), and the description. The description is used for the in-built handling of the --help switch, which prints a usage summary.

optParameters is much the same, except the list also contains a default value: | optParameters = [[outfile, O, 'outfile.log, 'Description...']]

If you want to handle your own options, define a method named opt_paramname that takes (self, option) as arguments. option will be whatever immediately follows the parameter on the command line. Options fully supports the mapping interface, so you can do things like self["option"] = val in these methods.

Advanced functionality is covered in the howto documentation, available at http://twistedmatrix.com/documents/howto/options, or doc/howto/options.html in you Twisted directory.

Base Classes   
UserDict.UserDict
Methods   
__getattr__
__hash__
__init__
__str__
_gather_flags
_gather_handlers
_gather_parameters
_generic_flag
_generic_parameter
opt_help
parseArgs
parseOptions
postOptions
  __getattr__ 
__getattr__ ( self,  attr )

I make sure that old style optObj.option access still works.

Exceptions   
AttributeError( 'Options instance has no attribute data: You probably forgot to call Options.__init__ from your subclass.' )
AttributeError("%s instance has no attribute '%s'" %( self.__class__, attr ) )
  __hash__ 
__hash__ ( self )

  __init__ 
__init__ ( self )

  __str__ 
__str__ ( self,  width=None )

  _gather_flags 
_gather_flags ( self )

Gather up boolean (flag) options.

Exceptions   
ValueError, "A flag cannot be without a name."
  _gather_handlers 
_gather_handlers ( self )

Gather up options with their own handler methods.

Exceptions   
UsageError( 'Invalid Option function for %s' % name )
  _gather_parameters 
_gather_parameters ( self )

Gather options which take a value.

Exceptions   
ValueError, "A parameter cannot be without a name."
  _generic_flag 
_generic_flag (
        self,
        flagName,
        value=None,
        )

Exceptions   
UsageError, ("Flag '%s' takes no argument." " Not even \"%s\"." %( flagName, value ) )
  _generic_parameter 
_generic_parameter (
        self,
        parameterName,
        value,
        )

Exceptions   
UsageError, ("Parameter '%s' requires an argument." %( parameterName, ) )
  opt_help 
opt_help ( self )

  parseArgs 
parseArgs ( self )

I am called with any leftover arguments which were not options.

Override me to do something with the remaining arguments on the command line, those which were not flags or options. e.g. interpret them as a list of files to operate on.

Note that if there more arguments on the command line than this method accepts, parseArgs will blow up with a getopt.error. This means if you don't override me, parseArgs will blow up if I am passed any arguments at all!

  parseOptions 
parseOptions ( self,  options=None )

The guts of the command-line parser.

Exceptions   
UsageError( "Wrong number of arguments." )
UsageError, "No such option '%s'" %( opt, )
UsageError, e
  postOptions 
postOptions ( self )

I am called after the options are parsed.

Override this method in your subclass to do something after the options have been parsed and assigned, like validate that all options are sane.


Table of Contents

This document was automatically generated on Sun Jul 14 19:53:11 2002 by HappyDoc version 2.0