Skip to content

refactor: adjust to util/xfunc primary command convention #965

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

Merged
merged 17 commits into from
May 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
56a7cec
refactor(rpm): adjust to util/xfunc primary command convention
scop May 7, 2023
0f927c0
refactor(ssh): `_comp_cmd_ssh__suboption` signature to convention
scop May 21, 2023
185b063
refactor(ssh): `_comp_xfunc_ssh_suboption_check` main command convention
scop May 22, 2023
d55f5e6
feat(scp,sftp): prefer `ssh` from same dir to resolve options etc
scop May 22, 2023
8a0a71e
fix(ssh-keygen): first arg to ssh query for protocol versions
scop May 22, 2023
d569ea2
fix(ssh-keygen): call ssh, not -keygen to resolve supported protocols
scop May 22, 2023
473278f
fix(ssh-copy-id): call ssh, not -copy-id for suboption completion check
scop May 22, 2023
5c1d270
feat(ssh-copy-id,ssh-keygen): prefer `ssh` from same dir
scop May 22, 2023
898926c
refactor(ssh): `_comp_xfunc_ssh_query` main command convention
scop May 22, 2023
01611dd
refactor(perl): `_comp_cmd_perl__helper` main command convention
scop May 22, 2023
5085d6b
refactor(pytest): `...__option_choice_args` main command convention
scop May 22, 2023
d9bf3f8
refactor(python): `_comp_xfunc_python_modules` main command convention
scop May 22, 2023
c14986a
refactor(aspell): `_comp_cmd_aspell__dictionary` main command convention
scop May 22, 2023
0d27563
refactor(carton): utility function main command convention
scop May 22, 2023
80da3de
refactor(iconv): `_comp_xfunc_iconv_charsets` main command convention
scop May 22, 2023
a4294f3
refactor(pylint): `_comp_cmd_pylint__message_ids` main cmd convention
scop May 22, 2023
643886c
feat(mysql): prefer `mysqlshow` from same dir
scop May 23, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion completions/aspell
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

