mirror of
https://github.com/clap-rs/clap
synced 2024-12-14 06:42:33 +00:00
cd82f8cc8e
Because -v is not supported below bash 5.0.
153 lines
4.9 KiB
Bash
153 lines
4.9 KiB
Bash
_my-app() {
|
|
local i cur prev opts cmd
|
|
COMPREPLY=()
|
|
cur="${COMP_WORDS[COMP_CWORD]}"
|
|
prev="${COMP_WORDS[COMP_CWORD-1]}"
|
|
cmd=""
|
|
opts=""
|
|
|
|
for i in ${COMP_WORDS[@]}
|
|
do
|
|
case "${cmd},${i}" in
|
|
",$1")
|
|
cmd="my__app"
|
|
;;
|
|
*)
|
|
;;
|
|
esac
|
|
done
|
|
|
|
case "${cmd}" in
|
|
my__app)
|
|
opts="-p -f -d -e -c -u -H -h --choice --unknown --other --path --file --dir --exe --cmd-name --cmd --user --host --url --email --help [command_with_args]..."
|
|
if [[ ${cur} == -* || ${COMP_CWORD} -eq 1 ]] ; then
|
|
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
|
|
return 0
|
|
fi
|
|
case "${prev}" in
|
|
--choice)
|
|
COMPREPLY=($(compgen -W "bash fish zsh" -- "${cur}"))
|
|
return 0
|
|
;;
|
|
--unknown)
|
|
COMPREPLY=($(compgen -f "${cur}"))
|
|
return 0
|
|
;;
|
|
--other)
|
|
COMPREPLY=("${cur}")
|
|
if [[ "${BASH_VERSINFO[0]}" -ge 4 ]]; then
|
|
compopt -o nospace
|
|
fi
|
|
return 0
|
|
;;
|
|
--path)
|
|
COMPREPLY=($(compgen -f "${cur}"))
|
|
return 0
|
|
;;
|
|
-p)
|
|
COMPREPLY=($(compgen -f "${cur}"))
|
|
return 0
|
|
;;
|
|
--file)
|
|
local oldifs
|
|
if [ -n "${IFS+x}" ]; then
|
|
oldifs="$IFS"
|
|
fi
|
|
IFS=$'\n'
|
|
COMPREPLY=($(compgen -f "${cur}"))
|
|
if [ -n "${oldifs+x}" ]; then
|
|
IFS="$oldifs"
|
|
fi
|
|
if [[ "${BASH_VERSINFO[0]}" -ge 4 ]]; then
|
|
compopt -o filenames
|
|
fi
|
|
return 0
|
|
;;
|
|
-f)
|
|
local oldifs
|
|
if [ -n "${IFS+x}" ]; then
|
|
oldifs="$IFS"
|
|
fi
|
|
IFS=$'\n'
|
|
COMPREPLY=($(compgen -f "${cur}"))
|
|
if [ -n "${oldifs+x}" ]; then
|
|
IFS="$oldifs"
|
|
fi
|
|
if [[ "${BASH_VERSINFO[0]}" -ge 4 ]]; then
|
|
compopt -o filenames
|
|
fi
|
|
return 0
|
|
;;
|
|
--dir)
|
|
COMPREPLY=()
|
|
if [[ "${BASH_VERSINFO[0]}" -ge 4 ]]; then
|
|
compopt -o plusdirs
|
|
fi
|
|
return 0
|
|
;;
|
|
-d)
|
|
COMPREPLY=()
|
|
if [[ "${BASH_VERSINFO[0]}" -ge 4 ]]; then
|
|
compopt -o plusdirs
|
|
fi
|
|
return 0
|
|
;;
|
|
--exe)
|
|
COMPREPLY=($(compgen -f "${cur}"))
|
|
return 0
|
|
;;
|
|
-e)
|
|
COMPREPLY=($(compgen -f "${cur}"))
|
|
return 0
|
|
;;
|
|
--cmd-name)
|
|
COMPREPLY=($(compgen -f "${cur}"))
|
|
return 0
|
|
;;
|
|
--cmd)
|
|
COMPREPLY=($(compgen -f "${cur}"))
|
|
return 0
|
|
;;
|
|
-c)
|
|
COMPREPLY=($(compgen -f "${cur}"))
|
|
return 0
|
|
;;
|
|
--user)
|
|
COMPREPLY=($(compgen -f "${cur}"))
|
|
return 0
|
|
;;
|
|
-u)
|
|
COMPREPLY=($(compgen -f "${cur}"))
|
|
return 0
|
|
;;
|
|
--host)
|
|
COMPREPLY=($(compgen -f "${cur}"))
|
|
return 0
|
|
;;
|
|
-H)
|
|
COMPREPLY=($(compgen -f "${cur}"))
|
|
return 0
|
|
;;
|
|
--url)
|
|
COMPREPLY=($(compgen -f "${cur}"))
|
|
return 0
|
|
;;
|
|
--email)
|
|
COMPREPLY=($(compgen -f "${cur}"))
|
|
return 0
|
|
;;
|
|
*)
|
|
COMPREPLY=()
|
|
;;
|
|
esac
|
|
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
|
|
return 0
|
|
;;
|
|
esac
|
|
}
|
|
|
|
if [[ "${BASH_VERSINFO[0]}" -eq 4 && "${BASH_VERSINFO[1]}" -ge 4 || "${BASH_VERSINFO[0]}" -gt 4 ]]; then
|
|
complete -F _my-app -o nosort -o bashdefault -o default my-app
|
|
else
|
|
complete -F _my-app -o bashdefault -o default my-app
|
|
fi
|