当参数没有空格时如何编写 bash 补全,但=
例如 myapp 可以采用--arg=argVal1
或--arg=argVal2
_myapp_completion() {
local cur prev opts
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
opts="--arg1 --arg2 --arg3" # List of valid options for your application
case "${prev}" in
--arg1)
# If previous word is --arg1, complete the values
COMPREPLY=($(compgen -W "value1 value2 value3" -- "${cur}"))
if [[ ${#COMPREPLY[@]} -eq 1 ]]; then
# If only one completion option, remove space after --arg1=
COMPREPLY=(${COMPREPLY[@]/#/${prev}=})
fi
return 0
;;
*)
# Default completion logic for other arguments
COMPREPLY=($(compgen -W "${opts}" -- "${cur}"))
return 0
;;
esac
}
complete -F _myapp_completion ./myapp
上面的例子将采用后面myapp
之一,不带空格。value1 value2 value3
--arg1=