From 8cdd226cf35e4e0b4afb7a9dd80a7bedd4237a34 Mon Sep 17 00:00:00 2001 From: Emily Grace Seville Date: Tue, 23 Nov 2021 17:13:52 +1000 Subject: [PATCH 1/5] getopt completion --- completion/available/getopt.completion.bash | 32 +++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 completion/available/getopt.completion.bash diff --git a/completion/available/getopt.completion.bash b/completion/available/getopt.completion.bash new file mode 100644 index 00000000..45a10206 --- /dev/null +++ b/completion/available/getopt.completion.bash @@ -0,0 +1,32 @@ +__getopt() { + local OPTIONS=('-a' '--alternative' + '-h' '--help' + '-l' '--longoptions' + '-n' '--name' + '-o' '--options' + '-q' '--quiet' + '-Q' '--quiet-output' + '-s' '--shell' + '-T' '--test' + '-u' '--unquoted' + '-V' '--version') + + local SHELL_ARGS=('sh' 'bash' 'csh' 'tcsh') + + local current=$2 + local previous=$3 + + case $previous in + -s|--shell) + readarray -t COMPREPLY < <(compgen -W "${SHELL_ARGS[*]}" -- "$current") + ;; + -n|--name) + readarray -t COMPREPLY < <(compgen -A function -- "$current") + ;; + *) + readarray -t COMPREPLY < <(compgen -W "${OPTIONS[*]}" -- "$current") + ;; + esac +} + +complete -F __getopt getopt From 9d4b23b9927d5b1b7bf65e188b8e020a1dff47fd Mon Sep 17 00:00:00 2001 From: John D Pell Date: Sun, 16 Jan 2022 11:28:59 -0800 Subject: [PATCH 2/5] completion/getopt: `shfmt` --- completion/available/getopt.completion.bash | 56 +++++++++++---------- 1 file changed, 29 insertions(+), 27 deletions(-) diff --git a/completion/available/getopt.completion.bash b/completion/available/getopt.completion.bash index 45a10206..adc48a8d 100644 --- a/completion/available/getopt.completion.bash +++ b/completion/available/getopt.completion.bash @@ -1,32 +1,34 @@ -__getopt() { - local OPTIONS=('-a' '--alternative' - '-h' '--help' - '-l' '--longoptions' - '-n' '--name' - '-o' '--options' - '-q' '--quiet' - '-Q' '--quiet-output' - '-s' '--shell' - '-T' '--test' - '-u' '--unquoted' - '-V' '--version') +# shellcheck shell=bash - local SHELL_ARGS=('sh' 'bash' 'csh' 'tcsh') +function _getopt() { + local OPTIONS=('-a' '--alternative' + '-h' '--help' + '-l' '--longoptions' + '-n' '--name' + '-o' '--options' + '-q' '--quiet' + '-Q' '--quiet-output' + '-s' '--shell' + '-T' '--test' + '-u' '--unquoted' + '-V' '--version') - local current=$2 - local previous=$3 + local SHELL_ARGS=('sh' 'bash' 'csh' 'tcsh') - case $previous in - -s|--shell) - readarray -t COMPREPLY < <(compgen -W "${SHELL_ARGS[*]}" -- "$current") - ;; - -n|--name) - readarray -t COMPREPLY < <(compgen -A function -- "$current") - ;; - *) - readarray -t COMPREPLY < <(compgen -W "${OPTIONS[*]}" -- "$current") - ;; - esac + local current=$2 + local previous=$3 + + case $previous in + -s | --shell) + readarray -t COMPREPLY < <(compgen -W "${SHELL_ARGS[*]}" -- "$current") + ;; + -n | --name) + readarray -t COMPREPLY < <(compgen -A function -- "$current") + ;; + *) + readarray -t COMPREPLY < <(compgen -W "${OPTIONS[*]}" -- "$current") + ;; + esac } -complete -F __getopt getopt +complete -F _getopt getopt From cd366fc8a5c413b488398e734fdb5944b35ec9dd Mon Sep 17 00:00:00 2001 From: John D Pell Date: Sun, 16 Jan 2022 11:38:43 -0800 Subject: [PATCH 3/5] completion/getopt: compat with v3.2 --- completion/available/getopt.completion.bash | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/completion/available/getopt.completion.bash b/completion/available/getopt.completion.bash index adc48a8d..9422ca86 100644 --- a/completion/available/getopt.completion.bash +++ b/completion/available/getopt.completion.bash @@ -1,6 +1,10 @@ # shellcheck shell=bash function _getopt() { + local IFS=$'\n' + local cur=${COMP_WORDS[COMP_CWORD]} + local prev=${COMP_WORDS[COMP_CWORD-1]:-} + local OPTIONS=('-a' '--alternative' '-h' '--help' '-l' '--longoptions' @@ -15,18 +19,15 @@ function _getopt() { local SHELL_ARGS=('sh' 'bash' 'csh' 'tcsh') - local current=$2 - local previous=$3 - - case $previous in + case $prev in -s | --shell) - readarray -t COMPREPLY < <(compgen -W "${SHELL_ARGS[*]}" -- "$current") + read -d '' -ra COMPREPLY < <(compgen -W "${SHELL_ARGS[*]}" -- "$cur") ;; -n | --name) - readarray -t COMPREPLY < <(compgen -A function -- "$current") + read -d '' -ra COMPREPLY < <(compgen -A function -- "$cur") ;; *) - readarray -t COMPREPLY < <(compgen -W "${OPTIONS[*]}" -- "$current") + read -d '' -ra COMPREPLY < <(compgen -W "${OPTIONS[*]}" -- "$cur") ;; esac } From 67869422b91185a0d1aca6b2aa6907f58a7ddcb6 Mon Sep 17 00:00:00 2001 From: John D Pell Date: Sun, 16 Jan 2022 11:38:56 -0800 Subject: [PATCH 4/5] completion/getopt: use `-X '!&*'` --- completion/available/getopt.completion.bash | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/completion/available/getopt.completion.bash b/completion/available/getopt.completion.bash index 9422ca86..5f3554ad 100644 --- a/completion/available/getopt.completion.bash +++ b/completion/available/getopt.completion.bash @@ -21,15 +21,15 @@ function _getopt() { case $prev in -s | --shell) - read -d '' -ra COMPREPLY < <(compgen -W "${SHELL_ARGS[*]}" -- "$cur") + COMPREPLY=("${SHELL_ARGS[@]}") ;; -n | --name) read -d '' -ra COMPREPLY < <(compgen -A function -- "$cur") ;; *) - read -d '' -ra COMPREPLY < <(compgen -W "${OPTIONS[*]}" -- "$cur") + COMPREPLY=("${OPTIONS[@]}") ;; esac } -complete -F _getopt getopt +complete -F _getopt -X '!&*' getopt From 271a669724f8541968367ae1692155682b12dc94 Mon Sep 17 00:00:00 2001 From: John D Pell Date: Sun, 16 Jan 2022 11:30:36 -0800 Subject: [PATCH 5/5] completion/getopt: add to `clean_files.txt` --- clean_files.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/clean_files.txt b/clean_files.txt index 0cc1ecb3..51028fe4 100644 --- a/clean_files.txt +++ b/clean_files.txt @@ -49,6 +49,7 @@ completion/available/docker-machine.completion.bash completion/available/docker.completion.bash completion/available/gcloud.completion.bash completion/available/gem.completion.bash +completion/available/getopt.completion.bash completion/available/git.completion.bash completion/available/github-cli.completion.bash completion/available/go.completion.bash