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.
-
__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.
-
binary¶ Derive program’s help-oriented binary name(s) from init args & argv.
-
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.
-
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__.
-
load_collection()¶ Load a task collection based on parsed core args, or die trying.
-
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.
-
parse_cleanup()¶ Post-parsing, pre-execution steps such as –help, –list, etc.
-
parse_collection()¶ Load a tasks collection & project-level config.
-
parse_core_args()¶ Filter out core args, leaving any tasks or their args for later.
Sets
self.coreto theParseResultfrom this step.
-
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.
-
print_columns(tuples)¶ Print tabbed columns from (name, help)
tuples.Useful for listing tasks + docstrings, flags + help strings, etc.
-
print_task_help(name)¶ Print help for a specific task, e.g.
inv --help <taskname>.
-
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
True(default:False), will ignoreParseError,ExitandFailureexceptions, which otherwise trigger calls tosys.exit.Note
This is mostly a concession to testing. If you’re setting this to
Truein a production setting, you should probably be usingExecutorand friends directly instead!
- argv – The arguments to execute against. May be
-