@@ -2159,46 +2159,101 @@ _comp_realcommand()
21592159 fi
21602160}
21612161
2162- # This function returns the first argument, excluding options
2163- # @var[out] ret First argument before current being completed if any, or
2164- # otherwise an empty string
2162+ # This function returns the position of the first argument, excluding options
2163+ #
2164+ # Options:
2165+ # -a GLOB Pattern of options that take an option argument
2166+ #
2167+ # @var[out] ret Position of the first argument before the current one being
2168+ # completed if any, or otherwise an empty string
21652169# @return True (0) if any argument is found, False (> 0) otherwise.
21662170# @since 2.12
2167- _comp_get_first_arg ()
2171+ _comp_locate_first_arg ()
21682172{
2169- local i
2173+ local has_optarg=" "
2174+ local OPTIND=1 OPTARG=" " OPTERR=0 _opt
2175+ while getopts ' :a:' _opt " $@ " ; do
2176+ case $_opt in
2177+ a) has_optarg=$OPTARG ;;
2178+ * )
2179+ echo " bash_completion: $FUNCNAME : usage error" >&2
2180+ return 2
2181+ ;;
2182+ esac
2183+ done
2184+ shift " $(( OPTIND - 1 )) "
21702185
2186+ local i
21712187 ret=
21722188 for (( i = 1 ; i < cword; i++ )) ; do
2173- if [[ ${words[i]} != -?* ]]; then
2174- ret=${words[i]}
2189+ # shellcheck disable=SC2053
2190+ if [[ $has_optarg && ${words[i]} == $has_optarg ]]; then
2191+ (( i++ ))
2192+ elif [[ ${words[i]} != -?* ]]; then
2193+ ret=$i
21752194 return 0
21762195 elif [[ ${words[i]} == -- ]]; then
2177- (( i + 1 < cword)) && ret=${words[ i + 1]} && return 0
2196+ (( i + 1 < cword)) && ret=$(( i + 1 )) && return 0
21782197 break
21792198 fi
21802199 done
21812200 return 1
21822201}
21832202
2203+ # This function returns the first argument, excluding options
2204+ #
2205+ # Options:
2206+ # -a GLOB Pattern of options that take an option argument
2207+ #
2208+ # @var[out] ret First argument before the current one being completed if any,
2209+ # or otherwise an empty string
2210+ # @return True (0) if any argument is found, False (> 0) otherwise.
2211+ # @since 2.12
2212+ _comp_get_first_arg ()
2213+ {
2214+ _comp_locate_first_arg " $@ " && ret=${words[ret]}
2215+ }
2216+
21842217# This function counts the number of args, excluding options
2185- # @param $1 chars Characters out of $COMP_WORDBREAKS which should
2186- # NOT be considered word breaks. See _comp__reassemble_words.
2187- # @param $2 glob Options whose following argument should not be counted
2188- # @param $3 glob Options that should be counted as args
2218+ #
2219+ # Options:
2220+ # -n CHARS Characters out of $COMP_WORDBREAKS which should
2221+ # NOT be considered word breaks. See
2222+ # _comp__reassemble_words.
2223+ # -a GLOB Options whose following argument should not be counted
2224+ # -i GLOB Options that should be counted as args
2225+ #
21892226# @var[out] ret Return the number of arguments
21902227# @since 2.12
21912228_comp_count_args ()
21922229{
2193- local i cword words
2194- _comp__reassemble_words " ${1-} " words cword
2230+ local has_optarg=" " has_exclude=" " exclude=" " glob_include=" "
2231+ local OPTIND=1 OPTARG=" " OPTERR=0 _opt
2232+ while getopts ' :a:n:i:' _opt " $@ " ; do
2233+ case $_opt in
2234+ a) has_optarg=$OPTARG ;;
2235+ n) has_exclude=set exclude+=$OPTARG ;;
2236+ i) glob_include=$OPTARG ;;
2237+ * )
2238+ echo " bash_completion: $FUNCNAME : usage error" >&2
2239+ return 2
2240+ ;;
2241+ esac
2242+ done
2243+ shift " $(( OPTIND - 1 )) "
21952244
2245+ if [[ $has_exclude ]]; then
2246+ local cword words
2247+ _comp__reassemble_words " $exclude <>&" words cword
2248+ fi
2249+
2250+ local i
21962251 ret=1
21972252 for (( i = 1 ; i < cword; i++ )) ; do
21982253 # shellcheck disable=SC2053
2199- if [[ ${2-} && ${words[i]} == ${2-} ]]; then
2254+ if [[ $has_optarg && ${words[i]} == $has_optarg ]]; then
22002255 (( i++ ))
2201- elif [[ ${words[i]} != -?* || ${3-} && ${words[i]} == ${3-} ]]; then
2256+ elif [[ ${words[i]} != -?* || $glob_include && ${words[i]} == $glob_include ]]; then
22022257 (( ret++ ))
22032258 elif [[ ${words[i]} == -- ]]; then
22042259 (( ret += cword - i - 1 ))
0 commit comments