bash-completion: Add support for cmake --build --target

With `ninja` and `make` we can ask for a list of target names.

Fixes: #27695
This commit is contained in:
ClausKlein
2026-03-15 12:51:58 +01:00
committed by Brad King
parent bb5da66f1f
commit 56780517be

View File

@@ -114,6 +114,31 @@ _cmake()
_filedir -d
return
;;
--target)
local quoted
printf -v quoted %q "$cur"
# Options allowed right after `--target`
local target_options='--verbose --clean-first --config --'
local build_dir="build"
for ((i=0; i < ${#COMP_WORDS[@]}; i++)); do
if [[ "${COMP_WORDS[i]}" == "--build" ]]; then
build_dir="${COMP_WORDS[i+1]}"
break
fi
done
if [[ "$cur" == -* ]]; then
COMPREPLY=( $( compgen -W "$target_options" -- "$quoted" ) )
else
local targets=$( cmake --build "$build_dir" --target help 2>/dev/null \
| sed -n -e 's/^... \([^ ]*\).*$/\1/p' \
-e '/^\[/d' -e 's/^\([^ :]*\):.*$/\1/p' | \
grep -v '^/' | sort -u )
COMPREPLY=( $( compgen -W "$targets $target_options" -- "$quoted" ) )
fi
return
;;
--install|--open)
_filedir -d
return