Commands

class nutcli.commands.Actor(parent=None, cli_args=None, shell=None, logger=None)

Bases: object

The command actor. It defines the handler that is executed either via the command line or through other actors when re-using the code.

Example usage
class ExampleActor(Actor):
    def setup_parser(parser):
        parser.add_argument('--hello', action='store', type=str)

    def __call__(self, hello):
        self.info(hello)
Parameters
  • parent (Actor, optional) – Parent actor to inherit settings from, defaults to None

  • cli_args (list of any, optional) – Override cli arguments, defaults to None

  • shell (Shell, optional) – Override shell, defaults to None

  • logger (logger, optional) – Override logger, defaults to None

Variables
  • cli_args – All arguments passed to the command line.

  • shell – Instance of nutcli.shell.Shell ready to be used in the actor.

setup_parser(parser)

Setup argument parser.

You should override this in your actor if it accepts any arguments.

Parameters

parser (argparse.Parser) – You can add arguments to this parser.

debug(msg, *args, **kwargs)

Log a debug message.

Parameters

msg (str) – The message.

info(msg, *args, **kwargs)

Log an information message.

Parameters

msg (str) – The message.

warning(msg, *args, **kwargs)

Log a warning message.

Parameters

msg (str) – The message.

error(msg, *args, **kwargs)

Log an error message.

Parameters

msg (str) – The message.

critical(msg, *args, **kwargs)

Log a critical message.

Parameters

msg (str) – The message.

__call__()

Command handler.

You should override this in your actor.

Raises

NotImplementedError – The error is risen if this method is not overridden.

class nutcli.commands.SubcommandsActor(*args, **kwargs)

Bases: nutcli.commands.Actor

This is a special actor that can provide additional subcommands.

You can use this to reuse code for your cli when you want to use the same actors but need to instantiate them with different parameters.

Example usage
class ExampleActor(SubcommandsActor):
    def __init__(self, extra, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.extra = extra

    def get_commands(self):
        return [
            Command('ex1', 'Description')(Example1Actor(self.extra)),
            Command('ex2', 'Description')(Example2Actor(self.extra)),
        ]
Parameters
  • parent (Actor, optional) – Parent actor to inherit settings from, defaults to None

  • cli_args (list of any, optional) – Override cli arguments, defaults to None

  • shell (Shell, optional) – Override shell, defaults to None

  • logger (logger, optional) – Override logger, defaults to None

Variables
  • cli_args – All arguments passed to the command line.

  • shell – Instance of nutcli.shell.Shell ready to be used in the actor.

get_commands()

Return list of commands.

Returns

List of commands.

Return type

list of (Command or CommandGroup)

__call__()

Command handler.

You should override this in your actor.

Raises

NotImplementedError – The error is risen if this method is not overridden.

class nutcli.commands.Command(name, help_message, handler, **add_parser_kwargs)

Bases: object

Command that can be run through CLI.

Example usage - Command takes an Actor
Command('command-name', 'description', Actor())
Example usage - Command takes CommandParser
Command('nested-command', 'Description', CommandParser()([
    Command('command-name', 'Description', Actor()),
]))
Parameters
  • name (str) – CLI command name. The command is executed if the name is provided in CLI arguments.

  • help_message (str) – Help message visible in usage description.

  • handler (Actor) – Actor that is executed when the command is run.

  • **add_parser_kwargs (dict of str:any) – Additional keyword arguments passed to argparse.Parser.add_parser

Note

The default formatter class passed to argparse.Parser.add_parser is argparse.RawTextHelpFormatter. You can override it with formatter_class option. E.g. formatter_class=argparse.ArgumentDefaultsHelpFormatter.

setup_parser(parent_parser)

Add new command parser to the parent parser.

Parameters

parent_parser (argparse.Parser) – Parent parser.

class nutcli.commands.CommandParser(title=None, metavar='COMMANDS', **kwargs)

Bases: object

Comand parser.

This is the root element for CLI command list.

Example usage
parser = argparse.ArgumentParser()

CommandParser('Test Commands')([
    Command('ex1', 'Example 1', Actor()),
    CommandGroup('Below are nested commands')([
        Command('nested', 'Commands can be nested', CommandParser()([
            Command('test2', 'Example 2', Actor()),
        ]))
    ])
]).setup_parser(parser)

# ->
# Test Commands:
# COMMANDS
#    test1               Example 1
#    Below are nested commands
#      nested            Commands can be nested

It will setup the argument parser and make sure that all command handlers are set correctly.

Parameters
  • title (str, optional) – Title visible in help message, defaults to None

  • metavar (str, optional) – Metavar name used for positional argument, defaults to ‘COMMANDS’

setup_parser(parent_parser)

Setup command parsers on the parent parser.

Parameters

parent_parser (argparse.Parser) – Parent parser.

Returns

Command subparser.

Return type

argparse.Parser

__call__(commands)

Set command list.

Parameters

commands (list of (Command or CommandGroup)) – List of commands to handle.

Returns

Self.

Return type

CommandParser

class nutcli.commands.CommandGroup(title, *args, **kwargs)

Bases: nutcli.commands.CommandParser

Adding commands to a command group will group them together under common descriptions.

See CommandParser for example usage.

Parameters

title (str) – Group title

setup_parser(parent_parser)

Setup command parsers on the parent parser.

Parameters

parent_parser (argparse.Parser) – Parent parser.

Returns

Command subparser.

Return type

argparse.Parser