From 447ff244e0b827fbb803051a3aa6ee1b62b38960 Mon Sep 17 00:00:00 2001 From: Edwin Kofler Date: Sun, 24 Jan 2021 22:03:59 -0800 Subject: [PATCH] feat: add shell completion --- completions/dotty.sh | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 completions/dotty.sh diff --git a/completions/dotty.sh b/completions/dotty.sh new file mode 100644 index 0000000..e292740 --- /dev/null +++ b/completions/dotty.sh @@ -0,0 +1,41 @@ +# shellcheck shell=bash + +# based on https://tylerthrailkill.com/2019-01-19/writing-bash-completion-script-with-subcommands/ +_dotty() { + local i=1 cmd + + # iterate over COMP_WORDS (ending at currently completed word) + # this ensures we bm_get command completion even after passing flags + while [[ "$i" -lt "$COMP_CWORD" ]]; do + local s="${COMP_WORDS[i]}" + case "$s" in + # if our current word starts with a '-', it is not a subcommand + -*) ;; + # we are completing a subcommand, set cmd + *) + cmd="$s" + break + ;; + esac + (( i++ )) + done + + # check if we're completing 'dotty' + if [[ "$i" -eq "$COMP_CWORD" ]]; then + local cur="${COMP_WORDS[COMP_CWORD]}" + # shellcheck disable=SC2207 + COMPREPLY=($(compgen -W "status reconcile --version --help" -- "$cur")) + return + fi + + # if we're not completing 'dotty', then we're completing a subcommand + case "$cmd" in + status) + COMPREPLY=() ;; + reconcile) + COMPREPLY=() ;; + *) + ;; + esac + +} && complete -F _dotty dotty