Skip to content

Commit 6bd5e26

Browse files
authored
Merge pull request #1359 from akinomyoga/_comp_compgen-P-0
fix: add miscellaneous fixes before implementing `_comp_compgen -P`
2 parents 3cdb549 + 594706a commit 6bd5e26

File tree

9 files changed

+32
-28
lines changed

9 files changed

+32
-28
lines changed

bash_completion

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1352,10 +1352,12 @@ _comp_delimited()
13521352
# `show-all-if-ambiguous` on, but even that has cases where it fails
13531353
# and the last separator including everything before it is lost.
13541354
# https://github.com/scop/bash-completion/pull/913#issuecomment-1490140309
1355-
local i
1356-
for i in "${!COMPREPLY[@]}"; do
1357-
COMPREPLY[i]="$prefix${COMPREPLY[i]}"
1358-
done
1355+
if [[ $prefix ]]; then
1356+
local i
1357+
for i in "${!COMPREPLY[@]}"; do
1358+
COMPREPLY[i]="$prefix${COMPREPLY[i]}"
1359+
done
1360+
fi
13591361

13601362
[[ $delimiter != : ]] || _comp_ltrim_colon_completions "$cur"
13611363
}

completions/cppcheck

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,8 @@ _comp_cmd_cppcheck()
1616
return
1717
;;
1818
--enable)
19-
# split comma-separated list
20-
local split=""
21-
if [[ $cur == ?*,* ]]; then
22-
prev="${cur%,*}"
23-
cur="${cur##*,}"
24-
split="set"
25-
fi
26-
_comp_compgen -- -W 'all warning style performance portability
27-
information unusedFunction missingInclude' &&
28-
[[ $split ]] &&
29-
_comp_compgen -Rv COMPREPLY -- -P "$prev," -W '"${COMPREPLY[@]}"'
19+
_comp_delimited , -W 'all warning style performance portability
20+
information unusedFunction missingInclude'
3021
return
3122
;;
3223
--error-exitcode)

completions/firefox

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ _comp_cmd_firefox()
55
local cur prev words cword was_split comp_args
66
_comp_initialize -s -- "$@" || return
77

8-
[[ $cur == -MOZ_LOG*=* ]] && prev=${cur%%=*} cur=${cur#*=}
8+
[[ ! $was_split && $cur == -MOZ_LOG*=* ]] && prev=${cur%%=*} cur=${cur#*=}
99

1010
case $prev in
1111
--help | --version | --display | --UILocale | -MOZ_LOG | --new-window | --new-tab | \

completions/make

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,9 @@ _comp_cmd_make()
110110
elif [[ $cur == *=* ]]; then
111111
prev=${cur%%=*}
112112
cur=${cur#*=}
113-
local diropt
113+
local diropt=""
114114
[[ ${prev,,} == *dir?(ectory) ]] && diropt=-d
115-
_comp_compgen_filedir $diropt
115+
_comp_compgen_filedir ${diropt:+"$diropt"}
116116
else
117117
# before we check for makefiles, see if a path was specified
118118
# with -C/--directory

completions/openssl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ _comp_cmd_openssl__compgen_digests()
3232
"$1" dgst -h 2>&1 |
3333
_comp_awk '/^-.*[ \t]to use the .* message digest algorithm/ { print $1 }'
3434
)"
35-
_comp_compgen -ac "${cur#-}" split -P "-" -- "$("$1" help 2>&1 |
35+
_comp_compgen_split -- "$("$1" help 2>&1 |
3636
command sed -ne '/^Message Digest commands/,/^[[:space:]]*$/p' |
37-
command sed -e 1d)"
37+
command sed -e '1d;s/^/-/')"
3838
}
3939

4040
_comp_cmd_openssl()

completions/strings

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ _comp_cmd_strings()
3838
[[ ${COMPREPLY-} == *= ]] && compopt -o nospace
3939
return
4040
elif [[ $cur == @* ]]; then
41-
_comp_compgen -c "${cur:1}" filedir
42-
COMPREPLY=("${COMPREPLY[@]/#/@}")
41+
_comp_compgen -c "${cur:1}" filedir &&
42+
COMPREPLY=("${COMPREPLY[@]/#/@}")
4343
return
4444
fi
4545

completions/tshark

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
_comp_cmd_tshark()
44
{
5-
local cur prev words cword comp_args prefix
5+
local cur prev words cword comp_args prefix=""
66
_comp_initialize -n : -- "$@" || return
77

88
case $cur in

doc/api-and-naming.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,8 @@ The exit status is implementation-defined.
172172

173173
- The `_comp_compgen -- COMPGEN_ARGS` returns whether there is at least one
174174
completion. This is useful when one wants to reuse the array content with
175-
`"${tmp[@]}"` avoiding `nounset` error.
175+
`"${tmp[@]}"` avoiding `nounset` error in Bash <= 4.3 for an empty array
176+
`tmp`.
176177
- Some use other rules for the exit status. E.g., `help` and `usage` return
177178
whether there were options *before* filtering by cur. This is used for
178179
`_comp_compgen_help || _comp_compgen_usage`.
@@ -206,8 +207,8 @@ in `_comp_compgen_mygen1`.
206207
_comp_compgen_mygen1()
207208
{
208209
local -a arr=(1 2 3)
209-
_comp_compgen -av arr -- -W '4 5 6'
210-
_comp_compgen_set "${arr[@]/#p}"
210+
_comp_compgen -av arr -- -W '4 5 6' &&
211+
_comp_compgen_set "${arr[@]/#p}"
211212
}
212213

213214
_comp_compgen -v arr mygen1 # fails to get the result in array `arr`
@@ -222,8 +223,8 @@ assigning the final result.
222223
_comp_compgen_mygen1()
223224
{
224225
local -a arr=(1 2 3)
225-
_comp_compgen -av arr -- -W '4 5 6'
226-
_comp_compgen -U arr set "${arr[@]/#p}"
226+
_comp_compgen -av arr -- -W '4 5 6' &&
227+
_comp_compgen -U arr set "${arr[@]/#p}"
227228
}
228229
```
229230

doc/styleguide.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,16 @@ E.g. write `${foo[bar]}`, not `${foo[$bar]}`, and similarly `${foo[bar+1]}`
111111
vs `${foo[((bar+1))]}` or `${foo[$((bar+1))]}`, `${foo[--i]}` vs
112112
`${foo[((--i))]}`.
113113

114+
## `((${#array[@]})) && cmd "${array[@]}"`
115+
116+
When one uses the array expansion of the form `"${array[@]}"` or `${array[*]}`,
117+
please make sure that the `array` is not empty. This is because of a strange
118+
behavior of Bash <= 4.3, where `${array[@]}` and `${array[*]}` fail for the
119+
`nounset` error when `array` is an empty array. It can be explicitly tested
120+
with `((${#array[@]}))`, or the exit status of `_comp_compgen` that assigned
121+
values to the array can be used (when the generator is designed to return the
122+
appropriate exit status).
123+
114124
## Loop variable names
115125

116126
Use `i`, `j`, `k` for loop-local indices; `n` and `m` for lengths; some other

0 commit comments

Comments
 (0)