Package qm :: Module cmdline :: Class CommandParser
[hide private]
[frames] | no frames]

Class CommandParser

source code

Class for the functionality that parses the command line.

The command parser is used to easily specify a list of command line options and commands to be parsed from an argument list.

Instance Methods [hide private]
 
__init__(self, name, options, commands, conflicting_options=())
Create a new command parser.
source code
 
CheckOptions(self, options)
Check that a list of options 4-tuples is correct.
source code
 
BuildGetoptList(self, options)
Build a getopt list for the long options.
source code
 
BuildGetoptString(self, options)
Build a getopt string for the options passed in.
source code
 
GetOptionsHelp(self, options)
Return a string that is the basic help for options.
source code
 
GetBasicHelp(self)
Return a string that is the basic help for the commands.
source code
 
GetCommandHelp(self, command)
Return a string that is the help for a specific command.
source code
 
ParseCommandLine(self, argv)
Parse a command line.
source code
Method Details [hide private]

__init__(self, name, options, commands, conflicting_options=())
(Constructor)

source code 
Create a new command parser.

'name' -- The name of the executable that we are currently
using.  This will normally be argv[0].

'options' -- A list of 4-tuples specifying options that you wish
this parser to accept.  The 4-tuple has the following form:
(short_form, long_form, options, description).  'short_form'
must be exactly one character.  'long_form' must be specified
for every option in the list.  'arg_name' is a string
representing the name of the argument that is passed to this
option.  If it is 'None,' then this option doesn't take an
argument.  'description' is a string describing the option.

'commands' -- A list of 5-tuples specifying commands to be
accepted after the command line options.  The 5-tuple has the
form '(name, short_description, args_string, long_description,
options)'.

  'name' -- The string for the command.

  'short_description' -- A short description of the command to
  be printed out in general help.

  'args_string' -- The string that will be printed after the
  command in the command specific help.

  'long_description' -- The long description to be printed out
  in the command specfic help.

  'options' -- A list of 4-tuples of the same form as the
  'options' described above.

'conflicting_options' -- A sequence of sets of conflicting
options.  Each element is a sequence of option specifiers in the
same form as 'options', above.

CheckOptions(self, options)

source code 

Check that a list of options 4-tuples is correct.

'options' -- A list of 4-tuples as described above.

returns -- 1 if the options are all valid, 0 otherwise.

BuildGetoptList(self, options)

source code 

Build a getopt list for the long options.

'options' -- A list of 4-tuples as described above.

returns -- A list to be passed to getopt to parse long options.

BuildGetoptString(self, options)

source code 

Build a getopt string for the options passed in.

'options' -- A list of 4-tuples as described above.

returns -- A string to be passed to getopt to parse the options.

GetOptionsHelp(self, options)

source code 

Return a string that is the basic help for options.

options -- A list of options to get the help string for.

returns -- A string to be printed for the options.

GetBasicHelp(self)

source code 

Return a string that is the basic help for the commands.

returns -- A string to be printed with basic functionality of arguments and commands.

GetCommandHelp(self, command)

source code 

Return a string that is the help for a specific command.

command -- A string of the command that you want help for.

returns -- A string of help for a given command.

ParseCommandLine(self, argv)

source code 

Parse a command line.

'argv' -- A string containing the command line starting with argv[1]. It should not contain the name of the executed program.

returns -- A 4-tuple of the options given, the command given, the command options, and the command arguments. Its form is this: (options, command, command_options, command_args). 'options' is a list of 2-tuples indicating each option specified and the argument given to that option (if applicable). 'command' is the command given. 'command_options' is a list of 2-tuples indicating each option given to the command and its possible argument. 'command_args' is a list of arguments as given to the command. If no command is given, then the function will return '' for the command, [] for the arguments, and [] for the command options.

raises -- 'CommandError' if the command is invalid.