cli2 Python API Documentation

Command

class cli2.command.Command(target, name=None, color=None, doc=None, posix=False, outfile=None, log=True)[source]

Represents a command bound to a target callable.

call(*args, **kwargs)[source]

Execute command target with bound arguments.

help(error=None, short=False)[source]

Show help for a command.

parse(*argv)[source]

Parse arguments into BoundArguments.

setargs()[source]

Reset arguments.

Group

class cli2.group.Group(name=None, doc=None, color=None, posix=False, outfile=None, cmdclass=None, log=True)[source]

Represents a group of named commands.

add(target, *args, **kwargs)[source]

Add a new target as sub-command.

cmd(*args, **kwargs)[source]

Decorator to add a command with optionnal overrides.

group(name, **kwargs)[source]

Return a new sub-group.

help(*args, error=None, short=False)[source]

Get help for a command or group.

Parameters:
  • args – Command or sub-command chain to show help for.
  • error – Error message to print out.
  • short – Show short documentation.
load(obj, parent=None, public=True)[source]

Load a Python object callables into sub-commands.

Argument

class cli2.argument.Argument(cmd, param, doc=None, color=None, **kwargs)[source]

Class representing a bound parameter and command line argument.

accepts

Return True if this argument still accepts values to bind.

aliasmatch(arg)[source]

Return True if the CLI arg matches an alias of this argument.

cast(value)[source]

Cast a string argument from the CLI into a Python object.

help()[source]

Render help for this argument.

iskw

Return True if this argument is not positional.

match(arg)[source]

Return the value extracted from a matching CLI argument.

take(arg)[source]

Return False if it doesn’t accept this arg, otherwise bind it.

value

Return the value bound to this argument.

Decorators

cli2.decorators.arg(name, **kwargs)[source]

Set the overrides for an argument.

cli2.decorators.cmd(**overrides)[source]

Set the overrides for a command.

Node

class cli2.node.Node(name, target, module=None)[source]

A Python node, used to browse python module programatically for callables.

callables

Return the list of callables in this Node.

classmethod factory(name)[source]

Return a Node based on a string dotted python path.

Examples:

Node.factory('your.module')
Node.factory('your.module.your_object')
Node.factory('your.module.your_object.some_attribute')

Test

cli2.test.autotest(path, cmd, ignore=None, env=None)[source]

The autowriting test pattern, minimal for testing cli2 scripts.

Example:

from cli2.test import autotest
autotest(
    'tests/djcli_save_user.txt',
    'djcli save auth.User username="test"',
)