Debian Codegraph @

sites/docs/invoke.rst

raw
1 .. _inv:
2
3 ========================
4 ``inv[oke]`` core usage
5 ========================
6
7 .. seealso::
8 This page documents ``invoke``'s core arguments, options and behavior
9 (which includes options present in :ref:`custom Invoke-based binaries
10 <reusing-as-a-binary>`). For details on invoking user-specified tasks and
11 other parser-related details, see :doc:`/concepts/invoking-tasks`.
12
13
14 Core options and flags
15 ======================
16
17 ``invoke``'s usage looks like::
18
19 $ inv[oke] [--core-opts] task1 [--task1-opts] ... taskN [--taskN-opts] [--
20 remainder-opts-or-values]
21
22 All core options & flags are below; almost all of them must be given *before*
23 any task names, with a few (such as :option:`--help`) being specially looked
24 for anywhere in the command line. (For parsing details, see
25 :ref:`basic-cli-layout`.)
26
27 .. option:: --complete
28
29 Print (line-separated) valid tab-completion options for an Invoke command
30 line given as the 'remainder' (i.e. after a ``--``). Used for building
31 :ref:`shell completion scripts <tab-completion>`.
32
33 For example, when the local tasks tree contains tasks named ``foo`` and
34 ``bar``, and when ``foo`` takes flags ``--foo-arg`` and ``--foo-arg-2``,
35 you might use it like this::
36
37 # Empty input: just task names
38 $ inv --complete --
39 foo
40 bar
41
42 # Input not ending with a dash: task names still
43 $ inv --complete -- foo --foo-arg
44 foo
45 bar
46
47 # Input ending with a dash: current context's flag names
48 $ inv --complete -- foo -
49 --foo-arg
50 --foo-arg-2
51
52 For more details on how to make best use of this option, see
53 :option:`--print-completion-script`.
54
55 .. option:: --hide=STRING
56
57 Set default value of run()'s 'hide' kwarg.
58
59 .. option:: --no-dedupe
60
61 Disable task deduplication.
62
63 .. _print-completion-script:
64
65 .. option:: --print-completion-script=SHELL
66
67 Print a completion script for desired ``SHELL`` (e.g. ``bash``, ``zsh``,
68 etc). This can be sourced into the current session in order to enjoy
69 :ref:`tab-completion for tasks and options <tab-completion>`.
70
71 These scripts are bundled with Invoke's distributed codebase, and
72 internally make use of :option:`--complete`.
73
74 .. _prompt-for-sudo-password:
75
76 .. option:: --prompt-for-sudo-password
77
78 Prompt at the start of the session (before executing any tasks) for the
79 ``sudo.password`` configuration value. This allows users who don't want to
80 keep sensitive material in the config system or their shell environment to
81 rely on user input, without otherwise interrupting the flow of the program.
82
83 .. option:: --write-pyc
84
85 By default, Invoke disables bytecode caching as it can cause hard-to-debug
86 problems with task files and (for the kinds of things Invoke is typically
87 used for) offers no noticeable speed benefit. If you really want your
88 ``.pyc`` files back, give this option.
89
90 .. option:: -c STRING, --collection=STRING
91
92 Specify collection name to load.
93
94 .. option:: -d, --debug
95
96 Enable debug output.
97
98 .. option:: --dry
99
100 Echo commands instead of actually running them; specifically, causes any
101 ``run`` calls to:
102
103 - Act as if the ``echo`` option has been turned on, printing the
104 command-to-be-run to stdout;
105 - Skip actual subprocess invocation (returning before any of that machinery
106 starts running);
107 - Return a dummy `~invoke.runners.Result` object with 'blank' values (empty
108 stdout/err strings, ``0`` exit code, etc).
109
110 .. option:: -D, --list-depth=INT
111
112 Limit :option:`--list` display to the specified number of levels, e.g.
113 ``--list-depth 1`` to show only top-level tasks and namespaces.
114
115 If an argument is given to ``--list``, then this depth is relative; so
116 ``--list build --list-depth 1`` shows everything at the top level of the
117 ``build`` subtree.
118
119 Default behavior if this is not given is to show all levels of the entire
120 task tree.
121
122 .. option:: -e, --echo
123
124 Echo executed commands before running.
125
126 .. option:: -f, --config
127
128 Specify a :ref:`runtime configuration file <config-hierarchy>` to load.
129
130 Note that you may instead use the ``INVOKE_RUNTIME_CONFIG`` environment
131 variable in place of this option. If both are given, the CLI option will
132 win out.
133
134 .. option:: -F, --list-format=STRING
135
136 Change the format used to display the output of :option:`--list`; may be
137 one of:
138
139 - ``flat`` (the default): single, flat vertical list with dotted task
140 names.
141 - ``nested``: a nested (4-space indented) vertical list, where each level
142 implicitly includes its parent (with leading dots as a strong visual clue
143 that these are still subcollection tasks.)
144 - ``json``: intended for consumption by scripts or other programs, this
145 format emits JSON representing the task tree, with each 'node' in the
146 tree (the outermost document being the root node, and thus a JSON object)
147 consisting of the following keys:
148
149 - ``name``: String name of collection; for the root collection this is
150 typically the module name, so unless you're supplying alternate
151 collection name to the load process, it's usually ``"tasks"`` (from
152 ``tasks.py``.)
153 - ``help``: First line of collection's docstring, if it came from a
154 module; null otherwise (or if module lacked a docstring.)
155 - ``tasks``: Immediate children of this collection; an array of objects
156 of the following form:
157
158 - ``name``: Task's local name within its collection (i.e. not the full
159 dotted path you might see with the ``flat`` format; reconstructing
160 that path is left up to the consumer.)
161 - ``help``: First line of task's docstring, or null if it had none.
162 - ``aliases``: An array of string aliases for this task.
163
164 - ``default``: String naming which task within this collection, if any,
165 is the default task. Is null if no task is the default.
166 - ``collections``: An array of any sub-collections within this
167 collection, members of which which will have the same structure as this
168 outermost document, recursively.
169
170 The JSON emitted is not pretty-printed, but does end with a trailing
171 newline.
172
173 .. option:: -h STRING, --help=STRING
174
175 When given without any task names, displays core help; when given with a
176 task name (may come before *or* after the task name) displays help for that
177 particular task.
178
179 .. option:: -l, --list=STRING
180
181 List available tasks. Shows all tasks by default; may give an explicit
182 namespace to 'root' the displayed task tree to only that namespace. (This
183 argument may contain periods, as with task names, so it's possible to show
184 only a small, deep portion of the overall tree if desired.)
185
186 .. option:: -p, --pty
187
188 Use a pty when executing shell commands.
189
190 .. option:: -r STRING, --search-root=STRING
191
192 Change root directory used for finding task modules.
193
194 .. option:: -T INT, --command-timeout=INT
195
196 Set a default command execution timeout of INT seconds. Maps to the
197 ``timeouts.command`` config setting.
198
199 .. option:: -V, --version
200
201 Show version and exit.
202
203 .. option:: -w, --warn-only
204
205 Warn, instead of failing, when shell commands fail.
206
207 .. _remainder:
208
209 Unparsed input: the 'remainder'
210 -------------------------------
211
212 Invoke takes a page from the GNU command-line tradition and many shell
213 builtins, and implements the concept of the *remainder* (aka the "end of
214 options" signal): a 'bare' ``--`` not attached to any option name, followed by
215 arbitrary text.
216
217 Such text is not subject to Invoke's CLI parser, and is used to power features
218 like :ref:`tab-completion` or :ref:`wrapper-tasks`
219
220
221 .. _tab-completion:
222
223 Shell tab completion
224 ====================
225
226 Generating a completion script
227 ------------------------------
228
229 Invoke's philosophy is to implement generic APIs and then "bake in" a few
230 common use cases built on top of those APIs; tab completion is no different.
231 Generic tab completion functionality (outputting a shell-compatible list of
232 completion tokens for a given command line context) is provided by the
233 :option:`--complete` core CLI option described above.
234
235 However, you probably won't need to use that flag yourself: we distribute a
236 handful of ready-made wrapper scripts aimed at the most common shells like
237 ``bash`` and ``zsh`` (plus others). These scripts can be automatically
238 generated from Invoke or :ref:`any Invoke-driven command-line tool
239 <reusing-as-a-binary>`, using :option:`--print-completion-script`; the printed
240 scripts will contain the correct binary name(s) for the program generating
241 them.
242
243 For example, the following command prints (to stdout) a script which works for
244 ``zsh``, instructs ``zsh`` to use it for the ``inv`` and ``invoke`` programs,
245 and calls ``invoke --complete`` at runtime to get dynamic completion
246 information::
247
248 $ invoke --print-completion-script zsh
249
250 .. note::
251 You'll probably want to source this command or store its output somewhere
252 permanently; more on that in the next section.
253
254 Similarly, the `Fabric <https://fabfile.org>`_ tool inherits from Invoke, and
255 only has a single binary name (``fab``); if you wanted to get Fabric completion
256 in ``bash``, you would say::
257
258 $ fab --print-completion-script bash
259
260 In the rest of this section, we'll use ``inv`` in examples, but please remember
261 to replace it with the program you're actually using, if it's not Invoke
262 itself!
263
264 Sourcing the script
265 -------------------
266
267 There are a few ways to utilize the output of the above commands, depending on
268 your needs, where the program is installed, and your shell:
269
270 - The simplest and least disruptive method is to ``source`` the printed
271 completion script inline, which doesn't place anything on disk, and will only
272 affect the current shell session::
273
274 $ source <(inv --print-completion-script zsh)
275
276 - If you've got the program available in your system's global Python
277 interpreter (and you're okay with running the program at the startup of each
278 shell session - Python's speed is admittedly not its strong point) you could
279 add that snippet to your shell's startup file, such as ``~/.zshrc`` or
280 ``~/.bashrc``.
281 - If the program's available globally but you'd prefer to *avoid* running an
282 extra Python program at shell startup, you can cache the output of the
283 command in its own file; where this file lives is entirely up to you and how
284 your shell is configured. For example, you might just drop it into your home
285 directory as a hidden file::
286
287 $ inv --print-completion-script zsh > ~/.invoke-completion.sh
288
289 and then perhaps add the following to the end of ``~/.zshrc``::
290
291 source ~/.invoke-completion.sh
292
293 But again, this is entirely up to you and your shell.
294
295 .. note::
296 If you're using ``fish``, you *must* use this tactic, as our fish
297 completion script is not suitable for direct sourcing. Fish shell users
298 should direct the output of the command to a file in the
299 ``~/.config/fish/completions/`` directory.
300
301 - Finally, if your copy of the needing-completion program is only installed in
302 a specific environment like a virtualenv, you can use either of the above
303 techniques:
304
305 - Caching the output and referencing it in a global shell startup file will
306 still work in this case, as it does not require the program to be
307 available when the shell loads -- only when you actually attempt to tab
308 complete.
309 - Using the ``source <(inv --print-completion-script yourshell)`` approach
310 will work *as long as* you place it in some appropriate per-environment
311 startup file, which will vary depending on how you manage Python
312 environments. For example, if you use ``virtualenvwrapper``, you could
313 append the ``source`` line in ``/path/to/virtualenv/bin/postactivate``.
314
315 Utilizing tab completion itself
316 -------------------------------
317
318 You've ensured that the completion script is active in your environment - what
319 have you gained?
320
321 - By default, tabbing after typing ``inv`` or ``invoke`` will display task
322 names from your current directory/project's tasks file.
323 - Tabbing after typing a dash (``-``) or double dash (``--``) will display
324 valid options/flags for the current context: core Invoke options if no task
325 names have been typed yet; options for the most recently typed task
326 otherwise.
327
328 - Tabbing while typing a partial long option will complete matching long
329 options, using your shell's native substring completion. E.g. if no task
330 names have been typed yet, ``--e<tab>`` will offer ``--echo`` as a
331 completion option.
332
333 - Hitting tab when the most recent typed/completed token is a flag which takes
334 a value, will 'fall through' to your shell's native filename completion.
335
336 - For example, prior to typing a task name, ``--config <tab>`` will
337 complete local file paths to assist in filling in a config file.