@@ -1057,9 +1057,12 @@ _comp_quote_compgen()
10571057
10581058# This function performs file and directory completion. It's better than
10591059# simply using 'compgen -f', because it honours spaces in filenames.
1060- # @param $1 If `-d', complete only on directories. Otherwise filter/pick only
1061- # completions with `.$1' and the uppercase version of it as file
1062- # extension.
1060+ # @param $1 Complete filenames matching `.$1' and the uppercase version of it.
1061+ # Ignored with `-d`.
1062+ # OPTIONS
1063+ # -d Complete only on directories
1064+ # -f Perform `compopt -f filenames` modifications manually. This suffix
1065+ # a slash to a directory name.
10631066# @return 0 if at least one completion is generated, or 1 otherwise.
10641067#
10651068# @since 2.12
@@ -1068,9 +1071,22 @@ _comp_compgen_filedir()
10681071 _comp_compgen_tilde && return
10691072
10701073 local -a toks
1074+ local _dir=" " _filenames=" "
1075+ local OPTIND=1 OPTARG=" " OPTERR=0 _opt
1076+ while getopts " :df" _opt " $@ " ; do
1077+ case $_opt in
1078+ d) _dir=set ;;
1079+ f) _filenames=set ;;
1080+ * )
1081+ printf " bash_completion: %s: usage error\n" " $FUNCNAME " >&2
1082+ return 2
1083+ ;;
1084+ esac
1085+ done
1086+ shift " $(( OPTIND - 1 )) "
10711087 local _arg=${1-}
10721088
1073- if [[ $_arg == -d ]]; then
1089+ if [[ $_dir ]]; then
10741090 _comp_compgen -v toks -- -d
10751091 else
10761092 local REPLY
@@ -1106,9 +1122,19 @@ _comp_compgen_filedir()
11061122 fi
11071123
11081124 if (( ${# toks[@]} != 0 )) ; then
1109- # 2>/dev/null for direct invocation, e.g. in the _comp_compgen_filedir
1110- # unit test
1111- compopt -o filenames 2> /dev/null
1125+ # compopt 2>/dev/null for direct invocation, e.g. in
1126+ # _comp_compgen_filedir unit test
1127+ if [[ $_filenames ]]; then
1128+ local i
1129+ for i in " ${! toks[@]} " ; do
1130+ if [[ -d ${toks[i]} ]]; then
1131+ toks[i]+=/
1132+ compopt -o nospace 2> /dev/null
1133+ fi
1134+ done
1135+ else
1136+ compopt -o filenames 2> /dev/null
1137+ fi
11121138 fi
11131139
11141140 # Note: bash < 4.4 has a bug that all the elements are connected with
0 commit comments