Debian Codegraph @

docs/documentation.md.in

raw
1 Crazy-Complete Documentation
2 ============================
3
4 This documentation provides an overview of how to define shell completion for commands using crazy-complete.
5
6 - [Generating a Definition File from Help](#generating-a-definition-file-from-help)
7 - [Defining a Command](#defining-a-command)
8 - [Defining an Option](#defining-an-option)
9 - [Defining a Positional Argument](#defining-a-positional-argument)
10 - [Using Aliases](#using-aliases)
11 - [Completion Commands](#completion-commands)
12 - [Meta Commands](#meta-commands)
13 - [Built-in Commands](#built-in-commands)
14 - [User-Defined Commands](#user-defined-commands)
15 - [Bonus Commands](#bonus-commands)
16 - [Options](#options)
17 - [When Conditionals](#when-conditionals)
18 - [Capturing Options](#capturing-options)
19 - [Tips and Tricks](#tips-and-tricks)
20
21 ## Generating a Definition File from Help
22
23 It is possible to generate a definition file from a commands help output:
24
25 ```
26 grep --help > help_file
27 crazy-complete --input-type=help yaml help_file
28
29 # or
30
31 grep --help | crazy-complete --input-type=help yaml /dev/stdin
32 ```
33
34 ## Defining a Command
35
36 To define a completion for a command, use the following structure:
37
38 ```yaml
39 prog: "<PROGRAM NAME>"
40 aliases: ["<ALIAS>", ...]
41 help: "<PROGRAM DESCRIPTION>"
42 wraps: "<PROGRAM>"
43 options:
44 <OPTION ...>
45 positionals:
46 <POSITIONAL ...>
47 ```
48
49 - *prog*: The name of the program for which you want to create completion
50 - *aliases* (optional): Specify alternative program names for which this completion should also apply
51 - *help* (optional): A short description of the program
52 - *wraps* (optional): Inherit completion behaviour of another command
53 - *options* (optional): A list of [options](#defining-an-option) the program accepts
54 - *positionals* (optional): A list of [positional arguments](#defining-a-positional-argument) the program uses
55
56 ## Defining an Option
57
58 To define an option, use this format:
59
60 ```yaml
61 [...]
62 options:
63 - option_strings: ["<OPTION STRING>", ...]
64 metavar: "<METAVAR>"
65 help: "<OPTION DESCRIPTION>"
66 optional_arg: <BOOL>
67 complete: <COMPLETE ACTION>
68 nosort: <BOOL>
69 repeatable: <BOOL>
70 final: <BOOL>
71 hidden: <BOOL>
72 groups: ["<GROUP>", ...]
73 when: "<CONDITION>"
74 capture: "<VARIABLE>"
75 long_opt_arg_sep: "equals|space|both"
76 [...]
77 ```
78
79 - *option\_strings*: A list of option strings (e.g., ["-h", "--help"])
80 - *metavar* (optional): The placeholder used for the argument (e.g., "FILE")
81 - *help* (optional): A description of the option
82 - *optional\_arg* (optional): Indicates if the option's argument is optional (default: false)
83 - *complete* (optional): Defines the method used to provide possible completions for this option. If not set, the option does not take an argument. Use `["none"]` if the option accepts an argument but no specific completion method applies. See [Completion Commands](#completion-commands)
84 - *nosort* (optional): Do not sort completion suggestions alphabetically. Keep them in the order generated by the completer. (true or false, default: false)
85 - *repeatable* (optional): Indicates whether an option can be suggested multiple times (true or false, default: false)
86 - *final* (optional): The final parameter indicates that no further options are shown after this one if it is passed on command line. Mostly used for --help and --version (default: false)
87 - *hidden* (optional): Specifies whether an option should be excluded from the auto-completion suggestions, though it remains usable when typed manually. (default: false)
88 - *groups* (optional): Add this option into the specified groups. Multiple flags from the same group cannot be completed at once. Useful for mutually exclusive flags
89 - *when* (optional): Only enable this option if [CONDITION](#when-conditionals) evaluates to true
90 - *capture* (optional): Specify the [variable](#capturing-options) where values of this option should be captured
91 - *long_opt_arg_sep* (optional): Specifies which separators are used for delimiting a long option from its argument
92
93 ## Defining a Positional Argument
94
95 Positional arguments are defined as follows:
96
97 ```yaml
98 [...]
99 positionals:
100 - number: <NUMBER>
101 metavar: "<METAVAR>"
102 help: "<POSITIONAL DESCRIPTION>"
103 repeatable: <BOOL>
104 complete: <COMPLETE ACTION>
105 nosort: <BOOL>
106 when: "<CONDITION>"
107 [...]
108 ```
109
110 - *number*: The order of the positional argument (e.g., 1 for the first argument)
111 - *metavar* (optional): A placeholder for the positional argument in the help text
112 - *help* (optional): A description of the positional argument
113 - *repeatable* (optional): Indicates if this positional argument can be repeated (true or false, default: false)
114 - *complete* (optional): The method used to generate possible completions for this positional argument. Default `["none"]`. See [Completion Commands](#completion-commands).
115 - *nosort* (optional): Do not sort completion suggestions alphabetically. Keep them in the order generated by the completer. (true or false, default: false)
116 - *when* (optional): Only enable this positional if [CONDITION](#when-conditionals) evaluates to true
117
118 ### Using Aliases
119
120 Aliases / defines can be handy if completers are reused or to keep the definition file clean.
121
122 **NOTE:** Every defined string is replaced throughout the YAML document, regardless of its context.
123
124 ```yaml
125 prog: '%defines%'
126 complete_bool: ['choices', ['true', 'false']]
127 ---
128 prog: "example"
129 options:
130 - option_strings: ['--option-1']
131 complete: 'complete_bool'
132
133 - option_strings: ['--option-2']
134 complete: 'complete_bool'
135 ```
136
137 ## Defining Subcommands
138
139 To define subcommands, append the subcommand name directly to the program name:
140
141 ```yaml
142 prog: "<PROGRAM NAME> <SUBCOMMAND> ..."
143 aliases: ["<ALIAS>", ...]
144 help: "<SUBCOMMAND DESCRIPTION>"
145 [...]
146 ```
147
148 - *prog*: The name of the program, followed by the subcommand(s)
149 - *aliases* (optional): A list of alternative names for the subcommand. Aliases must not include the program name
150 - *help* (optional): A description of the subcommand
151
152 ## Completion Commands
153
154 %COMMANDS%
155
156 ### Options
157
158 %OPTIONS%
159
160 ## When Conditionals
161
162 Options and Positional Arguments can include a `when` attribute that defines
163 a condition under which the option (or positional argument) should be
164 activated.
165
166 ### has\_option
167
168 > **Syntax**: `has_option OPTION...`
169
170 > Checks if one or more specified options have been provided on the command line.
171
172 > **NOTE**: The options used inside the condition have also to be defined as options!
173
174 **Examples:**
175
176 ```yaml
177 # This activates --conditional if --foo, --bar or --baz are present on the command line
178
179 [...]
180 options:
181 - option_strings: ["--conditional"]
182 when: "has_option --foo --bar --baz"
183
184 - option_strings: ["--foo", "--bar", "--baz"]
185 [...]
186 ```
187
188 ### option\_is
189
190 > **Syntax**: `option_is [ANY] [NOCASE] OPTION... -- VALUE...`
191
192 > Checks if one or more specified options have been set to a specific value.
193
194 > If `ANY` is specified, the match succeeds if any of the found options matches; otherwise, only the last option is checked.
195
196 > If `NOCASE` is specified, perform a case-insensitive match.
197
198 > **NOTE**: The options used inside the condition have also to be defined as options!
199
200 **Example:**
201
202 ```yaml
203 # This activates --conditional if --foo, --bar or --baz are set to value1, value2 or value3
204
205 [...]
206 options:
207 - option_strings: ["--conditional"]
208 when: "option_is --foo --bar --baz -- value1 value2 value3"
209
210 - option_strings: ["--foo", "--bar", "--baz"]
211 complete: ["none"]
212 [...]
213 ```
214
215 ### option\_match
216
217 > **Syntax**: `option_match [ANY] [NOCASE] OPTION... -- REGEX`
218
219 > Checks if one or more specified options match an extended regular expression.
220
221 > If `ANY` is specified, the match succeeds if any of the found options matches; otherwise, only the last option is checked.
222
223 > If `NOCASE` is specified, perform a case-insensitive match.
224
225 > **NOTE**: The options used inside the condition have also to be defined as options!
226
227 **Example:**
228
229 ```yaml
230 [...]
231 options:
232 - option_strings: ["--conditional"]
233 when: "option_match NOCASE --foo --bar -- 'foo[0-9]+'"
234
235 - option_strings: ["--foo", "--bar"]
236 complete: ["none"]
237 [...]
238 ```
239
240
241 ### positional\_count
242
243 > **Syntax**: `positional_count OPERATOR NUMBER`
244
245 > Checks the number of given positionals on the command line.
246
247 **Example:**
248
249 ```yaml
250 # This activates --conditional if one or more positionals are given on the command line
251
252 [...]
253 options:
254 - option_strings: ["--conditional"]
255 when: "positional_count >= 1"
256
257 positionals:
258 - number: 1
259 repeatable: true
260 [...]
261 ```
262
263 ### positional\_contains
264
265 > **Syntax**: `positional_contains [NOCASE] NUMBER VALUES...`
266
267 > Checks if a positional has a specific value.
268
269 > If `NOCASE` is specified, perform a case-insensitive match.
270
271 **Example:**
272
273 ```yaml
274 # This activates --conditional if positional number 1 is either "foo" or "bar"
275
276 [...]
277 options:
278 - option_strings: ["--conditional"]
279 when: "positional_contains 1 foo bar"
280
281 positionals:
282 - number: 1
283 [...]
284 ```
285
286 ### positional\_match
287
288 > **Syntax**: `positional_match [NOCASE] NUMBER REGEX`
289
290 > Checks if a positional matches an extended reguar expression.
291
292 > If `NOCASE` is specified, perform a case-insensitive match.
293
294 **Example:**
295
296 ```yaml
297 [...]
298 options:
299 - option_strings: ["--conditional"]
300 when: "positional_match NOCASE 1 'foo[0-9]+'"
301
302 positionals:
303 - number: 1
304 [...]
305 ```
306
307 ### Multiple conditions
308
309 > Multiple conditions can be combined using the logical operators `&&` (**AND**) and `||` (**OR**).
310
311 > Expressions can be grouped using parentheses `(` and `)` to control evaluation order.
312
313 > The negation operator `!` is also supported for logical **NOT** expressions.
314
315 **Example:**
316
317 ```yaml
318 # This activates --conditional if --foo is given but --bar is not given.
319
320 [...]
321 options:
322 - option_strings: ["--conditional"]
323 when: "has_option --foo && ! has_option --bar"
324
325 - option_strings: ["--foo"]
326
327 - option_strings: ["--bar"]
328 [...]
329 ```
330
331 ## Capturing Options
332
333 Options can include a `capture` field to store their values for later use.
334
335 The value of `capture` is the **name of a variable** that will receive all values passed to that option.
336
337 - The captured variable is always an **array**, containing one element for each occurence of the option on the command line.
338 - This makes it easy to implement **context-sensitive completions** that depend on previously supplied option values
339
340 > **NOTE:** In **Fish**, captured variables are stored as global variables.
341 > To avoid interfering with the user's environment, it is highly recommended to use distinctive variable names.
342 > Variable names should begin with an underscore and include a program-specific prefix (for example, `_MYTOOL_DATABASE`).
343
344 **Example:**
345
346 ```yaml
347 prog: my_db_tool
348 options:
349 - option_strings: ["--database", "-d"]
350 complete: ["choices", ["mysql", "postgres", "sqlite"]]
351 capture: "_MYDBTOOL_DATABASE"
352
353 - option_strings: ["--table", "-t"]
354 complete: ["exec", "_my_db_tool_complete_table"]
355 ```
356
357 For **Bash** and **Zsh**:
358
359 ```bash
360 _my_db_tool_complete_table() {
361 case "${_MYDBTOOL_DATABASE[-1]}" in
362 mysql) printf '%s\n' users orders products;;
363 postgres) printf '%s\n' customers invoices transactions;;
364 sqlite) printf '%s\n' local_cache config sessions;;
365 esac
366 }
367 ```
368
369 For **Fish**:
370
371 ```fish
372 function _my_db_tool_complete_table
373 switch $_MYDBTOOL_DATABASE[-1]
374 case mysql; printf '%s\n' users orders products
375 case postgres; printf '%s\n' customers invoices transactions
376 case sqlite; printf '%s\n' local_cache config sessions
377 end
378 end
379 ```
380
381 ## Tips and Tricks
382
383 It is always recommended to define your command line **as precisely as possible**.
384 This helps crazy-complete generate reliable completions. Key practices include:
385 - **Final options**: Use `final: true` for options like `--help` and `--version` that should
386 prevent further options from being completed
387 - **Hidden options**: Use `hidden: true` for options that should be completable but not shown
388 in the suggestion list
389 - **Mutually exclusive options**: Use `groups: [...]` to define sets of options that cannot appear
390 together
391 - **Repeatable options**: Use `repeatable: true` for options that may be used more than once
392
393 ### Trying out zsh autocompletion scripts
394
395 By default, crazy-complete generates scripts that should be installed under `/usr/share/zsh/site-functions`
396 and loaded from there. If you want to try the generated scripts directly, use `--zsh-compdef=False`.
397
398 ### Optimizing Script Output
399
400 Especially for **Fish** scripts, performance can decrease if many options are defined.
401 Features like final options and non-repeatable options require extra conditional code to execute, which can make completions slower.
402
403 To improve performance these features can be completely disabled using:
404
405 ```
406 crazy-complete fish --disable=final,repeatable DEFINITION_FILE
407 ```
408
409 This turns off final and repeatable option handling, reducing script size and improving completion speed