Package lyntin :: Module argparser :: Class ArgumentParser
[show private | hide private]
[frames | no frames]

Class ArgumentParser


This is the actual ArgumentParser class. Each command gets its own ArgumentParser. It handles taking in user input and parsing it into a series of arguments which are then placed in a dict and passed to the command function. This centralizes parsing user input into one place and also enables us to centralize type checking, argument checking, and conversion in one place as well.
Method Summary
  __init__(self, argspec, argoptions)
Initializes the ArgParser instance so it can then parse user input for arguments.
  buildOptions(self, argoptions)
Set the options for this ArgumentParser key=value values in argoptions get put directly into self.options other values in argoptions get set to 1 in self.options.
  buildParsers(self, argspec)
Build up the set of parsers to be used for argument parsing.
varies (string or int) getOption(self, optionname)
Gets a specific option from the arg options hash as passed into the __init__.
dict parse(self, input, defaultresolver)
Takes an input string and produces the populated dictionary matching self's argspec.
list of 2-length-tuples split(self, input, maxsplit, buildsyntaxline)
Take an input string and tokenizes it into a list of pairs.

Method Details

__init__(self, argspec, argoptions=None)
(Constructor)

Initializes the ArgParser instance so it can then parse user input for arguments. It also generates the syntaxline which is used by the help system to tell the user what the command syntax is.
Parameters:
argspec - the argument specification for the command
           (type=string)
argoptions - the options to use to parse the argspec
           (type=string)

buildOptions(self, argoptions)

Set the options for this ArgumentParser key=value values in argoptions get put directly into self.options other values in argoptions get set to 1 in self.options.

example 1:
 argoptions="ignorall loud:boolean=true"
 self.options={"ignorall":1,"loud":1}
example 2:
 argoptions="lalala wewewewe=hahaha"
 self.options={"lalala":1,"wewewewe"="hahaha")
Parameters:
argoptions - the argoptions as passed into __init__.
           (type=string)

buildParsers(self, argspec)

Build up the set of parsers to be used for argument parsing.

The argspec follows the following format:
  1. [argname[:argtype]]+
  2. [argname[:argtype]=defaultval]+
  3. [argname:argtype*]
  4. [argname[:argtype]]+
  5. [argname[:argtype]=defaultval]+
  6. [argname:argtype**]

Any of the arguments can be specified either by name or populated by position, except for arguments after the index collector argument. Those must be specified by name only.

Once one default value is given all further arguments must have default values (except collector arguments, which have implicit default arguments of the empty list and the empty map).

For examples see the test code at the end of argparser.py.
Parameters:
argspec - the argument specification to use to parse future user input
           (type=string)

getOption(self, optionname)

Gets a specific option from the arg options hash as passed into the __init__.
Parameters:
optionname - the name of the option
           (type=string)
Returns:
the value of the option or None
           (type=varies (string or int))

parse(self, input, defaultresolver=None)

Takes an input string and produces the populated dictionary matching self's argspec.
Parameters:
input - the user input string
           (type=string)
defaultresolver - A function that will take an argument name and return a default value (None if there should be none) to override builtin defaults.
           (type=function)
Returns:
the populated dictionary of all the args and values
           (type=dict)
Raises:
ParserException - if extra arguments are encountered without appropriate collection arguments specified, or if required arguments are missing, or if arguments passed in aren't valid

split(self, input, maxsplit=-1, buildsyntaxline=0)

Take an input string and tokenizes it into a list of pairs. Tokens with equal signs come back as (key,value) pairs, those without come back as (argument,None)

{}s are treated like quotes, and everything between the {}s is ignored.

Any amount of white space between arguments is ignored. (No empty arguments are returned.)

"\" (single backslash) escapes anything, including =, {, }, \ (single backslash), and any character, so "\a" (single backslash then a) becomes "a" in the argument, and "\n\o\t\a\d\r\a\g\o\n" (a series of single backslashes followed by a letter) is the same as "notadragon".

After maxplit arguments are parsed (all of the arguments if maxsplit < 0), we'll stop and return the rest of input as the final item.
Parameters:
input - the user input to tokenize
           (type=string)
maxsplit - the maximum number of pairs to split
           (type=int (-1 if no maxplit))
buildsyntaxline - whether or not to build the syntax line as we're going along (this method is used both when the ArgParser is initialized as well as for parsing user input)
           (type=int (0 or 1))
Returns:
the split input
           (type=list of 2-length-tuples)
Raises:
ParserException - if \ (single backslash) is found at the end of the line or if mismatched { or } are found

Generated by Epydoc 2.1 on Mon Aug 9 09:17:41 2004 http://epydoc.sf.net