Skip to content

Commit fe7a3f9

Browse files
committed
refactor(ssh): accept dironly flag as an option
1 parent 456d50a commit fe7a3f9

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

completions/ssh

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -471,17 +471,29 @@ _comp_cmd_scp__path_esc='[][(){}<>"'"'"',:;^&!$=?`\\|[:space:]]'
471471
# "compopt +o nospace" instead, but it would suffix a space to directory names
472472
# unexpectedly.
473473
#
474-
# @param $1 dirs_only - If a non-empty value is specified, only directory names
475-
# are selected.
476-
# @param $2 escape_replacement - If a non-empty value is specified, special
474+
# Options:
475+
# -d Only directory names are selected.
476+
# @param $1 escape_replacement - If a non-empty value is specified, special
477477
# characters are replaced with the specified value (instead of the default
478478
# '\\&').
479479
# @stdin List of filenames in the "ls -1F" format, where filenames are
480480
# separated by newlines, and characters /=@|* are suffixed based on the
481481
# types of the files.
482482
_comp_cmd_scp__escape_path()
483483
{
484-
local dirs_only=$1 escape_replacement=${2:-'\\&'}
484+
local OPTIND=1 OPTARG="" OPTERR=0 opt dirs_only=""
485+
while getopts ':d' _flag "$@"; do
486+
case $_flag in
487+
d) dirs_only=set ;;
488+
*)
489+
echo "bash_completion: $FUNCNAME: usage error: $*" >&2
490+
return 1
491+
;;
492+
esac
493+
done
494+
shift "$((OPTIND - 1))"
495+
local escape_replacement=${1:-'\\&'}
496+
485497
if [[ $dirs_only ]]; then
486498
# escape problematic characters; remove non-dirs
487499
command sed \
@@ -543,7 +555,8 @@ _comp_xfunc_scp_compgen_remote_files()
543555
local _files
544556
_files=$(ssh -o 'Batchmode yes' "$_userhost" \
545557
command ls -aF1dL "$_path*" 2>/dev/null |
546-
_comp_cmd_scp__escape_path "$_dirs_only" "$_escape_replacement")
558+
_comp_cmd_scp__escape_path ${_dirs_only:+'-d'} -- \
559+
"$_escape_replacement")
547560
_comp_compgen -R split -l -- "$_files"
548561
}
549562

@@ -570,7 +583,7 @@ _comp_xfunc_scp_compgen_local_files()
570583
_comp_expand_glob files '"$cur"*' || return 0
571584
_comp_compgen -RU files split -l ${1:+-P "$1"} -- "$(
572585
command ls -aF1dL "${files[@]}" 2>/dev/null |
573-
_comp_cmd_scp__escape_path "$_dirs_only"
586+
_comp_cmd_scp__escape_path ${_dirs_only:+'-d'}
574587
)"
575588
}
576589

0 commit comments

Comments
 (0)