CLI

This module provides tools for handling command-line arguments for a Krux application.

Krux applications use argparse for parsing command-line options. The add_*_args functions in this module all expect to be called with an instance of argparse.ArgumentParser, a subclass, or an object that follows the same interface.

argparse is part of the standard library in Python 3.x, but Krux uses Python 2.6, so you will need to add it to your requirements.pip file:

argparse==1.2.1

Usage:

from krux.cli import get_parser, Application


if __name__ == '__main__':

    ### functional
    parser = get_parser(description = 'My Krux CLI App')

    ### OO
    app     = Application(name = 'My Krux CLI App')
    parser  = app.parser

Functions

exception krux.cli.CriticalApplicationError[source]

This error is only raised if the application is expected to exit. It should never be caught.

class krux.cli.Application(name, parser=None, logger=None, lockfile=False)[source]

Krux base class for CLI applications

Parameters:name – name of your CLI application

Wraps object and sets up CLI argument parsing, stats and logging.

Parameters:name (string) – name of the application. Should be unique to Krux

(required)

Parameters:parser – The CLI parser. Defaults to

cli.get_parser

add_cli_arguments(parser)[source]

Any additional CLI arguments that (super) classes might want to add. This method is overridable.

add_exit_hook(hook, *args, **kwargs)[source]

Adds the given function as an exit hook. The function will be called with the given positional and keyword arguments when the application’s exit() method is called.

exit(code=0)[source]

Shut down the application and exit with the given status code.

Calls all of the defined exit hooks before exiting. Exceptions are caught and logged.

raise_critical_error(err)[source]

This logs the error, releases any lock files and throws an exception. The expectation is that the application exits after this.

krux.cli.add_logging_args(parser)[source]

Add logging-related command-line arguments to the given parser.

Logging arguments are added to the ‘logging’ argument group which is created if it doesn’t already exist.

Parameters:parser – parser instance to which the arguments will be added
krux.cli.add_stats_args(parser)[source]

Add stats-related command-line arguments to the given parser.

Parameters:parser – parser instance to which the arguments will be added
krux.cli.get_group(parser, group_name)[source]

Return an argument group based on the group name.

This will return an existing group if it exists, or create a new one.

Parameters:
  • parserargparse.ArgumentParser to add/retrieve the group to/from.
  • group_name – Name of the argument group to be created/returned.
krux.cli.get_parser(description='Krux CLI', logging=True, stats=True, **kwargs)[source]

Run setup and return an argument parser for Krux applications

Parameters:
  • description (string) – Branding for the usage output.
  • logging (bool) – Enable standard logging arguments.
  • stats (bool) – Enable standard stats argument.

All other keywords are passed verbatim to argparse.ArgumentParser

Table Of Contents

This Page