plainbox.impl.commands – shared code for plainbox sub-commands



class plainbox.impl.commands.PlainBoxCommand[source]

Bases: plainbox.impl.clitools.CommandBase

Simple interface class for plainbox commands.

Command objects like this are consumed by PlainBoxTool subclasses to implement hierarchical command system. The API supports arbitrary many sub commands in arbitrary nesting arrangement.


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., 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., 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.

Get a cleaned-up, localized copy of docstring of this class.

gettext_domain = 'plainbox'

Implement what should happen when the command gets invoked

The ns is the namespace produced by argument parser


Implement to customize which arguments need to be added to a parser.

This method differs from register_parser() in that it allows commands which implement it to be invoked directly from a tool class (without being a subcommand that needs to be selected). If implemented it should be used from within register_parser() to ensure identical behavior in both cases (subcommand and tool-level command)


Implement what should happen to register the additional parser for this command. The subparsers argument is the return value of ArgumentParser.add_subparsers()

class plainbox.impl.commands.PlainBoxToolBase[source]

Bases: plainbox.impl.clitools.ToolBase

Base class for implementing commands like ‘plainbox’.

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 four methods to implement for a basic tool. Those are:

  1. get_exec_name() – to know how the command will be called
  2. get_exec_version() – to know how the version of the tool
  3. add_subcommands() – to add some actual commands to execute
  4. get_config_cls() – to know which config to use

This class has some complex control flow to support important and interesting use cases. There are some concerns to people that subclass this in order to implement their own command line tools.

The first concern is 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.

The second concern is that this command natively supports the concept of a config object and a provider object. This may not be desired by all users but it is the current state as of this writing. This means that by the time eary init is done we have a known provider and config objects that can be used to instantiate command objects in add_subcommands(). This API might change when full multi-provider is available but details are not known yet.

add_subcommands(subparsers, early_ns)

Add top-level subcommands to the argument parser.

  • subparsers – The argparse subparsers object. Use it to register additional command line syntax parsers and to add your commands there.
  • early_ns – A namespace from parsing by the special early parser. The early parser may be used to quickly guess the command that needs to be loaded, despite not really being able to parse everything the full parser can. Using this as a hint one can optimize the command loading process to skip loading commands that would not be executed.

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.

classmethod get_config_cls()[source]

Get the Config class that is used by this implementation.

This can be overridden by subclasses to use a different config class that is suitable for the particular application.


Get the name of this executable


Get the version reported by this executable


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.


Initialize with early command line arguments being already parsed


Run as if invoked from command line directly


Setup i18n and l10n system.

comments powered by Disqus