class documentation

class Program: (source)

View In Hierarchy

Manages top-level CLI invocation, typically via ``setup.py`` entrypoints. Designed for distributing Invoke task collections as standalone programs, but also used internally to implement the ``invoke`` program itself. .. seealso:: :ref:`reusing-as-a-binary` for a tutorial/walkthrough of this functionality. .. versionadded:: 1.0

Method __init__ Create a new, parameterized `.Program` instance.
Method core_args Return default core `.Argument` objects, as a list.
Method create_config Instantiate a `.Config` (or subclass, depending) for use in task exec.
Method display_with_columns Undocumented
Method execute Hand off data & tasks-to-execute specification to an `.Executor`.
Method list_flat Undocumented
Method list_json Undocumented
Method list_nested Undocumented
Method list_tasks Undocumented
Method load_collection Load a task collection based on parsed core args, or die trying.
Method no_tasks_given Undocumented
Method normalize_argv Massages ``argv`` into a useful list of strings.
Method parse_cleanup Post-parsing, pre-execution steps such as --help, --list, etc.
Method parse_collection Load a tasks collection & project-level config.
Method parse_core Undocumented
Method parse_core_args Filter out core args, leaving any tasks or their args for later.
Method parse_tasks Parse leftover args, which are typically tasks & per-task args.
Method print_columns Print tabbed columns from (name, help) ``tuples``.
Method print_help Undocumented
Method print_task_help Print help for a specific task, e.g. ``inv --help <taskname>``.
Method print_version Undocumented
Method run Execute main CLI logic, based on ``argv``.
Method task_args Return default task-related `.Argument` objects, as a list.
Method task_list_opener Undocumented
Method update_config Update the previously instantiated `.Config` with parsed data.
Class Variable col_padding Undocumented
Class Variable indent Undocumented
Class Variable indent_width Undocumented
Class Variable leading_indent Undocumented
Class Variable leading_indent_width Undocumented
Instance Variable argv Undocumented
Instance Variable collection Undocumented
Instance Variable config Undocumented
Instance Variable config_class Undocumented
Instance Variable core Undocumented
Instance Variable core_via_tasks Undocumented
Instance Variable executor_class Undocumented
Instance Variable list_depth Undocumented
Instance Variable list_format Undocumented
Instance Variable list_root Undocumented
Instance Variable loader_class Undocumented
Instance Variable namespace Undocumented
Instance Variable parser Undocumented
Instance Variable scoped_collection Undocumented
Instance Variable tasks Undocumented
Instance Variable version Undocumented
Property args Obtain core program args from ``self.core`` parse result.
Property binary Derive program's help-oriented binary name(s) from init args & argv.
Property binary_names Derive program's completion-oriented binary name(s) from args & argv.
Property called_as Returns the program name we were actually called as.
Property initial_context The initial parser context, aka core program flags.
Property name Derive program's human-readable name based on `.binary`.
Method _make_pairs Undocumented
Method _make_parser Undocumented
Method _update_core_context Undocumented
Instance Variable _binary Undocumented
Instance Variable _binary_names Undocumented
Instance Variable _name Undocumented
def __init__(self, version=None, namespace=None, name=None, binary=None, loader_class=None, executor_class=None, config_class=None, binary_names=None): (source)

Create a new, parameterized `.Program` instance. :param str version: The program's version, e.g. ``"0.1.0"``. Defaults to ``"unknown"``. :param namespace: A `.Collection` to use as this program's subcommands. If ``None`` (the default), the program will behave like ``invoke``, seeking a nearby task namespace with a `.Loader` and exposing arguments such as :option:`--list` and :option:`--collection` for inspecting or selecting specific namespaces. If given a `.Collection` object, will use it as if it had been handed to :option:`--collection`. Will also update the parser to remove references to tasks and task-related options, and display the subcommands in ``--help`` output. The result will be a program that has a static set of subcommands. :param str name: The program's name, as displayed in ``--version`` output. If ``None`` (default), is a capitalized version of the first word in the ``argv`` handed to `.run`. For example, when invoked from a binstub installed as ``foobar``, it will default to ``Foobar``. :param str binary: Descriptive lowercase binary name string used in help text. For example, Invoke's own internal value for this is ``inv[oke]``, denoting that it is installed as both ``inv`` and ``invoke``. As this is purely text intended for help display, it may be in any format you wish, though it should match whatever you've put into your ``setup.py``'s ``console_scripts`` entry. If ``None`` (default), uses the first word in ``argv`` verbatim (as with ``name`` above, except not capitalized). :param binary_names: List of binary name strings, for use in completion scripts. This list ensures that the shell completion scripts generated by :option:`--print-completion-script` instruct the shell to use that completion for all of this program's installed names. For example, Invoke's internal default for this is ``["inv", "invoke"]``. If ``None`` (the default), the first word in ``argv`` (in the invocation of :option:`--print-completion-script`) is used in a single-item list. :param loader_class: The `.Loader` subclass to use when loading task collections. Defaults to `.FilesystemLoader`. :param executor_class: The `.Executor` subclass to use when executing tasks. Defaults to `.Executor`; may also be overridden at runtime by the :ref:`configuration system <default-values>` and its ``tasks.executor_class`` setting (anytime that setting is not ``None``). :param config_class: The `.Config` subclass to use for the base config object. Defaults to `.Config`. .. versionchanged:: 1.2 Added the ``binary_names`` argument.

def core_args(self): (source)

Return default core `.Argument` objects, as a list. .. versionadded:: 1.0

def create_config(self): (source)

