Package pology :: Module subcmd :: Class ParamParser

Class ParamParser

object --+
         |
        ParamParser

Parser for subcommand parameters.

Instance Methods
 
__init__(self)
Constructor.
SubcmdView
add_subcmd(self, subcmd, desc=None)
Add a subcommand for which the parameters may be added afterwards.
SubcmdView
get_view(self, subcmd)
The view into previously defined subcommand.
string
help(self, subcmds=None, wcol=None, stream=sys.stdout)
Formatted help for subcommands.
string
listcmd(self, subcmds=None, wcol=None, stream=sys.stdout)
Formatted listing of subcommands with short descriptions.
[string]
cmdnames(self)
Get the list of all defined subcommands by name.
[SubcmdView]
cmdviews(self)
Get the list of all defined subcommand views.
dict of objects by subcommand name and list of strings
parse(self, rawpars, subcmds)
Parse the list of parameters collected from the command line.

Inherited from object: __delattr__, __format__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __repr__, __setattr__, __sizeof__, __str__, __subclasshook__

Properties

Inherited from object: __class__

Method Details

__init__(self)
(Constructor)

 

Constructor.

Overrides: object.__init__

add_subcmd(self, subcmd, desc=None)

 

Add a subcommand for which the parameters may be added afterwards.

Use double-newline in the description for splitting into paragraphs. The description can also be set later, using set_desc method of subcommand view.

Parameters:
  • subcmd (string) - subcommand name
  • desc (string or None) - description of the subcommand
Returns: SubcmdView
subcommand view

get_view(self, subcmd)

 

The view into previously defined subcommand.

Parameters:
  • subcmd (string) - subcommand name
Returns: SubcmdView
subcommand view

help(self, subcmds=None, wcol=None, stream=sys.stdout)

 

Formatted help for subcommands.

Parameters:
  • subcmds (list of strings) - subcommand names (all subcommands if None)
  • wcol (int) - column to wrap text at (<= 0 for no wrapping, None for automatic according to output stream)
  • stream (file) - intended output stream for the text
Returns: string
formatted help

listcmd(self, subcmds=None, wcol=None, stream=sys.stdout)

 

Formatted listing of subcommands with short descriptions.

Parameters:
  • subcmds (list of strings) - subcommand names (all subcommands if None)
  • wcol (int) - column to wrap text at (<= 0 for no wrapping, None for automatic according to output stream)
  • stream (file) - intended output stream for the text
Returns: string
formatted listing

cmdnames(self)

 

Get the list of all defined subcommands by name.

Returns: [string]
list of subcommands

cmdviews(self)

 

Get the list of all defined subcommand views.

Returns: [SubcmdView]
list of subcommand views

parse(self, rawpars, subcmds)

 

Parse the list of parameters collected from the command line.

If the command line had parameters specified as:

   -sfoo -sbar:xyz -sbaz:10

then the function call should get the list:

   rawpars=['foo', 'bar:xyz', 'baz:10']

Result of parsing will be a dictionary of objects by subcommand name, where each object has attributes named like subcommand parameters. If attribute name has not been explicitly defined for a parameter, its parameter name will be used; if not a valid identifier by itself, it will be normalized by replacing all troublesome characters with an underscore, collapsing contiguous underscore sequences to a single underscore, and prepending an 'x' if it does not start with a letter.

If a parameter is parsed which is not accepted by any of the given subcommands, its name is added to list of non-accepted parameters, which is the second element of the return tuple.

Parameters:
  • rawpars (list of strings) - raw parameters
  • subcmds (list of strings) - names of issued subcommands
Returns: dict of objects by subcommand name and list of strings
objects with parameters as attributes, and list of parameter names not accepted by any of subcommands