Skip to content

Commit

Permalink
refactor: use _comp_get_option_arg
Browse files Browse the repository at this point in the history
  • Loading branch information
akinomyoga committed May 13, 2024
1 parent c97f70c commit 014b757
Show file tree
Hide file tree
Showing 20 changed files with 74 additions and 210 deletions.
11 changes: 3 additions & 8 deletions completions/_chsh
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,9 @@ _comp_cmd_chsh()
local cur prev words cword comp_args
_comp_initialize -- "$@" || return

local word chroot="" has_chroot=""
for word in "${words[@]}"; do
if [[ $has_chroot ]]; then
chroot=$word
break
fi
[[ $word != -@(R|-root) ]] || has_chroot=set
done
local REPLY
_comp_get_option_arg -A -- "-@(R|-root)"
local chroot=$REPLY

case $prev in
--list-shells | --help | -v | --version)
Expand Down
13 changes: 2 additions & 11 deletions completions/_mock
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,8 @@ _comp_cmd_mock()
_comp_initialize -s -- "$@" || return

local plugins='tmpfs root_cache yum_cache bind_mount ccache'
local cfgdir=/etc/mock count=0 i

for i in "${words[@]}"; do
((count == cword)) && break
if [[ $i == --configdir ]]; then
cfgdir="${words[count + 1]}"
elif [[ $i == --configdir=* ]]; then
cfgdir=${i/*=/}
fi
((count++))
done
local cfgdir=/etc/mock REPLY
_comp_get_option_arg -- "--configdir" && cfgdir=$REPLY

case $prev in
-h | --help | --copyin | --copyout | --arch | -D | --define | --with | --without | \
Expand Down
14 changes: 5 additions & 9 deletions completions/ant
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,11 @@ _comp_cmd_ant()
else
# available targets completion
# find which buildfile to use
local buildfile=build.xml i
for ((i = 1; i < cword; i++)); do
if [[ ${words[i]} == -@(?(build)file|f) ]]; then
buildfile=${words[i + 1]}
break
fi
done
if ((i == cword)); then
local IFS=$' \t\n'
local buildfile=build.xml REPLY
if _comp_get_option_arg -- "-@(?(build)file|f)"; then
buildfile=$REPLY
else
local IFS=$' \t\n' i
for i in ${ANT_ARGS-}; do
if [[ $prev == -@(?(build)file|f) ]]; then
buildfile=$i
Expand Down
9 changes: 2 additions & 7 deletions completions/cfrun
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,8 @@ _comp_cmd_cfrun()
if [[ $cur == -* ]]; then
_comp_compgen -- -W '-f -h -d -S -T -v'
else
local i hostfile=${CFINPUTS:-/var/lib/cfengine/inputs}/cfrun.hosts
for ((i = 1; i < cword; i++)); do
if [[ ${words[i]} == -f ]]; then
hostfile=${words[i + 1]}
break
fi
done
local REPLY hostfile=${CFINPUTS:-/var/lib/cfengine/inputs}/cfrun.hosts
_comp_get_option_arg -- "-f" && hostfile=$REPLY
[[ ! -f $hostfile ]] && return

_comp_compgen_split -- "$(command grep -v -E '(=|^$|^#)' \
Expand Down
18 changes: 2 additions & 16 deletions completions/java
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,10 @@ _comp_cmd_java__classpath()
# @return 0 if at least one element is generated, or otherwise 1
_comp_cmd_java__find_classpath()
{
local i

REPLY=

# search first in current options
for ((i = 1; i < cword; i++)); do
if [[ ${words[i]} == -@(cp|classpath) ]]; then
REPLY=${words[i + 1]}
break
fi
done
_comp_get_option_arg -- "-@(cp|classpath)"

# fall back to environment, followed by current directory
_comp_split -F : REPLY "${REPLY:-${CLASSPATH:-.}}"
Expand All @@ -32,17 +25,10 @@ _comp_cmd_java__find_classpath()
# @return 0 if at least one element is generated, or otherwise 1
_comp_cmd_java__find_sourcepath()
{
local i

REPLY=

# search first in current options
for ((i = 1; i < cword; i++)); do
if [[ ${words[i]} == -sourcepath ]]; then
REPLY=${words[i + 1]}
break
fi
done
_comp_get_option_arg -- "-sourcepath"

# fall back to classpath
if [[ ! $REPLY ]]; then
Expand Down
11 changes: 3 additions & 8 deletions completions/lilo
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,9 @@ _comp_cmd_lilo()
;;
-I | -D | -R)
# label completion
local i conf=""
for i in "${!words[@]}"; do
if [[ ${words[i]} == -C ]]; then
conf=${words[i + 1]-}
break
fi
done
_comp_cmd_lilo__labels "$conf"
local REPLY
_comp_get_option_arg -A -- "-C"
_comp_cmd_lilo__labels "$REPLY"
return
;;
-A | -b | -M | -u | -U)
Expand Down
31 changes: 10 additions & 21 deletions completions/make
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ _comp_cmd_make()
local cur prev words cword was_split comp_args
_comp_initialize -s -- "$@" || return

local makef makef_dir=("-C" ".") i
local makef makef_dir=("-C" ".")

local noargopts='!(-*|*[foWICmEDVxj]*)'
# shellcheck disable=SC2254
Expand Down Expand Up @@ -116,29 +116,18 @@ _comp_cmd_make()
else
# before we check for makefiles, see if a path was specified
# with -C/--directory
for ((i = 1; i < ${#words[@]}; i++)); do
if [[ ${words[i]} == @(-${noargopts}C|--directory) ]]; then
# Expand tilde expansion
local REPLY
_comp_dequote "${words[i + 1]-}" &&
[[ -d ${REPLY-} ]] &&
makef_dir=(-C "$REPLY")
break
fi
done
local REPLY
_comp_get_option_arg -A -- "@(-${noargopts}C|--directory)" &&
_comp_dequote "$REPLY" &&
[[ -d $REPLY ]] &&
makef_dir=(-C "$REPLY")

# before we scan for targets, see if a Makefile name was
# specified with -f/--file/--makefile
for ((i = 1; i < ${#words[@]}; i++)); do
if [[ ${words[i]} == @(-${noargopts}f|--?(make)file) ]]; then
# Expand tilde expansion
local REPLY
_comp_dequote "${words[i + 1]-}" &&
[[ -f ${REPLY-} ]] &&
makef=(-f "$REPLY")
break
fi
done
_comp_get_option_arg -A -- "@(-${noargopts}f|--?(make)file)" &&
_comp_dequote "$REPLY" &&
[[ -f $REPLY ]] &&
makef=(-f "$REPLY")

# recognise that possible completions are only going to be displayed so
# only the base name is shown.
Expand Down
10 changes: 3 additions & 7 deletions completions/mdadm
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,9 @@ _comp_cmd_mdadm__raid_level()

_comp_cmd_mdadm__raid_layout()
{
local i level=""
for ((i = 1; i < cword; i++)); do
if [[ ${words[i]} == -@(l|-level) ]]; then
level=${words[i + 1]}
break
fi
done
local REPLY
_comp_get_option_arg -- "-@(l|-level)"
local level=$REPLY

case $level in
raid5)
Expand Down
20 changes: 3 additions & 17 deletions completions/mutt
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,9 @@ _comp_cmd_mutt__addresses()
# @var[out] REPLY muttrc filename
_comp_cmd_mutt__get_muttrc()
{
REPLY=
# Search COMP_WORDS for '-F muttrc' or '-Fmuttrc' argument
set -- "${words[@]}"
while (($# > 0)); do
if [[ $1 == -F* ]]; then
if ((${#1} > 2)); then
_comp_dequote "${1:2}"
else
shift
[[ ${1-} ]] && _comp_dequote "$1"
fi
break
fi
shift
done

if [[ ! $REPLY ]]; then
if ! {
_comp_get_option_arg -A -- "-F" && _comp_dequote "$REPLY" && [[ $REPLY ]]
}; then
if [[ -f ~/.${muttcmd}rc ]]; then
REPLY=\~/.${muttcmd}rc
elif [[ -f ~/.${muttcmd}/${muttcmd}rc ]]; then
Expand Down
9 changes: 3 additions & 6 deletions completions/openssl
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,9 @@ _comp_cmd_openssl__compgen_sections()
local config="" _i _file

# check if a specific configuration file is used
for ((_i = 2; _i < cword; _i++)); do
if [[ ${words[_i]} == -config ]]; then
config=${words[_i + 1]}
break
fi
done
local REPLY
_comp_get_option_arg -o 2 -- "-config" && config=$REPLY
_comp_unlocal REPLY

# if no config given, check some usual default locations
if [[ ! $config ]]; then
Expand Down
15 changes: 4 additions & 11 deletions completions/rpcdebug
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,11 @@

_comp_cmd_rpcdebug__flags()
{
local i module

for ((i = 1; i < ${#words[@]}; i++)); do
if [[ ${words[i]} == -m ]]; then
module=${words[i + 1]-}
break
fi
done

if [[ $module ]]; then
local REPLY
if _comp_get_option_arg -- "-m"; then
local _module=$REPLY
_comp_compgen_split -- "$(rpcdebug -vh 2>&1 |
command sed -ne 's/^'"$module"'[[:space:]]\{1,\}//p')"
command sed -ne 's/^'"$_module"'[[:space:]]\{1,\}//p')"
fi
}

Expand Down
9 changes: 2 additions & 7 deletions completions/rsync
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,8 @@ _comp_cmd_rsync()
;;
*:*)
# find which remote shell is used
local i shell=ssh
for ((i = 1; i < cword; i++)); do
if [[ ${words[i]} == -@(e|-rsh) ]]; then
shell=${words[i + 1]}
break
fi
done
local REPLY shell=ssh
_comp_get_option_arg -- "-@(e|-rsh)" && shell=$REPLY
[[ $shell == ssh ]] && _comp_compgen -x scp remote_files
;;
*)
Expand Down
18 changes: 3 additions & 15 deletions completions/ssh
Original file line number Diff line number Diff line change
Expand Up @@ -262,21 +262,9 @@ _ssh_suboption_check()
# @var[out] configfile Found configfile, if any
_comp_cmd_ssh__configfile()
{
configfile=""
set -- "${words[@]}"
while (($# > 0)); do
if [[ $1 == -F* ]]; then
local REPLY
if ((${#1} > 2)); then
_comp_dequote "${1:2}" && configfile=$REPLY
else
shift
[[ ${1-} ]] && _comp_dequote "$1" && configfile=$REPLY
fi
break
fi
shift
done
local REPLY
_comp_get_option_arg -A -- "-F" && _comp_dequote "$REPLY"
configfile=$REPLY
}

# With $1 set, look for public key files, else private
Expand Down
15 changes: 6 additions & 9 deletions completions/sysbench
Original file line number Diff line number Diff line change
Expand Up @@ -101,15 +101,12 @@ _comp_cmd_sysbench()
esac

# find out which test we're running
local i test="" has_test=""
for ((i = 1; i < ${#words[@]} - 1; i++)); do
# TODO --test= is deprecated, bare test name preferred
if [[ ${words[i]} == --test=* ]]; then
test=${words[i]#*=}
has_test=set
break
fi
done
# TODO --test= is deprecated, bare test name preferred
local REPLY test="" has_test=""
if _comp_get_option_arg -- "--test"; then
test=$REPLY
has_test=set
fi

local opts
_comp_compgen -v opts help
Expand Down
19 changes: 7 additions & 12 deletions completions/tipc
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,14 @@ _comp_cmd_tipc__media()
_comp_cmd_tipc__bearer()
{
local optind=$1
local media="" i

if _comp_cmd_tipc__media "$optind"; then
return
fi

for ((i = 0; i < cword; i++)); do
if [[ ${words[i]} == 'media' ]]; then
media=${words[i + 1]}
fi
done
local REPLY
_comp_get_option_arg "media"
local media=$REPLY

if ((cword == optind + 2)); then
case "$media" in
Expand Down Expand Up @@ -109,19 +106,17 @@ _comp_cmd_tipc()

case "${words[optind]}" in
enable)
local media="" params
local params
((optind++))

if ((cword < optind + 4)); then
_comp_cmd_tipc__bearer $optind
return
fi

for ((i = 0; i < cword; i++)); do
if [[ ${words[i]} == 'media' ]]; then
media=${words[i + 1]}
fi
done
local REPLY
_comp_get_option_arg "media"
local media=$REPLY
case "$media" in
"udp")
local -a params=("localip" "localport" "remoteip"
Expand Down
8 changes: 2 additions & 6 deletions completions/update-alternatives
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,8 @@ _comp_cmd_update_alternatives__installed()
for i in alternatives dpkg/alternatives rpm/alternatives; do
[[ -d /var/lib/$i ]] && admindir=/var/lib/$i && break
done
for ((i = 1; i < cword; i++)); do
if [[ ${words[i]} == --admindir ]]; then
admindir=${words[i + 1]}
break
fi
done
local REPLY
_comp_get_option_arg -- "--admindir" && admindir=$REPLY
[[ -d $admindir ]] && _comp_compgen_split -- "$(command ls "$admindir")"
}

Expand Down

0 comments on commit 014b757

Please sign in to comment.