program¶
- class invoke.program.Program(version: str | None = None, namespace: Collection | None = None, name: str | None = None, binary: str | None = None, loader_class: Type[Loader] | None = None, executor_class: Type[Executor] | None = None, config_class: Type[Config] | None = None, binary_names: List[str] | None = 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.参见
将 Invoke 的 CLI 模块重用作独立的二进制文件 for a tutorial/walkthrough of this functionality.
在 1.0 版本加入.
- __init__(version: str | None = None, namespace: Collection | None = None, name: str | None = None, binary: str | None = None, loader_class: Type[Loader] | None = None, executor_class: Type[Executor] | None = None, config_class: Type[Config] | None = None, binary_names: List[str] | None = None) None¶
Create a new, parameterized
Programinstance.- 参数:
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) –
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 bothinvandinvoke. 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 yoursetup.py’sconsole_scriptsentry.If
None(default), uses the first word inargvverbatim (as withnameabove, except not capitalized).binary_names –
List of binary name strings, for use in completion scripts.
This list ensures that the shell completion scripts generated by
--print-completion-scriptinstruct 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 inargv(in the invocation of--print-completion-script) is used in a single-item list.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; may also be overridden at runtime by the configuration system and itstasks.executor_classsetting (anytime that setting is notNone).config_class –
The
Configsubclass to use for the base config object.Defaults to
Config.
在 1.2 版本发生变更: Added the
binary_namesargument.
- __weakref__¶
list of weak references to the object
- property args: Lexicon¶
Obtain core program args from
self.coreparse result.在 1.0 版本加入.
- property binary: str¶
Derive program’s help-oriented binary name(s) from init args & argv.
在 1.0 版本加入.
- property binary_names: List[str]¶
Derive program’s completion-oriented binary name(s) from args & argv.
在 1.2 版本加入.
- property called_as: str¶
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.
在 1.2 版本加入.
- create_config() None¶
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.- 返回:
None; setsself.configinstead.
在 1.0 版本加入.
- execute() None¶
Hand off data & tasks-to-execute specification to an
Executor.备注
Client code just wanting a different
Executorsubclass can just setexecutor_classin__init__, or overridetasks.executor_classanywhere in the config system (which may allow you to avoid using a custom Program entirely).在 1.0 版本加入.
- property initial_context: ParserContext¶
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__.在 1.0 版本加入.
- load_collection() None¶
Load a task collection based on parsed core args, or die trying.
在 1.0 版本加入.
- normalize_argv(argv: List[str] | None) None¶
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.在 1.0 版本加入.
- parse_core_args() None¶
Filter out core args, leaving any tasks or their args for later.
Sets
self.coreto theParseResultfrom this step.在 1.0 版本加入.
- parse_tasks() None¶
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.Also modifies
self.coreto include the data fromcore_via_tasks(so that it correctly reflects any supplied core flags regardless of where they appeared).在 1.0 版本加入.
- print_columns(tuples: Sequence[Tuple[str, str | None]]) None¶
Print tabbed columns from (name, help)
tuples.Useful for listing tasks + docstrings, flags + help strings, etc.
在 1.0 版本加入.
- print_task_help(name: str) None¶
Print help for a specific task, e.g.
inv --help <taskname>.在 1.0 版本加入.
- run(argv: List[str] | None = None, exit: bool = True) None¶
Execute main CLI logic, based on
argv.- 参数:
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.备注
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!
在 1.0 版本加入.
- task_args() List[Argument]¶
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).在 1.0 版本加入.
- update_config(merge: bool = True) None¶
Update the previously instantiated
Configwith parsed data.For example, this is how
--echois able to override the default config value forrun.echo.- 参数:
merge (bool) – Whether to merge at the end, or defer. Primarily useful for subclassers. Default:
True.
在 1.0 版本加入.