Skip to content
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

Fix Issue with tab completion for subcommands with top level options #1432

Merged
merged 7 commits into from
Apr 20, 2023
41 changes: 31 additions & 10 deletions bash_completion.sh
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,32 @@ _buildtest ()
{
local cur="${COMP_WORDS[COMP_CWORD]}"
local prev="${COMP_WORDS[COMP_CWORD-1]}"
declare -i offset="0"

COMPREPLY=() # Array variable storing the possible completions.

next=${COMP_WORDS[1]}
declare -a buildtest_opts=("--color" "--config" "--debug" "--editor" "--help" "--helpcolor" "--logpath" "--loglevel" "--print-log" "--no-color" "--report" "--version" "--view-log" "-c" "-d" "-h" "-l" "-p" "-r" "-V")

for command in "${COMP_WORDS[@]}"
do
for element in "${buildtest_opts[@]}"
do

if [[ "$command" == "$element" ]]; then

if [[ "$command" == "--color" ]] || [[ "$command" == "--config" ]] || [[ "$command" == "-c" ]] || [[ "$command" == "--report" ]] || [[ "$command" == "-r" ]];
prathmesh4321 marked this conversation as resolved.
Show resolved Hide resolved
then
((offset+=2))
else
((offset+=1))
fi
fi

done

done

local next=${COMP_WORDS[1+offset]}

case "$next" in
#case "${prev}" in
Expand Down Expand Up @@ -205,7 +226,7 @@ _buildtest ()
COMPREPLY=( $( compgen -W "$(_avail_report_formatfields)" -- $cur ) )
return
esac
case "$prev" in summary)
case "$prev" in summary|sm)
local opts="-d -h --detailed --help"
COMPREPLY=( $( compgen -W "${opts}" -- $cur ) )
return
Expand All @@ -218,15 +239,15 @@ _buildtest ()
COMPREPLY=( $( compgen -W "${cmds}" -- $cur ) )
# handle completion logic for 'buildtest config <subcommand>' based on subcommands

case "${COMP_WORDS[COMP_CWORD-2]}" in
case "${COMP_WORDS[2+offset]}" in
compilers|co)
local opts="--help --json --yaml -h -j -y find test"
COMPREPLY=( $( compgen -W "${opts}" -- $cur ) )
if [[ "${prev}" == "find" ]]; then
local opts="--detailed --help --modulepath --update -d -h -m -u"
COMPREPLY=( $( compgen -W "${opts}" -- $cur ) )
fi
if [[ "${prev}" == "test" ]]; then
if [[ "${prev}" == "test" ]]; then
COMPREPLY=( $( compgen -W "$(_avail_compilers)" -- $cur ) )
fi
;;
Expand All @@ -253,7 +274,7 @@ _buildtest ()
COMPREPLY=( $( compgen -W "${cmds}" -- $cur ) )

# case statement to handle completion for buildtest inspect [name|id|list] command
case "${COMP_WORDS[2]}" in
case "${COMP_WORDS[2+offset]}" in
list|l)
local opts="--builder --help --no-header --pager --terse -b -h -n -t"
COMPREPLY=( $( compgen -W "${opts}" -- $cur ) );;
Expand Down Expand Up @@ -292,9 +313,9 @@ _buildtest ()
COMPREPLY=( $( compgen -W "${cmds}" -- $cur ) )

# switch based on 2nd word 'buildtest buildspec <subcommand>'
case ${COMP_WORDS[2]} in
case ${COMP_WORDS[2+offset]} in
find|f)
case ${COMP_WORDS[3]} in
case ${COMP_WORDS[3+offset]} in
# completion for 'buildtest buildspec find invalid'
invalid)
local opts="--error --help -e -h"
Expand All @@ -318,7 +339,7 @@ _buildtest ()
esac
;;
summary|sm)
case ${COMP_WORDS[3]} in
case ${COMP_WORDS[3+offset]} in
# completion for rest of arguments
*)
local longopts="--help --pager"
Expand Down Expand Up @@ -358,7 +379,7 @@ _buildtest ()
local opts="--breakdown --list --help --terse --no-header -b -h -l -n find"
COMPREPLY=( $( compgen -W "${opts}" -- $cur ) )

case ${COMP_WORDS[3]} in
case ${COMP_WORDS[3+offset]} in
find)
COMPREPLY=( $( compgen -W "$(_avail_maintainers)" -- $cur ) );;
esac
Expand All @@ -383,7 +404,7 @@ _buildtest ()
local cmds="--help --pager -h list query"
COMPREPLY=( $( compgen -W "${cmds}" -- $cur ) )

case ${COMP_WORDS[2]} in
case ${COMP_WORDS[2+offset]} in
list)
local opts="--help --no-header --terse -h -n -t"
COMPREPLY=( $( compgen -W "${opts}" -- $cur ) )
Expand Down