monolith.cli

class monolith.cli.Parser(*args, **kwargs)

Subclass of argparse.ArgumentParser providing more control over output stream.

SimpleExecutionManager

class monolith.cli.SimpleExecutionManager(program, commands)
Parameters:
  • program – name of the program under which commands would be executed (usually name of the program).
  • commands

    dictionary mapping subcommands to proper command classes. Values can be string - in that case proper command class would be importer and used. Example:

    {
        'subcommand1': SomeCommand,
        'subcommand2': 'myprogram.commands.another.AnotherCommand',
    }
    
get_commands_to_register()

Returns dictionary with commands given during construction. If value is a string, it would be converted into proper class pointer.

ExecutionManager

class monolith.cli.ExecutionManager(argv=None, stderr=None, stdout=None)
autocomplete()

If completion is enabled, this method would write to self.stdout completion words separated with space.

call_command(cmd, *argv)

Runs a command.

Parameters:
  • cmd – command to run (key at the registry)
  • argv – arguments that would be passed to the command
execute(argv=None)

Executes command based on given arguments.

get_commands()

Returns commands stored in the registry (sorted by name).

get_commands_to_register()

Returns dictionary (name / Command or string pointing at the command class.

get_parser()

Returns monolith.cli.Parser instance for this ExecutionManager.

get_usage()

Returns usage text of the main application parser.

parser_cls

alias of Parser

register(name, Command, force=False)

Registers given Command (as given name) at this ExecutionManager‘s registry.

Parameters:
  • name – name in the registry under which given Command should be stored.
  • Command – should be subclass of :class:monolith.cli.base.BaseCommand
  • force – Forces registration if set to True - even if another command was already registered, it would be overridden and no execption would be raised. Defaults to False.
Raises AlreadyRegistered:
 

If another command was already registered under given name.

BaseCommand

class monolith.cli.BaseCommand(prog_name=None, stdout=None)

Base command class that should be subclassed by concrete commands.

Attributes

  • help: Help description for this command. Defaults to empty string.
  • args: List of Argument instances. Defaults to empty list.
  • prog_name: Program name of ExecutionManager within which this command is run. Defaults to None.
  • stdout: File-like object. Command should write to it. Defaults to sys.stdout.
get_args()

Returns list of Argument instances for the parser. By default, it returns self.args.

handle(namespace)

Handles given namespace and executes command. Should be overridden at subclass.

post_register(manager)

Performs actions once this command is registered within given manager. By default it does nothing.

setup_parser(parser, cmdparser)

This would be called when command is registered by ExecutionManager after arguments from get_args are processed.

Default implementation does nothing.

Parameters:
  • parser – Global argparser.ArgumentParser
  • cmdparser – Subparser related with this command

LabelCommand

class monolith.cli.LabelCommand(prog_name=None, stdout=None)

Command that works on given position arguments (labels). By default, at least one label is required. This is controlled by labels_required attribute.

Extra attributes:

  • labels_required: If True, at least one label is required, otherwise no positional arguments could be given. Defaults to True.
get_labels_arg()

Returns argument for labels.

handle(namespace)

Handles given namespace by calling handle_label method for each given label.

handle_label(label, namespace)

Handles single label. Should be overridden at subclass.

handle_no_labels(namespace)

Performs some action if no lables were given. By default it does nothing.

SingleLabelCommand

class monolith.cli.SingleLabelCommand(prog_name=None, stdout=None)

Command that works on given positional argument (label).

Extra arguments:

  • label_default_value: If no label were given, this would be default value that would be passed to namespace. Defaults to None.
get_label_arg()

Returns argument for label.

handle(namespace)

Calls handle_label method for given label.

handle_label(label, namespace)

Handles label. Should be overridden at subclass.

Read the Docs v: latest
Versions
latest
Downloads
PDF
HTML
Epub
On Read the Docs
Project Home
Builds

Free document hosting provided by Read the Docs.