Skip to content

Commit 0384bd5

Browse files
committed
feat: add _comp_locate_first_arg
1 parent 3047c77 commit 0384bd5

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

bash_completion

+20-6
Original file line numberDiff line numberDiff line change
@@ -2156,16 +2156,16 @@ _comp_realcommand()
21562156
fi
21572157
}
21582158

2159-
# This function returns the first argument, excluding options
2159+
# This function returns the position of the first argument, excluding options
21602160
#
21612161
# Options:
21622162
# -a GLOB Pattern of options that take an option argument
21632163
#
2164-
# @var[out] ret First argument before current being completed if any, or
2165-
# otherwise an empty string
2164+
# @var[out] ret Position of the first argument before the current one being
2165+
# completed if any, or otherwise an empty string
21662166
# @return True (0) if any argument is found, False (> 0) otherwise.
21672167
# @since 2.12
2168-
_comp_get_first_arg()
2168+
_comp_locate_first_arg()
21692169
{
21702170
local has_optarg=""
21712171
local OPTIND=1 OPTARG="" OPTERR=0 _opt
@@ -2187,16 +2187,30 @@ _comp_get_first_arg()
21872187
if [[ $has_optarg && ${words[i]} == $has_optarg ]]; then
21882188
((i++))
21892189
elif [[ ${words[i]} != -?* ]]; then
2190-
ret=${words[i]}
2190+
ret=$i
21912191
return 0
21922192
elif [[ ${words[i]} == -- ]]; then
2193-
((i + 1 < cword)) && ret=${words[i + 1]} && return 0
2193+
((i + 1 < cword)) && ret=$((i + 1)) && return 0
21942194
break
21952195
fi
21962196
done
21972197
return 1
21982198
}
21992199

2200+
# This function returns the first argument, excluding options
2201+
#
2202+
# Options:
2203+
# -a GLOB Pattern of options that take an option argument
2204+
#
2205+
# @var[out] ret First argument before the current one being completed if any,
2206+
# or otherwise an empty string
2207+
# @return True (0) if any argument is found, False (> 0) otherwise.
2208+
# @since 2.12
2209+
_comp_get_first_arg()
2210+
{
2211+
_comp_locate_first_arg "$@" && ret=${words[ret]}
2212+
}
2213+
22002214
# This function counts the number of args, excluding options
22012215
# @param $1 chars Characters out of $COMP_WORDBREAKS which should
22022216
# NOT be considered word breaks. See _comp__reassemble_words.

0 commit comments

Comments
 (0)