您实际上可以complete
使用 更改选项compopt
。这对我有用:
_javaProgram(){
# The completion will not add a space by default. We will alter
# this behavior by calling `compopt +o nospace` to neutralize this.
# In completion, '$2' is the current word, and '$3' is the previous
case "$3" in
-D)
# No space by default.
COMPREPLY=( $(compgen -W "property=" -- "$2") )
;;
*)
# The `case` default:
COMPREPLY=( $(compgen -W "your default word list" -- "$2") )
# append a space by calling:
compopt +o nospace
;;
esac
}
complete -F _javaProgram -o nospace javaProgram
但是,我只需要在短选项之后的空间。=
要对比多头和空头选项,您可以检查建议中是否有 a COMPREPLY
。
_javaProgram(){
# The completion will not add a space by default. We will alter
# this behavior by calling `compopt +o nospace` to neutralize this.
# In completion, '$2' is the current word, and '$3' is the previous
case "$3" in
-D)
# Do not worry about appending spaces here ... .
COMPREPLY=( $(compgen -W "property=" -- "$2") )
;;
*)
# Do not worry about appending spaces here ... .
COMPREPLY=( $(compgen -W "your default word list" -- "$2") )
;;
esac
if [[ ! "${COMPREPLY[@]}" =~ "=" ]]; then
# Add space if there is not a '=' in suggestions
compopt +o nospace
fi
}
complete -F _javaProgram -o nospace javaProgram