friendlyshell.command_parsers module

Pre-defined command line parsers supported by Friendly Shell APIs

All Friendly Shell command lines are expected to begin with a command name, followed by 0 or more input parameters as shown below:

<command_token>[<parameter_tokens>]

The command portion of each line must map command names to Python class methods that are prefixed with the characters “do_” (ie: “do_exit()”). Because of this commands must meet the same naming restrictions as Python class methods. Thus most Friendly Shells line parsers will want to leverage the meth default_command_parser function to parse the first token of their command lines. Also, for convenience all command token parsers are expected to return a named token with the label ‘command’ associated with it.

The optional list of parameter tokens may satisfy whatever restrictions are desired by a particular command. Since they need not be mapped to Python class methods they do not need to be bound by the same naming rules as the command token. Finally, the list of input parameters - when provided - are expected to return a named token with the label ‘params’, which will define a list of 1 or more input parameters to be provided to the command.

Below you will find some helper functions that provide pre-built grammars for some common command and parameter token styles so users of the library need not always write their own.

friendlyshell.command_parsers.default_command_token()[source]

token parser that satisfies the requirements of the FShell interpreter

Meets the default naming requirements of the Friendly Shell interpreter, ensuring that command names may be safely mapped to Python class methods. The resulting token will be labelled ‘command’ as required by the Friendly Shell APIs.

Return type:pyparsing.Parser
friendlyshell.command_parsers.default_line_parser()[source]

Gets the default command line parser used by the Friendly Shell APIs

Return type:pyparsing.Parser
friendlyshell.command_parsers.quoted_space_sep_params_token()[source]

Gets a token parser that supports

This parser expects all commands to take the following form:
<command_name>[ <optional_param1> <optional_param2>…]<eol>

The <command_name> maps to a method of this class or a descendent of it with the same name but with a prefix of “do_”. As a result tokens for commands must adhere to the same restrictions as a typical Python class method.

Commands may optionally accept parameters if desired. Parameters are provided on the same line as the command and are separated by spaces. Each parameter may use any printable character since they are translated to Python strings during translation and thus need not be restricted to the same criteria as commands. Also, if a parameter needs to contain embedded spaces then it must be wrapped in quotation marks. Single or double quotes will work. Further, if you need to embed quotation marks within your string simply wrap the inner quotes with outer quotes of the opposite style.

The command token can be accessed by named attribute ‘command’, and the list of any parameters provided on the line can be acessed by the named attribute ‘params’.

TODO: Add support for escaping quote characters when embedded in strings delimited with the same quote style, as in “”hello” world”

Return type:pyparsing.Parser