Skip to content

Commit

Permalink
Handle greedy options in Bash completion
Browse files Browse the repository at this point in the history
Fixes: #1279
  • Loading branch information
praiskup committed Feb 13, 2024
1 parent f7f0ed6 commit b5c985d
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
33 changes: 31 additions & 2 deletions mock/etc/bash_completion.d/mock
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,32 @@ _mock_isopt()
return 1
}

_mock_isopt_multiarg() {
local the_option=$1
_mock_isopt "$the_option" || return 1
case $the_option in
--*)
the_option=${the_option##--}
;;
esac
case $the_option in
rebuild|install|installdeps|remove|chain|update|copyin|copyout|pm-cmd|yum-cmd|dnf-cmd|chroot|shell)
return 0
;;
esac
return 1
}

_mock()
{
local cur prev words cword split
_init_completion -s || return

local cfgdirs=( /etc/mock "$HOME/.config/mock" )
local count=0
local prevopt
local greedyopt=rebuild # the default mode eating srpms
local prevopt=rebuild

for word in "${words[@]}" ; do
if [[ $count -eq $cword ]] ; then
# If the last (i.e. current) argument is an option, clear prevopt so that we complete
Expand All @@ -51,7 +69,18 @@ _mock()
fi
# Record the option argument previous to the current argument to determine the type of
# completion that is needed
_mock_isopt "$word" && prevopt=$word
if _mock_isopt_multiarg "$word"; then
prevopt=$word
# last greedy option wins
greedyopt=$word
elif _mock_isopt "$word"; then
prevopt=$word
else
# Revert back to the last greedy option. E.g. for 'mock -r fedora-rawhide-x86_64 <tab>',
# we want to react on the default 'rebuild' mode, not `-r`.
prevopt=$greedyopt
fi

if [[ "$word" == --configdir ]] ; then
cfgdirs=( "${words[((count+1))]}" )
elif [[ "$word" == --configdir=* ]] ; then
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
The Bash completion script has been fixed to properly complete arguments
of [multi-arg options][issue#1279] like `--install` or `--chain`. This
bug-fix is a follow-up for fix related to [issue#746][].

0 comments on commit b5c985d

Please sign in to comment.