From daf010ad945d84f48ef672ab4905d56e34ad1886 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ville=20Skytt=C3=A4?= Date: Sat, 19 May 2018 18:44:16 +0200 Subject: [PATCH] _count_args: Add support for not counting specified option args --- bash_completion | 7 +++++-- test/unit/_count_args.exp | 5 +++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/bash_completion b/bash_completion index 38848507e3..0c2ef0529b 100644 --- a/bash_completion +++ b/bash_completion @@ -1354,14 +1354,17 @@ _get_first_arg() # This function counts the number of args, excluding options # @param $1 chars Characters out of $COMP_WORDBREAKS which should # NOT be considered word breaks. See __reassemble_comp_words_by_ref. +# @param $2 glob Options whose following argument should not be counted _count_args() { local i cword words __reassemble_comp_words_by_ref "$1" words cword args=1 - for i in "${words[@]:1:cword-1}"; do - [[ "$i" != -* ]] && args=$(($args+1)) + for (( i=1; i < cword; i++ )); do + if [[ ${words[i]} != -* && ${words[i-1]} != $2 ]]; then + args=$(($args+1)) + fi done } diff --git a/test/unit/_count_args.exp b/test/unit/_count_args.exp index b528846ac0..f3e34bf687 100644 --- a/test/unit/_count_args.exp +++ b/test/unit/_count_args.exp @@ -47,5 +47,10 @@ set cmd {COMP_WORDS=(a b -c d); COMP_CWORD=2; COMP_LINE='a b -c d'; COMP_POINT=6 assert_bash_list 2 $cmd $test sync_after_int +set test "a b -c| d e should set args to 2"; # | = cursor position +set cmd {COMP_WORDS=(a b -c d e); COMP_CWORD=4; COMP_LINE='a b -c d e'; COMP_POINT=6; _count_args "" "@(-c|--foo)"; echo $args} +assert_bash_list 2 $cmd $test +sync_after_int + teardown