_comp_cmd_aspell__dictionary()
{
local datadir aspell=${1:-aspell}
local datadir aspell=$1
datadir=$("$aspell" config data-dir 2>/dev/null || echo /usr/lib/aspell)
# First, get aliases (dicts dump does not list them)
COMPREPLY=($datadir/*.alias)
Expand Down
4 changes: 2 additions & 2 deletions completions/carton
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

_comp_cmd_carton__commands()
{
local cmds=$("${1:-carton}" usage 2>&1 |
local cmds=$("$1" usage 2>&1 |
command sed -ne '/.*command.* is one of/{n;p;q;}')
_comp_compgen -aF $' \t\n,' -- -W "$cmds"
}

_comp_cmd_carton__command_help()
{
local help=$(PERLDOC_PAGER=cat PERLDOC=-otext "${1:-carton}" -h "$2" 2>&1)
local help=$(PERLDOC_PAGER=cat PERLDOC=-otext "$1" -h "$2" 2>&1)
_comp_compgen -a -- -W '$help'
}

Expand Down
17 changes: 13 additions & 4 deletions completions/iconv
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,20 @@
# @since 2.12
_comp_xfunc_iconv_charsets()
{
COMPREPLY+=($(compgen -X ... -W '$("${1:-iconv}" -l | \
command sed -e "s@/*\$@@" -e "s/[,()]//g")' -- "$cur"))
_comp_cmd_iconv__charsets iconv
}

# @deprecated 2.12 use `_comp_xfunc_iconv_charsets` instead
_iconv_charsets()
{
_comp_cmd_iconv__charsets "${1:-iconv}"
}

_comp_deprecate_func 2.12 _iconv_charsets _comp_xfunc_iconv_charsets
_comp_cmd_iconv__charsets()
{
COMPREPLY+=($(compgen -X ... -W '$("$1" -l | \
command sed -e "s@/*\$@@" -e "s/[,()]//g")' -- "$cur"))
}

_comp_cmd_iconv()
{
Expand All @@ -22,7 +31,7 @@ _comp_cmd_iconv()
return
;;
--from-code | --to-code | -${noargopts}[ft])
_comp_xfunc_iconv_charsets "$1"
_comp_cmd_iconv__charsets "$1"
return
;;
--output | -${noargopts}o)
Expand Down
4 changes: 4 additions & 0 deletions completions/mysql
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ _comp_cmd_mysql()
local cur prev words cword was_split comp_args
_comp_initialize -s -- "$@" || return

# Prefer `mysqlshow` in the same dir as the command
local pathcmd
pathcmd=$(type -P "$1") && local PATH=${pathcmd%/*}:$PATH

local noargopts='!(-*|*[uDhSPeI]*)'
# shellcheck disable=SC2254
case $prev in
Expand Down
15 changes: 8 additions & 7 deletions completions/perl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
_comp_cmd_perl__helper()
{
COMPREPLY=($(compgen -P "$prefix" -W \
"$("${2:-perl}" "${BASH_SOURCE[0]%/*}/../helpers/perl" "$1" "$cur")" \
"$("${1:-perl}" "${BASH_SOURCE[0]%/*}/../helpers/perl" "$2" "$cur")" \
-- "$cur"))
[[ $1 == functions ]] || _comp_ltrim_colon_completions "$prefix$cur"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I think this line should have also been updated as [[ $2 == functions ]].

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@scop Can you include it in #988?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, good catch, done in b85263a.

}
Expand Down Expand Up @@ -41,7 +41,7 @@ _comp_cmd_perl()
temp="${cur#-}"
prefix+=${cur%"$temp"}
cur="$temp"
_comp_cmd_perl__helper modules "$1"
_comp_cmd_perl__helper "$1" modules
return
;;
-*V)
Expand All @@ -61,7 +61,7 @@ _comp_cmd_perl()
temp="${cur#:}"
prefix=$prefix${cur%"$temp"}
cur="Devel::$temp"
_comp_cmd_perl__helper modules "$1"
_comp_cmd_perl__helper "$1" modules
fi
;;
esac
Expand Down Expand Up @@ -106,8 +106,9 @@ _comp_cmd_perldoc()
prefix=$prev
fi

local perl="${1%doc}"
[[ $perl == "$1" ]] || ! type "$perl" &>/dev/null && perl=
# Prefer `perl` in the same dir in utility functions
local pathcmd
pathcmd=$(type -P "$1") && local PATH=${pathcmd%/*}:$PATH

case $prev in
-*[hVnoMwL])
Expand All @@ -118,7 +119,7 @@ _comp_cmd_perldoc()
return
;;
-*f)
_comp_cmd_perl__helper functions "$perl"
_comp_cmd_perl__helper "" functions
return
;;
esac
Expand All @@ -128,7 +129,7 @@ _comp_cmd_perldoc()
else
# return available modules (unless it is clearly a file)
if [[ $cur != @(*/|[.~])* ]]; then
_comp_cmd_perl__helper perldocs "$perl"
_comp_cmd_perl__helper "" perldocs
if [[ $cur == p* ]]; then
COMPREPLY+=($(compgen -W \
'$(PERLDOC_PAGER=cat "$1" -u perl | \
Expand Down
7 changes: 4 additions & 3 deletions completions/pydoc
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ _comp_cmd_pydoc()
_comp_compgen -- -W 'keywords topics modules'

if ! _comp_looks_like_path "$cur"; then
local python=python
[[ ${1##*/} == *3* ]] && python=python3
_comp_xfunc python modules $python
# Prefer python in the same dir for resolving modules
local pathcmd
pathcmd=$(type -P "$1") && local PATH=${pathcmd%/*}:$PATH
_comp_xfunc python modules
fi

# Note that we don't do "pydoc modules" as it is known to hang on
Expand Down
4 changes: 2 additions & 2 deletions completions/pylint
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ _comp_cmd_pylint__message_ids()
# again later?
local msgs="$(
set -o pipefail
${1:-pylint} --list-msgs-enabled 2>/dev/null |
"$1" --list-msgs-enabled 2>/dev/null |
command sed -ne "$filter" |
command sed -ne 's/^[[:space:]]\{1,\}\([a-z-]\{6,\}\).*/\1/p' ||
${1:-pylint} --list-msgs 2>/dev/null |
"$1" --list-msgs 2>/dev/null |
command sed -ne 's/^:\([a-z-]\{6,\}\).*/\1/p'
)"
_comp_delimited , -W "$msgs"
Expand Down
4 changes: 2 additions & 2 deletions completions/pytest
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

_comp_cmd_pytest__option_choice_args()
{
local modes=$("${2:-pytest}" "$1=bash-completion-nonexistent" 2>&1 |
local modes=$("$1" "$2=bash-completion-nonexistent" 2>&1 |
command sed -e 's/[^[:space:][:alnum:]-]\{1,\}//g' \
-ne 's/.*choose from //p')
_comp_compgen -a -- -W '$modes'
Expand Down Expand Up @@ -87,7 +87,7 @@ _comp_cmd_pytest()
return
;;
--dist | --vcr-record?(-mode))
_comp_cmd_pytest__option_choice_args "$prev" "$1"
_comp_cmd_pytest__option_choice_args "$1" "$prev"
return
;;
esac
Expand Down
18 changes: 15 additions & 3 deletions completions/python
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,22 @@

# @since 2.12
_comp_xfunc_python_modules()
{
local python=python
[[ ${comp_args[0]##*/} == *3* ]] && python=python3
_comp_cmd_python__modules "$python"
}

# @deprecated 2.12 use `_comp_xfunc_python_modules` instead
_python_modules()
{
_comp_cmd_python__modules "${1:-python}"
}

_comp_cmd_python__modules()
{
COMPREPLY+=($(compgen -W \
"$("${1:-python}" "${BASH_SOURCE[0]%/*}/../helpers/python" "$cur" \
"$("$1" "${BASH_SOURCE[0]%/*}/../helpers/python" "$cur" \
2>/dev/null)" -- "$cur"))
}

Expand All @@ -15,7 +28,6 @@ _comp_xfunc_python_warning_actions()
${prefix:+-P "$prefix"}
}

_comp_deprecate_func 2.12 _python_modules _comp_xfunc_python_modules
_comp_deprecate_func 2.12 _python_warning_actions \
_comp_xfunc_python_warning_actions

Expand All @@ -39,7 +51,7 @@ _comp_cmd_python()
return
;;
-${noargopts}m)
_comp_xfunc_python_modules "$1"
_comp_cmd_python__modules "$1"
return
;;
-${noargopts}Q)
Expand Down
37 changes: 26 additions & 11 deletions completions/rpm
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@

# @since 2.12
_comp_xfunc_rpm_installed_packages()
{
_comp_cmd_rpm__installed_packages rpm
}

_comp_cmd_rpm__installed_packages()
{
if [[ -r /var/log/rpmpkgs &&
/var/log/rpmpkgs -nt /var/lib/rpm/Packages ]]; then
Expand Down Expand Up @@ -38,13 +43,20 @@ _comp_cmd_rpm__macros()
-- "$cur"))
}

# shellcheck disable=SC2120
_comp_cmd_rpm__buildarchs()
{
COMPREPLY=($(compgen -W "$("${1:-rpm}" --showrc | command sed -ne \
's/^\s*compatible\s\s*build\s\s*archs\s*:\s*\(.*\)/\1/ p')" \
-- "$cur"))
}

# shellcheck disable=SC2120
_comp_cmd_rpm__configdir()
{
cfgdir=$("${1:-rpm}" --eval '%{_rpmconfigdir}' 2>/dev/null)
}

# rpm(8) completion
#
_comp_cmd_rpm()
Expand Down Expand Up @@ -147,7 +159,7 @@ _comp_cmd_rpm()
_comp_compgen -- -W '"${opts[@]}" --allmatches --noscripts
--notriggers --nodeps --test --repackage'
else
_comp_xfunc_rpm_installed_packages "$1"
_comp_cmd_rpm__installed_packages "$1"
fi
;;
-q* | --query)
Expand Down Expand Up @@ -190,7 +202,7 @@ _comp_cmd_rpm()
--whatrecommends --whatrequires --whatsuggests
--whatsupplements'
elif [[ ${words[*]} != *\ -@(*([^ -])a|-all )* ]]; then
_comp_xfunc_rpm_installed_packages "$1"
_comp_cmd_rpm__installed_packages "$1"
fi
fi
;;
Expand All @@ -216,14 +228,14 @@ _comp_cmd_rpm()
elif [[ ${words[*]} == *\ -@(*([^ -])p|-package )* ]]; then
_comp_compgen_filedir '[rs]pm'
else
_comp_xfunc_rpm_installed_packages "$1"
_comp_cmd_rpm__installed_packages "$1"
fi
;;
--resign | --addsign | --delsign)
_comp_compgen_filedir '[rs]pm'
;;
--setperms | --setgids)
_comp_xfunc_rpm_installed_packages "$1"
_comp_cmd_rpm__installed_packages "$1"
;;
--import | --dbpath | --root)
if [[ $cur == -* ]]; then
Expand All @@ -242,30 +254,33 @@ _comp_cmd_rpmbuild()
local cur prev words cword was_split comp_args
_comp_initialize -s -- "$@" || return

local rpm="${1%build*}"
[[ $rpm == "$1" ]] || ! type "$rpm" &>/dev/null && rpm=

local noargopts='!(-*|*[rED]*)'
# shellcheck disable=SC2254
# shellcheck disable=SC2119,SC2254
case $prev in
--buildroot | --root | --dbpath | -${noargopts}r)
_comp_compgen_filedir -d
return
;;
--target | --eval | -${noargopts}E | --buildpolicy)
# Prefer `rpm` in the same dir in utility functions
local pathcmd
pathcmd=$(type -P "$1") && local PATH=${pathcmd%/*}:$PATH
Copy link
Owner Author

@scop scop May 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The benefit of prepending to $PATH as opposed to trying to figure out the exact command is that we don't need to poke into the internals of the utility function even for file-local use, and the utility function's default command basename persists.

In some cases we may want to pass the full path, that's still possible, but I suppose the $PATH modification use case would be a more common one for cases like this. When the utility command's command is the same as the one being completed, passing "$1" is the way to go.

;;&
--target)
_comp_cmd_rpm__buildarchs "$rpm"
_comp_cmd_rpm__buildarchs
return
;;
--eval | -${noargopts}E)
_comp_cmd_rpm__macros "$rpm"
_comp_cmd_rpm__macros
return
;;
--macros | --rcfile)
_comp_compgen_filedir
return
;;
--buildpolicy)
local cfgdir=$("$rpm" --eval '%{_rpmconfigdir}' 2>/dev/null)
local cfgdir
_comp_cmd_rpm__configdir
if [[ $cfgdir ]]; then
COMPREPLY=($(compgen -W "$(command ls "$cfgdir" 2>/dev/null |
command sed -ne 's/^brp-//p')" -- "$cur"))
Expand Down
Loading