With the plethora of new command-line options, it is starting to get
difficult to remember them all. This commit introduces shell completions
for bash and zsh for the convenience of the user.

The completion files have been auto-generated using clap_generate [1]
via its YAML support [2]. The YAML files are also part of this commit to
support generating future completion files.

[1] https://docs.rs/clap_generate
[2] 
https://github.com/clap-rs/clap/tree/00a0c4ee148ae4106e3c405043b7f0bd52524ca8#using-yaml

Signed-off-by: Michael Adler <[email protected]>
---
 Makefile.am                            |   6 ++
 share/completion/bash/bg_printenv.bash |  62 ++++++++++++++
 share/completion/bash/bg_setenv.bash   | 110 +++++++++++++++++++++++++
 share/completion/bg_printenv.yaml      |  54 ++++++++++++
 share/completion/bg_setenv.yaml        |  82 ++++++++++++++++++
 share/completion/zsh/_bg_printenv      |  44 ++++++++++
 share/completion/zsh/_bg_setenv        |  57 +++++++++++++
 7 files changed, 415 insertions(+)
 create mode 100644 share/completion/bash/bg_printenv.bash
 create mode 100644 share/completion/bash/bg_setenv.bash
 create mode 100644 share/completion/bg_printenv.yaml
 create mode 100644 share/completion/bg_setenv.yaml
 create mode 100644 share/completion/zsh/_bg_printenv
 create mode 100644 share/completion/zsh/_bg_setenv

diff --git a/Makefile.am b/Makefile.am
index 8081839..b32d19f 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -218,6 +218,12 @@ efibootguard_DATA = $(efi_loadername)
 CLEANFILES += $(efi_objects) $(efi_solib) $(efi_loadername)
 EXTRA_DIST += $(efi_sources)
 
+bashcompletiondir = ${datarootdir}/efibootguard/completion/bash
+bashcompletion_DATA = share/completion/bash/bg_setenv.bash 
share/completion/bash/bg_printenv.bash
+
+zshcompletiondir = ${datarootdir}/efibootguard/completion/zsh
+zshcompletion_DATA = share/completion/zsh/_bg_setenv 
share/completion/zsh/_bg_printenv
+
 $(top_builddir)/%.o: $(top_srcdir)/%.c
        @$(MKDIR_P) $(shell dirname $@)/
        $(AM_V_CC)$(GNUEFI_CC) $(efi_cppflags) $(efi_cflags) -c $< -o $@
