@@ -1054,9 +1054,12 @@ _comp_quote_compgen()
10541054
10551055# This function performs file and directory completion. It's better than
10561056# simply using 'compgen -f', because it honours spaces in filenames.
1057- # @param $1 If `-d', complete only on directories. Otherwise filter/pick only
1058- # completions with `.$1' and the uppercase version of it as file
1059- # extension.
1057+ # @param $1 Complete filenames matching `.$1' and the uppercase version of it.
1058+ # Ignored with `-d`.
1059+ # OPTIONS
1060+ # -d Complete only on directories
1061+ # -f Perform `compopt -f filenames` modifications manually. This suffix
1062+ # a slash to a directory name.
10601063# @return 0 if at least one completion is generated, or 1 otherwise.
10611064#
10621065# @since 2.12
@@ -1065,9 +1068,22 @@ _comp_compgen_filedir()
10651068 _comp_compgen_tilde && return
10661069
10671070 local -a toks
1071+ local _dir=" " _filenames=" "
1072+ local OPTIND=1 OPTARG=" " OPTERR=0 _opt
1073+ while getopts " :df" _opt " $@ " ; do
1074+ case $_opt in
1075+ d) _dir=set ;;
1076+ f) _filenames=set ;;
1077+ * )
1078+ printf " bash_completion: %s: usage error\n" " $FUNCNAME " >&2
1079+ return 2
1080+ ;;
1081+ esac
1082+ done
1083+ shift " $(( OPTIND - 1 )) "
10681084 local _arg=${1-}
10691085
1070- if [[ $_arg == -d ]]; then
1086+ if [[ $_dir ]]; then
10711087 _comp_compgen -v toks -- -d
10721088 else
10731089 local ret
@@ -1103,9 +1119,19 @@ _comp_compgen_filedir()
11031119 fi
11041120
11051121 if (( ${# toks[@]} != 0 )) ; then
1106- # 2>/dev/null for direct invocation, e.g. in the _comp_compgen_filedir
1107- # unit test
1108- compopt -o filenames 2> /dev/null
1122+ # compopt 2>/dev/null for direct invocation, e.g. in
1123+ # _comp_compgen_filedir unit test
1124+ if [[ $_filenames ]]; then
1125+ local i
1126+ for i in " ${! toks[@]} " ; do
1127+ if [[ -d ${toks[i]} ]]; then
1128+ toks[i]+=/
1129+ compopt -o nospace 2> /dev/null
1130+ fi
1131+ done
1132+ else
1133+ compopt -o filenames 2> /dev/null
1134+ fi
11091135 fi
11101136
11111137 # Note: bash < 4.4 has a bug that all the elements are connected with
0 commit comments