Skip to content

Commit 47d7683

Browse files
committed
docs: describe uses of _comp_compgen_split
1 parent 7b2dc13 commit 47d7683

File tree

2 files changed

+23
-14
lines changed

2 files changed

+23
-14
lines changed

CONTRIBUTING.md

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -117,15 +117,21 @@ Also, please bear the following coding guidelines in mind:
117117
variables are set (e.g. with `[[ -v varname ]]`) or use default
118118
expansion (e.g. `${varname-}`).
119119

120-
- Prefer `compgen -W '...' -- $cur` over embedding `$cur` in external
121-
command arguments (often e.g. sed, grep etc) unless there's a good
122-
reason to embed it. Embedding user input in command lines can result
123-
in syntax errors and other undesired behavior, or messy quoting
124-
requirements when the input contains unusual characters. Good
125-
reasons for embedding include functionality (if the thing does not
126-
sanely work otherwise) or performance (if it makes a big difference
127-
in speed), but all embedding cases should be documented with
128-
rationale in comments in the code.
120+
- Prefer `_comp_compgen_split -- "$(...)"` over embedding `$cur` in external
121+
command arguments (often e.g. sed, grep etc) unless there's a good reason to
122+
embed it. Embedding user input in command lines can result in syntax errors
123+
and other undesired behavior, or messy quoting requirements when the input
124+
contains unusual characters. Good reasons for embedding include
125+
functionality (if the thing does not sanely work otherwise) or performance
126+
(if it makes a big difference in speed), but all embedding cases should be
127+
documented with rationale in comments in the code.
128+
129+
Do not use `_comp_compgen -- -W "$(...)"` or `_comp_compgen -- -W '$(...)'`
130+
but always use `_comp_compgen_split -- "$(...)"`. In the former case, when
131+
the command output contains strings looking like shell expansions, the
132+
expansions will be unexpectedly performed, which becomes a vulnerability. In
133+
the latter case, checks by shellcheck and shfmt will not be performed inside
134+
`'...'`. Also, `_comp_compgen_split` is `IFS`-safe.
129135

130136
- When completing available options, offer only the most descriptive
131137
ones as completion results if there are multiple options that do the

doc/api-and-naming.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -145,11 +145,14 @@ specified by `-v var` in `OPTS`).
145145
### Implementing a generator function
146146

147147
To implement a generator function, one should generate completion candidates by
148-
calling `_comp_compgen` or other generators. To avoid conflicts with the
149-
options specified to `_comp_compgen`, one should not directly modify or
150-
reference the target variable. When post-filtering is needed, store them in
151-
local arrays, filter them, and finally append them by `_comp_compgen -- -W
152-
"${arr[@]}"`.
148+
calling `_comp_compgen` or other generators.
149+
150+
To avoid conflicts with the options specified to `_comp_compgen`, one should
151+
not directly modify or reference the target variable. When post-filtering is
152+
needed, store them in a local array, filter them, and finally append them by
153+
`_comp_compgen -- -W '"${arr[@]}"'`. To split the output of commands and
154+
append the results to the target variable, use `_comp_compgen_split -- "$(cmd
155+
...)"` instead of using `_comp_split COMPREPLY "$(cmd ...)"`.
153156

154157
A generator function should replace the existing content of the variable by
155158
default. When the appending behavior is favored, the caller should specify it

0 commit comments

Comments
 (0)