mirror of
https://github.com/clap-rs/clap
synced 2024-12-15 15:22:30 +00:00
ea264fde16
Early in the Bash-completion script, we build up a string that identifies the command or subcommand. When we see the top-level command's name (e.g. `git`) we set the command so far to that value. We do that regardless of where in the argument list it appears. For example, if the argument list is `git diff git`, we set the current command to `git` when run into it the second time. We therefore suggest arguments to the top-level command afterwards, which is not correct. This patch fixes that by also considering the string that identifies the command so far, so we only set the overall command to `git` if the command so far is the empty string. This is actually just a step on the way to getting completion to work for aliases of subcommands. Closes #4273
54 lines
1.4 KiB
Bash
54 lines
1.4 KiB
Bash
_my-app() {
|
|
local i cur prev opts cmds
|
|
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="-F -f -O -o -h -V --flg --flag --opt --option --help --version [positional]"
|
|
if [[ ${cur} == -* || ${COMP_CWORD} -eq 1 ]] ; then
|
|
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
|
|
return 0
|
|
fi
|
|
case "${prev}" in
|
|
--option)
|
|
COMPREPLY=($(compgen -f "${cur}"))
|
|
return 0
|
|
;;
|
|
--opt)
|
|
COMPREPLY=($(compgen -f "${cur}"))
|
|
return 0
|
|
;;
|
|
-o)
|
|
COMPREPLY=($(compgen -f "${cur}"))
|
|
return 0
|
|
;;
|
|
-O)
|
|
COMPREPLY=($(compgen -f "${cur}"))
|
|
return 0
|
|
;;
|
|
*)
|
|
COMPREPLY=()
|
|
;;
|
|
esac
|
|
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
|
|
return 0
|
|
;;
|
|
esac
|
|
}
|
|
|
|
complete -F _my-app -o bashdefault -o default my-app
|