Skip to content

fix: add miscellaneous fixes before implementing _comp_compgen -P #1359

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jun 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions bash_completion
Original file line number Diff line number Diff line change
Expand Up @@ -1352,10 +1352,12 @@ _comp_delimited()
# `show-all-if-ambiguous` on, but even that has cases where it fails
# and the last separator including everything before it is lost.
# https://github.com/scop/bash-completion/pull/913#issuecomment-1490140309
local i
for i in "${!COMPREPLY[@]}"; do
COMPREPLY[i]="$prefix${COMPREPLY[i]}"
done
if [[ $prefix ]]; then
local i
for i in "${!COMPREPLY[@]}"; do
COMPREPLY[i]="$prefix${COMPREPLY[i]}"
done
fi

[[ $delimiter != : ]] || _comp_ltrim_colon_completions "$cur"
}
Expand Down
13 changes: 2 additions & 11 deletions completions/cppcheck
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,8 @@ _comp_cmd_cppcheck()
return
;;
--enable)
# split comma-separated list
local split=""
if [[ $cur == ?*,* ]]; then
prev="${cur%,*}"
cur="${cur##*,}"
split="set"
fi
_comp_compgen -- -W 'all warning style performance portability
information unusedFunction missingInclude' &&
[[ $split ]] &&
_comp_compgen -Rv COMPREPLY -- -P "$prev," -W '"${COMPREPLY[@]}"'
_comp_delimited , -W 'all warning style performance portability
information unusedFunction missingInclude'
return
;;
--error-exitcode)
Expand Down
2 changes: 1 addition & 1 deletion completions/firefox
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ _comp_cmd_firefox()
local cur prev words cword was_split comp_args
_comp_initialize -s -- "$@" || return

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

case $prev in
--help | --version | --display | --UILocale | -MOZ_LOG | --new-window | --new-tab | \
Expand Down
4 changes: 2 additions & 2 deletions completions/make
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,9 @@ _comp_cmd_make()
elif [[ $cur == *=* ]]; then
prev=${cur%%=*}
cur=${cur#*=}
local diropt
local diropt=""
[[ ${prev,,} == *dir?(ectory) ]] && diropt=-d
_comp_compgen_filedir $diropt
_comp_compgen_filedir ${diropt:+"$diropt"}
else
# before we check for makefiles, see if a path was specified
# with -C/--directory
Expand Down
4 changes: 2 additions & 2 deletions completions/openssl
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ _comp_cmd_openssl__compgen_digests()
"$1" dgst -h 2>&1 |
_comp_awk '/^-.*[ \t]to use the .* message digest algorithm/ { print $1 }'
)"
_comp_compgen -ac "${cur#-}" split -P "-" -- "$("$1" help 2>&1 |
_comp_compgen_split -- "$("$1" help 2>&1 |
command sed -ne '/^Message Digest commands/,/^[[:space:]]*$/p' |
command sed -e 1d)"
command sed -e '1d;s/^/-/')"
}

_comp_cmd_openssl()
Expand Down
4 changes: 2 additions & 2 deletions completions/strings
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ _comp_cmd_strings()
[[ ${COMPREPLY-} == *= ]] && compopt -o nospace
return
elif [[ $cur == @* ]]; then
_comp_compgen -c "${cur:1}" filedir
COMPREPLY=("${COMPREPLY[@]/#/@}")
_comp_compgen -c "${cur:1}" filedir &&
COMPREPLY=("${COMPREPLY[@]/#/@}")
return
fi

Expand Down
2 changes: 1 addition & 1 deletion completions/tshark
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

_comp_cmd_tshark()
{
local cur prev words cword comp_args prefix
local cur prev words cword comp_args prefix=""
_comp_initialize -n : -- "$@" || return

case $cur in
Expand Down
11 changes: 6 additions & 5 deletions doc/api-and-naming.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,8 @@ The exit status is implementation-defined.

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

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

Expand Down
10 changes: 10 additions & 0 deletions doc/styleguide.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,16 @@ E.g. write `${foo[bar]}`, not `${foo[$bar]}`, and similarly `${foo[bar+1]}`
vs `${foo[((bar+1))]}` or `${foo[$((bar+1))]}`, `${foo[--i]}` vs
`${foo[((--i))]}`.

## `((${#array[@]})) && cmd "${array[@]}"`

When one uses the array expansion of the form `"${array[@]}"` or `${array[*]}`,
please make sure that the `array` is not empty. This is because of a strange
behavior of Bash <= 4.3, where `${array[@]}` and `${array[*]}` fail for the
`nounset` error when `array` is an empty array. It can be explicitly tested
with `((${#array[@]}))`, or the exit status of `_comp_compgen` that assigned
values to the array can be used (when the generator is designed to return the
appropriate exit status).

## Loop variable names

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