Warning
THIS MODULE DOES NOT HAVE STABLE PUBLIC API
Simple interface class for sub-commands of ToolBase.
Command objects like this are consumed by ToolBase subclasses to implement hierarchical command system. The API supports arbitrary many sub commands in arbitrary nesting arrangement.
Subcommands need to be registered inside the register_parser(), either manually by calling add_parser() on the passed subparsers instance, or by calling the helper add_subcommand() method. By common convention each subclass of CommandBase adds exactly one subcommand to the parser.
Add a parser to the specified subparsers instance.
Returns: | The new parser for the added subcommand |
---|
This command works by convention, depending on get_command_name(), :meth:`get_command_help(), get_command_description() and get_command_epilog().
Enable automatic pager.
This invokes autopager() which wraps execution in a pager program so that long output is not a problem to read. Do not call this in interactive commands.
Get a multi-line description string associated with this command, as seen on command line.
The description is printed after command usage but before argument and option definitions.
Returns: | self.description, if defined |
---|---|
Returns: | A substring of the class docstring between the first line (which goes to get_command_help()) and the string @EPILOG@, if present, or the end of the docstring, if any. |
Returns: | None, otherwise |
Get a multi-line description string associated with this command, as seen on command line.
The epilog is printed after the definitions of arguments and options
Returns: | self.epilog, if defined |
---|---|
Returns: | A substring of the class docstring between the string @EPILOG and the end of the docstring, if defined |
Returns: | None, otherwise |
Get a single-line help string associated with this command, as seen on command line.
Returns: | self.help, if defined |
---|---|
Returns: | The first line of the docstring of this class, if any |
Returns: | None, otherwise |
Get the name of the command, as seen on command line.
Returns: | self.name, if defined |
---|---|
Returns: | lower-cased class name, with the string “command” stripped out |
Get the gettext translation domain associated with this command.
The domain will be used to translate the description, epilog and help string, as obtained by their respective methods.
Returns: | self.gettext_domain, if defined |
---|---|
Returns: | None, otherwise. Note that it will cause the string to be translated with the globally configured domain. |
Base class for implementing programs with hierarchical subcommands
The tools support a variety of sub-commands, logging and debugging support. If argcomplete module is available and used properly in the shell then advanced tab-completion is also available.
There are three methods to implement for a basic tool. Those are:
This class has some complex control flow to support important and interesting use cases. It is important to know that input is parsed with two parsers, the early parser and the full parser. The early parser quickly checks for a fraction of supported arguments and uses that data to initialize environment before construction of a full parser is possible. The full parser sees the reminder of the input and does not re-parse things that where already handled.
Add top-level subcommands to the argument parser.
This can be overridden by subclasses to use a different set of top-level subcommands.
Create a parser that captures some of the early data we need to be able to have a real parser and initialize the rest.
Construct a bare parser object.
This method is responsible for creating the main parser object and adding –version and other basic top-level properties to it (but not any of the commands).
It exists as a separate method in case some special customization is required, so that subclasses can still use standard version of construct_parser().
Returns: | argparse.ArgumentParser instance. |
---|
Do very early initialization. This is where we initialize stuff even without seeing a shred of command line data or anything else.
Do some final initialization just before the command gets dispatched. This is empty here but maybe useful for subclasses.
Get the name of the gettext domain that should be used by this tool.
The value returned will be used to select translations to global calls to gettext() and ngettext() everywhere in python.
Get the path of the gettext translation catalogs for this tool.
This value is used to bind the domain returned by get_gettext_domain() to a specific directory. By default None is returned, which means that standard, system-wide locations are used.
Enable automatic pager
Parameters: | pager_list – List of pager programs to try. |
---|---|
Returns: | Nothing immedaitely if auto-pagerification cannot be turned on. This is true when running on windows or when sys.stdout is not a tty. |
This function executes the following steps:
Note
Pager selection is influenced by the pager environment variable. if set it will be prepended to the pager_list. This makes the expected behavior of allowing users to customize their environment work okay.
Warning
This function must not be used for interactive commands. Doing so will prevent users from feeding any input to plainbox as all input will be “stolen” by the pager process.
Find the first executable from name_list in PATH
Parameters: | name_list – List of names of executable programs to look for, in the order of preference. Only basenames should be passed here (not absolute pathnames) |
---|---|
Returns: | Tuple (name, pathname), if the executable can be found |
Raises: | LookupError if none of the names in name_list are executable programs in PATH |