cli

Tools to assist in consolidating sources of project configurations.

class ArgParser(default_action=None, usage='%(prog)s [action(s)] [-h]', description='Refer to the README.md for the available actions!', epilog='\nAdditionally, all fields of the program\'s config can be set via\nlong-format options. Nested fields can be set by dot-separating\nlevels, e.g., "--root.level1.level2 value"\n\n{!r}\n', fmt_cls=<class 'argparse.RawTextHelpFormatter'>)[source]

Bases: object

Parse the command line for actions and any long-format options.

Using this command-line argument parser alleviates the need for defining any groups or options beforehand. Arguments immediately following the program call are interpreted as actions to perform as long as they do not start with a hyphen. Starting with the first argument that starts with a hyphen, command-line arguments will be interpreted as --key value pairs and this long format is the only one allowed. Abbreviated options (-k value) right after actions (and before any long-format options) are ignored.

Parameters:
  • default_action (str, optional) – Default action to return if none is found in the command-line arguments. Defaults to no action.

  • usage (str, optional) – Program usage message.

  • description (str, optional) – Program description.

  • epilog (str, optional) – Text displayed after the help on command-line options.

  • fmt_cls (type, optional) – Option passed on to the underlying argparse.ArgumentParser. Defaults to argparse.RawTextHelpFormatter

__call__(args=None)[source]

Parse the command-line arguments into actions and options.

Parameters:

args (list of str, optional) – The command-line arguments to parse. Mainly a debugging feature. If none is given, sys.argv[1:] will be parsed.

Returns:

  • actions (list of str) – A list with the actions (as strings) to perform. If none are found on the command line and no default_action is specified, that list will be empty.

  • options (dict) – Dictionary with keys and values parsed from long-format command line arguments.

class EnvParser(prefix='')[source]

Bases: ArgRepr

Parse OS environment variables, preferring prefixed over pure versions.

Sometimes, environment variables desired for individual use are already taken by the operating system or some other system component. In these cases, one can resort to prefixing these to avoid conflicts. The present class is instantiated with that prefix and will resolve conflicts when objects are called, returning the OS environment as a dictionary with the values of all variables parsed into python literals.

Parameters:

prefix (str, optional) – Prefix of environment variables that would otherwise be shadowed by existing ones. Defaults to empty string.

__call__(env=None)[source]

Parse the OS environment, resolving potentially prefixed variables.

Parameters:

env (dict, optional) – Dictionary to be parsed and resolved. Defaults to os.environ.

Returns:

Environment with prefixed keys removed and the values of their non-prefixed counterparts updated accordingly.

Return type:

dict

class Importer(package, module='steps')[source]

Bases: ArgRepr

Programmatically import objects from a module under a top-level package.

For ease of use and clarity in API, relative imports are not supported. Objects are instantiated with where to import from and called with what to import.

Parameters:
  • package (str) – Name of the top-level package to import from. Must not start with dots but can contain any number of dots to indicate sub-packages.

  • module (str, optional) – The specific module to import objects from. May contain dots to indicate that it is located further down within some sub-package. Defaults to “steps”.

__call__(*names)[source]

Import any number of objects from the specified package.module.

Parameters:

*names (str) – Name(s) of object(s) to import from package.module.

Returns:

Imported objects.

Return type:

list

Raises:

ImporterError – When the package.module is mis-specified, can’t be found, or when the specified object(s) can’t be found in it.

property path

Full path specification of (sub-)package and module concatenated.