diff --git a/share/completion/bash/bg_printenv.bash 
b/share/completion/bash/bg_printenv.bash
new file mode 100644
index 0000000..a2e8e06
--- /dev/null
+++ b/share/completion/bash/bg_printenv.bash
@@ -0,0 +1,62 @@
+_bg_printenv() {
+    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 "${i}" in
+            bg_printenv)
+                cmd="bg_printenv"
+                ;;
+            *)
+                ;;
+        esac
+    done
+
+    case "${cmd}" in
+        bg_printenv)
+            opts="-? -c -f -o -p -r -V -v --help --current --filepath --output 
--part --raw --usage --version --verbose"
+            if [[ ${cur} == -* || ${COMP_CWORD} -eq 1 ]] ; then
+                COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
+                return 0
+            fi
+            case "${prev}" in
+                --filepath)
+                    COMPREPLY=($(compgen -f "${cur}"))
+                    return 0
+                    ;;
+                -f)
+                    COMPREPLY=($(compgen -f "${cur}"))
+                    return 0
+                    ;;
+                --output)
+                    COMPREPLY=($(compgen -W "in_progress revision kernel 
kernelargs watchdog_timeout ustate user" -- "${cur}"))
+                    return 0
+                    ;;
+                -o)
+                    COMPREPLY=($(compgen -W "in_progress revision kernel 
kernelargs watchdog_timeout ustate user" -- "${cur}"))
+                    return 0
+                    ;;
+                --part)
+                    COMPREPLY=($(compgen -f "${cur}"))
+                    return 0
+                    ;;
+                -p)
+                    COMPREPLY=($(compgen -f "${cur}"))
+                    return 0
+                    ;;
+                *)
+                    COMPREPLY=()
+                    ;;
+            esac
+            COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
+            return 0
+            ;;
+    esac
+}
+
+complete -F _bg_printenv -o bashdefault -o default bg_printenv
diff --git a/share/completion/bash/bg_setenv.bash 
b/share/completion/bash/bg_setenv.bash
new file mode 100644
index 0000000..63e221a
--- /dev/null
+++ b/share/completion/bash/bg_setenv.bash
@@ -0,0 +1,110 @@
+_bg_setenv() {
+    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 "${i}" in
+            bg_setenv)
+                cmd="bg_setenv"
+                ;;
+            *)
+                ;;
+        esac
+    done
+
+    case "${cmd}" in
+        bg_setenv)
+            opts="-? -f -p -v -V -P -k -a -r -s -w -c -u -x -i --help 
--filepath --part --verbose --version --preserve --kernel --args --revision 
--ustate --watchdog --confirm --update --uservar --in_progress"
+            if [[ ${cur} == -* || ${COMP_CWORD} -eq 1 ]] ; then
+                COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
+                return 0
+            fi
+            case "${prev}" in
+                --filepath)
+                    COMPREPLY=($(compgen -f "${cur}"))
+                    return 0
+                    ;;
+                -f)
+                    COMPREPLY=($(compgen -f "${cur}"))
+                    return 0
+                    ;;
+                --part)
+                    COMPREPLY=($(compgen -f "${cur}"))
+                    return 0
+                    ;;
+                -p)
+                    COMPREPLY=($(compgen -f "${cur}"))
+                    return 0
+                    ;;
+                --kernel)
+                    COMPREPLY=($(compgen -f "${cur}"))
+                    return 0
+                    ;;
+                -k)
+                    COMPREPLY=($(compgen -f "${cur}"))
+                    return 0
+                    ;;
+                --args)
+                    COMPREPLY=($(compgen -f "${cur}"))
+                    return 0
+                    ;;
+                -a)
+                    COMPREPLY=($(compgen -f "${cur}"))
+                    return 0
+                    ;;
+                --revision)
+                    COMPREPLY=($(compgen -f "${cur}"))
+                    return 0
+                    ;;
+                -r)
+                    COMPREPLY=($(compgen -f "${cur}"))
+                    return 0
+                    ;;
+                --ustate)
+                    COMPREPLY=($(compgen -f "${cur}"))
+                    return 0
+                    ;;
+                -s)
+                    COMPREPLY=($(compgen -f "${cur}"))
+                    return 0
+                    ;;
+                --watchdog)
+                    COMPREPLY=($(compgen -f "${cur}"))
+                    return 0
+                    ;;
+                -w)
+                    COMPREPLY=($(compgen -f "${cur}"))
+                    return 0
+                    ;;
+                --uservar)
+                    COMPREPLY=($(compgen -f "${cur}"))
+                    return 0
+                    ;;
+                -x)
+                    COMPREPLY=($(compgen -f "${cur}"))
+                    return 0
+                    ;;
+                --in_progress)
+                    COMPREPLY=($(compgen -f "${cur}"))
+                    return 0
+                    ;;
+                -i)
+                    COMPREPLY=($(compgen -f "${cur}"))
+                    return 0
+                    ;;
+                *)
+                    COMPREPLY=()
+                    ;;
+            esac
+            COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
+            return 0
+            ;;
+    esac
+}
+
+complete -F _bg_setenv -o bashdefault -o default bg_setenv
diff --git a/share/completion/bg_printenv.yaml 
b/share/completion/bg_printenv.yaml
new file mode 100644
index 0000000..813f321
--- /dev/null
+++ b/share/completion/bg_printenv.yaml
@@ -0,0 +1,54 @@
+# input for https://crates.io/crates/clap_generate
+name: bg_printenv
+about: Environment tool for the EFI Boot Guard
+args:
+    - help:
+        short: "?"
+        long: help
+        about: Show help
+    - current:
+        short: c
+        long: current
+        about: Only print values from the current environment
+    - filepath:
+        short: f
+        long: filepath
+        takes_value: true
+        value_name: ENVFILE
+        about: Environment to use
+    - output:
+        short: o
+        long: output
+        takes_value: true
+        multiple_values: true
+        use_delimiter: true
+        possible_values:
+            - in_progress
+            - revision
+            - kernel
+            - kernelargs
+            - watchdog_timeout
+            - ustate
+            - user
+        about: Comma-separated list of fields which are printed
+    - part:
+        short: p
+        long: part
+        takes_value: true
+        value_name: ENV_PART
+        about: Set environment partition to update
+    - raw:
+        short: r
+        long: raw
+        about: Raw output mode
+    - usage:
+        long: usage
+        about: Give a short usage message
+    - version:
+        short: V
+        long: version
+        about: Print version
+    - verbose:
+        short: "v"
+        long: verbose
+        about: Be verbose
diff --git a/share/completion/bg_setenv.yaml b/share/completion/bg_setenv.yaml
new file mode 100644
index 0000000..b3a8ff1
--- /dev/null
+++ b/share/completion/bg_setenv.yaml
@@ -0,0 +1,82 @@
+# input for https://crates.io/crates/clap_generate
+name: bg_setenv
+about: Environment tool for the EFI Boot Guard
+args:
+    - help:
+        short: "?"
+        long: help
+        about: Show help
+    - filepath:
+        short: "f"
+        long: "filepath"
+        about: "Environment to use. Expects a file name, usually called 
BGENV.DAT."
+        takes_value: true
+        value_name: "ENVFILE"
+    - part:
+        short: "p"
+        long: "part"
+        about: "Set environment partition to update. If no partition is 
specified, the one with the smallest revision value above zero is updated."
+        takes_value: true
+        value_name: "ENV_PART"
+    - verbose:
+        short: "v"
+        long: "verbose"
+        about: "Be verbose"
+    - version:
+        short: "V"
+        long: "version"
+        about: "Print version"
+    - preserve:
+        short: "P"
+        long: "preserve"
+        about: "Preserve existing entries"
+    - kernel:
+        short: "k"
+        long: "kernel"
+        about: "Set kernel to load"
+        takes_value: true
+        value_name: "KERNEL"
+    - args:
+        short: "a"
+        long: "args"
+        about: "Set kernel arguments"
+        takes_value: true
+        value_name: "KERNEL_ARGS"
+    - revision:
+        short: "r"
+        long: "revision"
+        about: "Set revision value"
+        takes_value: true
+        value_name: "REVISION"
+    - ustate:
+        short: "s"
+        long: "ustate"
+        about: "Set update status for environment"
+        takes_value: true
+        value_name: "USTATE"
+    - watchdog:
+        short: "w"
+        long: "watchdog"
+        about: "Watchdog timeout in seconds"
+        takes_value: true
+        value_name: "WATCHDOG_TIMEOUT"
+    - confirm:
+        short: "c"
+        long: "confirm"
+        about: "Confirm working environment"
+    - update:
+        short: "u"
+        long: "update"
+        about: "Automatically update oldest revision"
+    - uservar:
+        short: "x"
+        long: "uservar"
+        about: "Set user-defined string variable. For setting multiple 
variables, use this option multiple times."
+        takes_value: true
+        value_name: "KEY=VAL"
+    - in_progress:
+        short: "i"
+        long: "in_progress"
+        about: "Set in_progress variable to simulate a running update process."
+        takes_value: true
+        value_name: "IN_PROGRESS"
diff --git a/share/completion/zsh/_bg_printenv 
b/share/completion/zsh/_bg_printenv
new file mode 100644
index 0000000..76fcca2
--- /dev/null
+++ b/share/completion/zsh/_bg_printenv
@@ -0,0 +1,44 @@
+#compdef bg_printenv
+
+autoload -U is-at-least
+
+_bg_printenv() {
+    typeset -A opt_args
+    typeset -a _arguments_options
+    local ret=1
+
+    if is-at-least 5.2; then
+        _arguments_options=(-s -S -C)
+    else
+        _arguments_options=(-s -C)
+    fi
+
+    local context curcontext="$curcontext" state line
+    _arguments "${_arguments_options[@]}" \
+'-f+[Environment to use]:ENVFILE: ' \
+'--filepath=[Environment to use]:ENVFILE: ' \
+'-o+[Comma-separated list of fields which are printed]: :(in_progress revision 
kernel kernelargs watchdog_timeout ustate user)' \
+'--output=[Comma-separated list of fields which are printed]: :(in_progress 
revision kernel kernelargs watchdog_timeout ustate user)' \
+'-p+[Set environment partition to update]:ENV_PART: ' \
+'--part=[Set environment partition to update]:ENV_PART: ' \
+'-?[Show help]' \
+'--help[Show help]' \
+'-c[Only print values from the current environment]' \
+'--current[Only print values from the current environment]' \
+'-r[Raw output mode]' \
+'--raw[Raw output mode]' \
+'--usage[Give a short usage message]' \
+'-V[Print version]' \
+'--version[Print version]' \
+'-v[Be verbose]' \
+'--verbose[Be verbose]' \
+&& ret=0
+}
+
+(( $+functions[_bg_printenv_commands] )) ||
+_bg_printenv_commands() {
+    local commands; commands=()
+    _describe -t commands 'bg_printenv commands' commands "$@"
+}
+
+_bg_printenv "$@"
\ No newline at end of file
diff --git a/share/completion/zsh/_bg_setenv b/share/completion/zsh/_bg_setenv
new file mode 100644
index 0000000..a3bc236
--- /dev/null
+++ b/share/completion/zsh/_bg_setenv
@@ -0,0 +1,57 @@
+#compdef bg_setenv
+
+autoload -U is-at-least
+
+_bg_setenv() {
+    typeset -A opt_args
+    typeset -a _arguments_options
+    local ret=1
+
+    if is-at-least 5.2; then
+        _arguments_options=(-s -S -C)
+    else
+        _arguments_options=(-s -C)
+    fi
+
+    local context curcontext="$curcontext" state line
+    _arguments "${_arguments_options[@]}" \
+'-f+[Environment to use. Expects a file name, usually called 
BGENV.DAT.]:ENVFILE: ' \
+'--filepath=[Environment to use. Expects a file name, usually called 
BGENV.DAT.]:ENVFILE: ' \
+'-p+[Set environment partition to update. If no partition is specified, the 
one with the smallest revision value above zero is updated.]:ENV_PART: ' \
+'--part=[Set environment partition to update. If no partition is specified, 
the one with the smallest revision value above zero is updated.]:ENV_PART: ' \
+'-k+[Set kernel to load]:KERNEL: ' \
+'--kernel=[Set kernel to load]:KERNEL: ' \
+'-a+[Set kernel arguments]:KERNEL_ARGS: ' \
+'--args=[Set kernel arguments]:KERNEL_ARGS: ' \
+'-r+[Set revision value]:REVISION: ' \
+'--revision=[Set revision value]:REVISION: ' \
+'-s+[Set update status for environment]:USTATE: ' \
+'--ustate=[Set update status for environment]:USTATE: ' \
+'-w+[Watchdog timeout in seconds]:WATCHDOG_TIMEOUT: ' \
+'--watchdog=[Watchdog timeout in seconds]:WATCHDOG_TIMEOUT: ' \
+'-x+[Set user-defined string variable. For setting multiple variables, use 
this option multiple times.]:KEY=VAL: ' \
+'--uservar=[Set user-defined string variable. For setting multiple variables, 
use this option multiple times.]:KEY=VAL: ' \
+'-i+[Set in_progress variable to simulate a running update 
process.]:IN_PROGRESS: ' \
+'--in_progress=[Set in_progress variable to simulate a running update 
process.]:IN_PROGRESS: ' \
+'-?[Show help]' \
+'--help[Show help]' \
+'-v[Be verbose]' \
+'--verbose[Be verbose]' \
+'-V[Print version]' \
+'--version[Print version]' \
+'-P[Preserve existing entries]' \
+'--preserve[Preserve existing entries]' \
+'-c[Confirm working environment]' \
+'--confirm[Confirm working environment]' \
+'-u[Automatically update oldest revision]' \
+'--update[Automatically update oldest revision]' \
+&& ret=0
+}
+
+(( $+functions[_bg_setenv_commands] )) ||
+_bg_setenv_commands() {
+    local commands; commands=()
+    _describe -t commands 'bg_setenv commands' commands "$@"
+}
+
+_bg_setenv "$@"
\ No newline at end of file
-- 
2.33.1

-- 
You received this message because you are subscribed to the Google Groups "EFI 
Boot Guard" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/efibootguard-dev/20211110132420.356036-1-michael.adler%40siemens.com.

Reply via email to