@@ -2189,23 +2189,43 @@ _comp_get_first_arg()
2189
2189
}
2190
2190
2191
2191
# This function counts the number of args, excluding options
2192
- # @param $1 chars Characters out of $COMP_WORDBREAKS which should
2193
- # NOT be considered word breaks. See _comp__reassemble_words.
2194
- # @param $2 glob Options whose following argument should not be counted
2195
- # @param $3 glob Options that should be counted as args
2192
+ #
2193
+ # Options:
2194
+ # -n CHARS Characters out of $COMP_WORDBREAKS which should
2195
+ # NOT be considered word breaks. See
2196
+ # _comp__reassemble_words.
2197
+ # -a GLOB Options whose following argument should not be counted
2198
+ # -i GLOB Options that should be counted as args
2199
+ #
2196
2200
# @var[out] ret Return the number of arguments
2197
2201
# @since 2.12
2198
2202
_comp_count_args ()
2199
2203
{
2200
- local i cword words
2201
- _comp__reassemble_words " ${1-} <>&" words cword
2204
+ local has_optarg=" " has_exclude=" " exclude=" " glob_include=" "
2205
+ local OPTIND=1 OPTARG=" " OPTERR=0 _opt
2206
+ while getopts ' :a:n:i:' _opt " $@ " ; do
2207
+ case $_opt in
2208
+ a) has_optarg=$OPTARG ;;
2209
+ n) has_exclude=set exclude+=$OPTARG ;;
2210
+ i) glob_include=$OPTARG ;;
2211
+ * )
2212
+ echo " bash_completion: $FUNCNAME : usage error" >&2
2213
+ return 2
2214
+ ;;
2215
+ esac
2216
+ done
2217
+ shift " $(( OPTIND - 1 )) "
2218
+
2219
+ local cword words
2220
+ _comp__reassemble_words " $exclude <>&" words cword
2202
2221
2222
+ local i
2203
2223
ret=1
2204
2224
for (( i = 1 ; i < cword; i++ )) ; do
2205
2225
# shellcheck disable=SC2053
2206
- if [[ ${2-} && ${words[i]} == ${2-} ]]; then
2226
+ if [[ $has_optarg && ${words[i]} == $has_optarg ]]; then
2207
2227
(( i++ ))
2208
- elif [[ ${words[i]} != -?* || ${3-} && ${words[i]} == ${3-} ]]; then
2228
+ elif [[ ${words[i]} != -?* || $glob_include && ${words[i]} == $glob_include ]]; then
2209
2229
(( ret++ ))
2210
2230
elif [[ ${words[i]} == -- ]]; then
2211
2231
(( ret += cword - i - 1 ))
0 commit comments