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
48 changes: 38 additions & 10 deletions bash_completion.sh
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,36 @@ _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")

commands_with_input=( "--color" "--config" "-c" "--report" "-r" "--loglevel" "-l" "--editor" ) # Array variable storing commands which require an input argument from the user.

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

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

if [[ "${commands_with_input[*]}" =~ $command ]];
then
((offset+=2))
else
((offset+=1))
fi
fi

done

done

local next=${COMP_WORDS[1+offset]}
local offset_by_two=2+offset
local offset_by_three=3+offset
prathmesh4321 marked this conversation as resolved.
Show resolved Hide resolved

case "$next" in
#case "${prev}" in
Expand Down Expand Up @@ -205,7 +230,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 +243,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[offset_by_two]}" in
shahzebsiddiqui marked this conversation as resolved.
Show resolved Hide resolved
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 +278,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[offset_by_two]}" 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 +317,9 @@ _buildtest ()
COMPREPLY=( $( compgen -W "${cmds}" -- $cur ) )

# switch based on 2nd word 'buildtest buildspec <subcommand>'
case ${COMP_WORDS[2]} in
case ${COMP_WORDS[offset_by_two]} in
find|f)
case ${COMP_WORDS[3]} in
case ${COMP_WORDS[offset_by_three]} in
# completion for 'buildtest buildspec find invalid'
invalid)
local opts="--error --help -e -h"
Expand All @@ -318,7 +343,7 @@ _buildtest ()
esac
;;
summary|sm)
case ${COMP_WORDS[3]} in
case ${COMP_WORDS[offset_by_three]} in
# completion for rest of arguments
*)
local longopts="--help --pager"
Expand Down Expand Up @@ -358,7 +383,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[offset_by_three]} in
find)
COMPREPLY=( $( compgen -W "$(_avail_maintainers)" -- $cur ) );;
esac
Expand All @@ -383,7 +408,7 @@ _buildtest ()
local cmds="--help --pager -h list query"
COMPREPLY=( $( compgen -W "${cmds}" -- $cur ) )

case ${COMP_WORDS[2]} in
case ${COMP_WORDS[offset_by_two]} in
list)
local opts="--help --no-header --terse -h -n -t"
COMPREPLY=( $( compgen -W "${opts}" -- $cur ) )
Expand Down Expand Up @@ -450,6 +475,9 @@ _buildtest ()
if [[ "${prev}" == "--loglevel" ]] || [[ "${prev}" == "-l" ]]; then
COMPREPLY=( $( compgen -W "DEBUG INFO WARNING ERROR CRITICAL" -- $cur ) )
fi
if [[ "${prev}" == "--editor" ]]; then
COMPREPLY=( $( compgen -W "vi vim emacs nano" -- $cur ) )
fi
;;
esac
esac
Expand Down