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.
Methods
|
|
BuildGetoptList
BuildGetoptString
CheckOptions
GetBasicHelp
GetCommandHelp
GetOptionsHelp
ParseCommandLine
__init__
|
|
BuildGetoptList
|
BuildGetoptList ( self, options )
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
|
BuildGetoptString ( self, options )
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.
|
|
CheckOptions
|
CheckOptions ( self, options )
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.
Exceptions
|
|
ValueError, "long option must be specified for -%s" % short_option
ValueError, "short option must have exactly 1 character"
|
|
|
GetBasicHelp
|
GetBasicHelp ( self )
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
|
GetCommandHelp ( self, command )
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.
|
|
GetOptionsHelp
|
GetOptionsHelp ( self, options )
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.
|
|
ParseCommandLine
|
ParseCommandLine ( self, argv )
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.
Exceptions
|
|
CommandError, "%s: %s" %( command, msg )
CommandError, msg
CommandError, qm.error( "unrecognized command", command = command )
qm.cmdline.CommandError, qm.error("conflicting options", option1 = matches [ 0 ], option2 = matches [ 1 ] )
|
|
|
__init__
|
__init__ (
self,
name,
options,
commands,
conflicting_options=(),
)
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.
Exceptions
|
|
ValueError, "duplicate long command option --%s" % option [ 1 ]
ValueError, "duplicate short command option -%s" % option [ 0 ]
ValueError, "unknown option --%s in conflict set", option [ 1 ]
|
|
|