xoutil.cli
– Command line application facilities¶
Define tools for command-line interface (CLI) applications.
CLi is a means of interaction with a computer program where the user (or client) issues commands to the program in the form of successive lines of text (command lines).
New in version 1.4.1.
-
class
xoutil.cli.
Command
[source]¶ A command base registry.
There are several methods to register new commands:
- Inheriting from this class
- Using the ABC mechanism of register virtual subclasses.
- Registering a class with the method “__commands__” defined.
If the method “__commands__” is used, it must be a class or static method.
Command names are calculated as class names in lower case inserting a hyphen before each new capital letter. For example “MyCommand” will be used as “my-command”.
Each command could include its own argument parser, but it isn’t used automatically, all arguments will be passed as a single parameter to
run()
removing the command when obtained from “sys.argv”.-
classmethod
set_default_command
(cmd=None)[source]¶ A default command can be defined for call it when no one is specified.
A command is detected when its name appears as the first command-line argument.
To specify a default command, use this method with the command as a string (the command name) or the command class.
If the command is specified, then the calling class is the selected one.
For example:
>>> Command.set_default_command('server') >>> Server.set_default_command() >>> Command.set_default_command(Server)
-
class
xoutil.cli.
Help
[source]¶ Show all commands
Define the class attribute __order__ to sort commands in special command “help”.
Commands could define its help in the first line of a sequence of documentations until found:
- command class,
- “run” method,
- definition module.
This command could not be overwritten unless using the class attribute:
__override__ = True
Applications¶
A simple main()
entry point for CLI based applications.
This module provides an example of how to use xoutil.cli
to create a CLI
application.
Tools¶
Utilities for command-line interface (CLI) applications.
-
xoutil.cli.tools.
command_name
(cls)[source]¶ Command names are calculated as class names in lower case inserting a hyphen before each new capital letter. For example “MyCommand” will be used as “my-command”.
It’s defined as an external function because a class method don’t apply to minimal commands (those with only the “run” method).
Example:
>>> class SomeCommand(object): ... pass >>> command_name(SomeCommand) == 'some-command' True
If the command class has an attribute command_cli_name, this will be used instead:
>>> class SomeCommand(object): ... command_cli_name = 'adduser' >>> command_name(SomeCommand) == 'adduser' True
It’s an error to have a non-string command_cli_name attribute:
>>> class SomeCommand(object): ... command_cli_name = None >>> command_name(SomeCommand) Traceback (most recent call last): ... TypeError: Attribute 'command_cli_name' must be a string.