common.command.base

Module defining base command class for all possible commands of lmi meta-command.

class lmi.scripts.common.command.base.LmiBaseCommand(app, cmd_name, parent=None)[source]

Abstract base class for all commands handling command line arguemtns. Instances of this class are organized in a tree with root element being the lmi meta-command (if not running in interactive mode). Each such instance can have more child commands if its LmiBaseCommand.is_end_point() method return False. Each has one parent command except for the top level one, whose parent property returns None.

Set of commands is organized in a tree, where each command (except for the root) has its own parent. is_end_point() method distinguish leaves from nodes. The path from root command to the leaf is a sequence of commands passed to command line.

If the LmiBaseCommand.has_own_usage() returns True, the parent command won’t process the whole command line and the remainder will be passed as a second argument to the LmiBaseCommand.run() method.

Parameters:
  • app – Main application object.
  • cmd_name (string) – Name of command.
  • parent (LmiBaseCommand) – Parent command.
app[source]

Return application object.

classmethod child_commands()[source]

Abstract class method returning dictionary of child commands with structure:

{ "command-name" : cmd_factory, ... }

Dictionary contains just a direct children (commands, which may immediately follow this particular command on command line).

cmd_full_name[source]

Name of this subcommand with all prior commands included. It’s the sequence of commands as given on command line up to this subcommand without any options present. In interactive mode this won’t contain the name of binary (sys.argv[0]).

Returns:Concatenation of all preceding commands with cmd_name.
Return type:string
cmd_name[source]

Name of this subcommand as a single word.

cmd_name_args[source]

The same as cmd_full_name, except the result is a list of subcommands.

Returns:List of command strings as given on command line up to this command.
Return type:list
docopt_cmd_name_args[source]

Arguments array for docopt parser. Similar to LmiBaseCommand.cmd_name_args() except for the leading binary name, which is omitted here.

Return type:list
classmethod get_description()[source]

Return description for this command. This is usually a first line of documentation string of a class.

Return type:string
get_usage(proper=False)[source]

Get command usage. Return value of this function is used by docopt parser as usage string. Command tree is traversed upwards until command with defined usage string is found. End point commands (leaves) require manually written usage, so the first command in the sequence of parents with own usage string is obtained and its usage returned. For nodes missing own usage string this can be generated based on its subcommands.

Parameters:proper (boolean) – Says, whether the usage string written manually is required or not. It applies only to node (not a leaf) commands without its own usage string.
classmethod has_own_usage()[source]
Returns:True, if this command has its own usage string, which is returned by LmiBaseCommand.get_description(). Otherwise the parent command must be queried.
Return type:boolean
classmethod is_end_point()[source]
Returns:True, if this command parses the rest of command line and can not have any child subcommands.
Return type:boolean
parent[source]

Return parent command.

run(args)[source]

Handle the command line arguments. If this is not an end point command, it will pass the unhandled arguments to one of it’s child commands. So the arguments are processed recursively by the instances of this class.

Parameters:args (list) – Arguments passed to the command line that were not yet parsed. It’s the contents of sys.argv (if in non-interactive mode) from the current command on.
Returns:Exit code of application. This maybe also be a boolean value or None. None and True are treated as a success causing exit code to be 0.
Return type:integer

Previous topic

common.command

Next topic

common.command.checkresult

This Page