Instantiate a `.Config` (or subclass, depending) for use in task exec. This Config is fully usable but will lack runtime-derived data like project & runtime config files, CLI arg overrides, etc. That data is added later in `update_config`. See `.Config` docstring for lifecycle details. :returns: ``None``; sets ``self.config`` instead. .. versionadded:: 1.0

def display_with_columns(self, pairs, extra=''): (source)

Undocumented

def execute(self): (source)

Hand off data & tasks-to-execute specification to an `.Executor`. .. note:: Client code just wanting a different `.Executor` subclass can just set ``executor_class`` in `.__init__`, or override ``tasks.executor_class`` anywhere in the :ref:`config system <default-values>` (which may allow you to avoid using a custom Program entirely). .. versionadded:: 1.0

def list_flat(self): (source)

Undocumented

def list_json(self): (source)

Undocumented

def list_nested(self): (source)

Undocumented

def list_tasks(self): (source)

Undocumented

def load_collection(self): (source)

Load a task collection based on parsed core args, or die trying. .. versionadded:: 1.0

def no_tasks_given(self): (source)

Undocumented

def normalize_argv(self, argv): (source)

Massages ``argv`` into a useful list of strings. **If None** (the default), uses `sys.argv`. **If a non-string iterable**, uses that in place of `sys.argv`. **If a string**, performs a `str.split` and then executes with the result. (This is mostly a convenience; when in doubt, use a list.) Sets ``self.argv`` to the result. .. versionadded:: 1.0

def parse_cleanup(self): (source)

Post-parsing, pre-execution steps such as --help, --list, etc. .. versionadded:: 1.0

def parse_collection(self): (source)

Load a tasks collection & project-level config. .. versionadded:: 1.0

def parse_core(self, argv): (source)

Undocumented

def parse_core_args(self): (source)

Filter out core args, leaving any tasks or their args for later. Sets ``self.core`` to the `.ParseResult` from this step. .. versionadded:: 1.0

def parse_tasks(self): (source)

Parse leftover args, which are typically tasks & per-task args. Sets ``self.parser`` to the parser used, ``self.tasks`` to the parsed per-task contexts, and ``self.core_via_tasks`` to a context holding any core flags seen within the task contexts. Also modifies ``self.core`` to include the data from ``core_via_tasks`` (so that it correctly reflects any supplied core flags regardless of where they appeared). .. versionadded:: 1.0

def print_columns(self, tuples): (source)

Print tabbed columns from (name, help) ``tuples``. Useful for listing tasks + docstrings, flags + help strings, etc. .. versionadded:: 1.0

def print_help(self): (source)

Undocumented

def print_task_help(self, name): (source)

Print help for a specific task, e.g. ``inv --help <taskname>``. .. versionadded:: 1.0

def print_version(self): (source)

Undocumented

def run(self, argv=None, exit=True): (source)

Execute main CLI logic, based on ``argv``. :param argv: The arguments to execute against. May be ``None``, a list of strings, or a string. See `.normalize_argv` for details. :param bool exit: When ``False`` (default: ``True``), will ignore `.ParseError`, `.Exit` and `.Failure` exceptions, which otherwise trigger calls to `sys.exit`. .. note:: This is mostly a concession to testing. If you're setting this to ``False`` in a production setting, you should probably be using `.Executor` and friends directly instead! .. versionadded:: 1.0

def task_args(self): (source)

Return default task-related `.Argument` objects, as a list. These are only added to the core args in "task runner" mode (the default for ``invoke`` itself) - they are omitted when the constructor is given a non-empty ``namespace`` argument ("bundled namespace" mode). .. versionadded:: 1.0

def task_list_opener(self, extra=''): (source)

Undocumented

def update_config(self, merge=True): (source)

Update the previously instantiated `.Config` with parsed data. For example, this is how ``--echo`` is able to override the default config value for ``run.echo``. :param bool merge: Whether to merge at the end, or defer. Primarily useful for subclassers. Default: ``True``. .. versionadded:: 1.0

col_padding: int = (source)

Undocumented

Undocumented

indent_width: int = (source)

Undocumented

leading_indent = (source)

Undocumented

leading_indent_width: int = (source)

Undocumented

Undocumented

collection = (source)

Undocumented

Undocumented

config_class = (source)

Undocumented

Undocumented

core_via_tasks = (source)

Undocumented

executor_class = (source)

Undocumented

list_depth = (source)

Undocumented

list_format = (source)

Undocumented

list_root = (source)

Undocumented

loader_class = (source)

Undocumented

namespace = (source)

Undocumented

Undocumented

scoped_collection = (source)

Undocumented

Undocumented

Undocumented

Obtain core program args from ``self.core`` parse result. .. versionadded:: 1.0

Derive program's help-oriented binary name(s) from init args & argv. .. versionadded:: 1.0

@property
binary_names = (source)

Derive program's completion-oriented binary name(s) from args & argv. .. versionadded:: 1.2

Returns the program name we were actually called as. Specifically, this is the (Python's os module's concept of a) basename of the first argument in the parsed argument vector. .. versionadded:: 1.2

@property
initial_context = (source)

The initial parser context, aka core program flags. The specific arguments contained therein will differ depending on whether a bundled namespace was specified in `.__init__`. .. versionadded:: 1.0

Derive program's human-readable name based on `.binary`. .. versionadded:: 1.0

def _make_pairs(self, coll, ancestors=None): (source)

Undocumented

def _make_parser(self): (source)

Undocumented

def _update_core_context(self, context, new_args): (source)

Undocumented

Undocumented

_binary_names = (source)

Undocumented

Undocumented