program¶
-
class
invoke.program.Program(version=None, namespace=None, name=None, binary=None, loader_class=None, executor_class=None, config_class=None)¶ Manages top-level CLI invocation, typically via
setup.pyentrypoints.Designed for distributing Invoke task collections as standalone programs, but also used internally to implement the
invokeprogram itself.See also
Reusing Invoke’s CLI module as a distinct binary for a tutorial/walkthrough of this functionality.
New in version 1.0.
-
__init__(version=None, namespace=None, name=None, binary=None, loader_class=None, executor_class=None, config_class=None)¶ Create a new, parameterized
Programinstance.Parameters: - version (str) – The program’s version, e.g.
"0.1.0". Defaults to"unknown". - namespace –
A
Collectionto use as this program’s subcommands.If
None(the default), the program will behave likeinvoke, seeking a nearby task namespace with aLoaderand exposing arguments such as--listand--collectionfor inspecting or selecting specific namespaces.If given a
Collectionobject, will use it as if it had been handed to--collection. Will also update the parser to remove references to tasks and task-related options, and display the subcommands in--helpoutput. The result will be a program that has a static set of subcommands. - name (str) –
The program’s name, as displayed in
--versionoutput.If
None(default), is a capitalized version of the first word in theargvhanded torun. For example, when invoked from a binstub installed asfoobar, it will default toFoobar. - binary (str) –
The binary name as displayed in
--helpoutput.If
None(default), uses the first word inargvverbatim (as withnameabove, except not capitalized).Giving this explicitly may be useful when you install your program under multiple names, such as Invoke itself does - it installs as both
invandinvoke, and setsbinary="inv[oke]"so its--helpoutput implies both names. - loader_class –
The
Loadersubclass to use when loading task collections.Defaults to
FilesystemLoader. - executor_class –
The
Executorsubclass to use when executing tasks.Defaults to
Executor. - config_class –
The
Configsubclass to use for the base config object.Defaults to
Config.
- version (str) – The program’s version, e.g.
-
__weakref__¶ list of weak references to the object (if defined)
-
args¶ Obtain core program args from
self.coreparse result.New in version 1.0.
-
binary¶ Derive program’s help-oriented binary name(s) from init args & argv.
New in version 1.0.
-
create_config()¶ 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. SeeConfigdocstring for lifecycle details.Returns: None; setsself.configinstead.New in version 1.0.
-
execute()¶ Hand off data & tasks-to-execute specification to an
Executor.Note
Client code just wanting a different
Executorsubclass can just setexecutor_classin__init__.New in version 1.0.
-
initial_context¶ 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__.New in version 1.0.
-
load_collection()¶ Load a task collection based on parsed core args, or die trying.
New in version 1.0.
-
normalize_argv(argv)¶ Massages
argvinto 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.splitand then executes with the result. (This is mostly a convenience; when in doubt, use a list.)Sets
self.argvto the result.New in version 1.0.
-
parse_cleanup()¶ Post-parsing, pre-execution steps such as –help, –list, etc.
New in version 1.0.
-
parse_collection()¶ Load a tasks collection & project-level config.
New in version 1.0.
-
parse_core_args()¶ Filter out core args, leaving any tasks or their args for later.
Sets
self.coreto theParseResultfrom this step.New in version 1.0.
-
parse_tasks()¶ Parse leftover args, which are typically tasks & per-task args.
Sets
self.parserto the parser used,self.tasksto the parsed per-task contexts, andself.core_via_tasksto a context holding any core flags seen within the task contexts.New in version 1.0.
-
print_columns(tuples)¶ Print tabbed columns from (name, help)
tuples.Useful for listing tasks + docstrings, flags + help strings, etc.
New in version 1.0.
-
print_task_help(name)¶ Print help for a specific task, e.g.
inv --help <taskname>.New in version 1.0.
-
run(argv=None, exit=True)¶ Execute main CLI logic, based on
argv.Parameters: - argv – The arguments to execute against. May be
None, a list of strings, or a string. Seenormalize_argvfor details. - exit (bool) –
When
False(default:True), will ignoreParseError,ExitandFailureexceptions, which otherwise trigger calls tosys.exit.Note
This is mostly a concession to testing. If you’re setting this to
Falsein a production setting, you should probably be usingExecutorand friends directly instead!
New in version 1.0.
- argv – The arguments to execute against. May be
-
task_args()¶ Return default task-related
Argumentobjects, as a list.These are only added to the core args in “task runner” mode (the default for
invokeitself) - they are omitted when the constructor is given a non-emptynamespaceargument (“bundled namespace” mode).New in version 1.0.
-
update_config(merge=True)¶ Update the previously instantiated
Configwith parsed data.For example, this is how
--echois able to override the default config value forrun.echo.Parameters: merge (bool) – Whether to merge at the end, or defer. Primarily useful for subclassers. Default: True.New in version 1.0.
-