@@ -449,6 +449,10 @@ _comp_split()
449
449
#
450
450
# _comp_compgen -R -- -W '"${words[@]}"'
451
451
#
452
+ # or use the utility `_comp_compgen_set`:
453
+ #
454
+ # _comp_compgen_set "${words[@]}"
455
+ #
452
456
# Other nested calls of _comp_compgen can also be used. The function is
453
457
# supposed to replace the existing content of the array by default to allow the
454
458
# caller control whether to replace or append by the option `-a`.
@@ -540,6 +544,22 @@ _comp_compgen()
540
544
_comp_split -l ${_append: +-a} " $_var " " $_result "
541
545
}
542
546
547
+ # usage: _comp_compgen_set [words...]
548
+ # Reset COMPREPLY with the specified WORDS. If no arguments are specified, the
549
+ # array is cleared.
550
+ #
551
+ # When an array name is specified by `-v VAR` in a caller _comp_compgen, the
552
+ # array is reset instead of COMPREPLY. When the `-a` flag is specified in a
553
+ # caller _comp_compgen, the words are appended to the existing elements of the
554
+ # array instead of replacing the existing elements. This function ignores
555
+ # ${cur-} or the prefix specified by `-v CUR`.
556
+ _comp_compgen_set ()
557
+ {
558
+ local _append=${_comp_compgen__append-}
559
+ local _var=${_comp_compgen__var-COMPREPLY}
560
+ eval -- " $_var ${_append: ++} =(\"\$ @\" )"
561
+ }
562
+
543
563
# Check if the argument looks like a path.
544
564
# @param $1 thing to check
545
565
# @return True (0) if it does, False (> 0) otherwise
@@ -868,8 +888,12 @@ _comp_compgen_filedir()
868
888
if (( ${# toks[@]} != 0 )) ; then
869
889
# 2>/dev/null for direct invocation, e.g. in the _comp_compgen_filedir unit test
870
890
compopt -o filenames 2> /dev/null
871
- _comp_compgen -R -- -W ' "${toks[@]}"'
872
891
fi
892
+
893
+ # Note: bash < 4.4 has a bug that all the elements are connected with
894
+ # ${v-"${a[@]}"} when IFS does not contain whitespace.
895
+ local IFS=$' \t\n '
896
+ _comp_compgen_set ${toks[@]+" ${toks[@]} " }
873
897
} # _comp_compgen_filedir()
874
898
875
899
# This function splits $cur=--foo=bar into $prev=--foo, $cur=bar, making it
0 commit comments