Skip to content
Renaud Guillard edited this page Jul 3, 2013 · 7 revisions

Python command line parser API

Program module

parse

res = program.parse(*argv*)

//##argv##//
Command line argument array. This should be `sys.argv[1:len(sys.argv)]`
Return value
`ParserResult`

usage

print program.usage(*parser_result*, *usage_flags*)

//##parser_result##//
The `ParserResult` returned by `Program.parse` or `None`
//##usage_flags##//
A combination of the `Program.Usage` flags
* `Program.Usage.Inline` One-line * `Program.Usage.Abstract` * `Program.Usage.Details` * `Program.Usage.AllInfos` * `Program.Usage.Raw`
Return value
A string

Parser result

ParserResult

A ParserResult instance is returned by the Program.parse() function

Attributes Type Description
`is_valid` Boolean `True` if no errors were reported
`options` Dynamic object Each attribute of this object represents the value of a program option. Attributes name refers to the value of `<databinding><variable /><databinding/>` node in the program interface XML definition file. Depending on the option type, the associated value can be a boolean value, a string, an array of string or a `GroupOptionResult`
`subcommand` `SubcommandResult` or `None` Contains informations on the selected subcommand (if any)
`values` Array of strings Array of all positional arguments (command line arguments which are not option names, option arguments nor subcommand names)
`issues` Dictionnary Contains all messages generated by the parser, grouped by severity. Keys: `debug`, `notices`, `warnings`, `errors`. Each key contains an array of strings.

GroupOptionResult

Option value for all option groups.

Attributes Type Description
`is_set` Boolean `True` if at least one of the group sub-options was present (and valid) on the command line
`options` Dynamic object A subset of the `ParserResult.options` with only the group sub-options.

Exclusive groups adds the following attributes

Attributes Type Description
`selected_option` *Mixed* A reference to the value of the selected option (or `None` if none was selected)
`selected_option_name` String The name of the selected option (an attribute name of `GroupOptionResult.options`)

SubcommandResult

Attributes Type Description
`name` String Subcommand name as describe by the `<name />` node of the program interface XML definition file
`options` Dynamic object Like `ParserResult.options`, represents the subcommand options value(s)

Tweaking

Code snippets


The program interface definition framework

Clone this wiki locally