]> git.ipfire.org Git - thirdparty/kmod.git/commitdiff
shell-completion/*/rmmod: add bash/fish/zsh completion
authorEmil Velikov <emil.l.velikov@gmail.com>
Wed, 18 Sep 2024 15:49:08 +0000 (16:49 +0100)
committerLucas De Marchi <lucas.de.marchi@gmail.com>
Sun, 22 Sep 2024 21:11:57 +0000 (16:11 -0500)
v2:
 - use e(x)clusive answers for fish, tweak force string

v3:
 - wire the completions to the autotools build

v4:
 - use SPDX style copyright statements

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/138
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
Makefile.am
meson.build
shell-completion/bash/rmmod [new file with mode: 0644]
shell-completion/fish/rmmod.fish [new file with mode: 0644]
shell-completion/zsh/_rmmod [new file with mode: 0644]

index b5ff612cdf3eb642deb41769e3e7fb9a3ba1f8e5..1c6faca6a75fc5916de6b0f29ab49aa4e8c51d96 100644 (file)
@@ -126,15 +126,18 @@ noarch_pkgconfig_DATA = tools/kmod.pc
 bashcompletiondir=@bashcompletiondir@
 dist_bashcompletion_DATA = \
        shell-completion/bash/kmod \
-       shell-completion/bash/lsmod
+       shell-completion/bash/lsmod \
+       shell-completion/bash/rmmod
 
 fishcompletiondir=@fishcompletiondir@
 dist_fishcompletion_DATA = \
-       shell-completion/fish/lsmod.fish
+       shell-completion/fish/lsmod.fish \
+       shell-completion/fish/rmmod.fish
 
 zshcompletiondir=@zshcompletiondir@
 dist_zshcompletion_DATA = \
-       shell-completion/zsh/_lsmod
+       shell-completion/zsh/_lsmod \
+       shell-completion/zsh/_rmmod
 
 install-exec-hook:
 if BUILD_TOOLS
index f45fb9bc83290fa558316869da9d662369870850..c85d984675cea5759bef8d4f9d1aafe237f08031 100644 (file)
@@ -216,6 +216,7 @@ foreach tuple : _completiondirs
 
   _completions = [
     'lsmod',
+    'rmmod',
   ]
 
   if completiondir != 'no'
diff --git a/shell-completion/bash/rmmod b/shell-completion/bash/rmmod
new file mode 100644 (file)
index 0000000..dc63955
--- /dev/null
@@ -0,0 +1,34 @@
+# rmmod(8) completion                                       -*- shell-script -*-
+# SPDX-License-Identifier: LGPL-2.1-or-later
+#
+# SPDX-FileCopyrightText: 2024 Emil Velikov <emil.l.velikov@gmail.com>
+#
+# Formatted using:
+# shfmt --language-dialect bash --indent 4 --func-next-line
+
+_rmmod()
+{
+    # long/short opt pairs
+    local -A opts=(
+        ['force']='f'
+        ['syslog']='s'
+        ['verbose']='v'
+        ['version']='V'
+        ['help']='h'
+    )
+
+    local cur="${COMP_WORDS[COMP_CWORD]}"
+
+    if [[ $cur == --* ]]; then
+        COMPREPLY=($(compgen -P '--' -W "${!opts[*]}" -- "${cur:2}"))
+    elif [[ $cur == -* ]]; then
+        if (( ${#cur} != 2 )); then
+            COMPREPLY=($(compgen -P '--' -W "${!opts[*]}" -- "${cur:2}"))
+        fi
+        COMPREPLY+=($(compgen -P '-' -W "${opts[*]}" -- "${cur:1}"))
+    else
+        local -a modules=($(lsmod | cut -f1 -d' '))
+        COMPREPLY=($(compgen -W "${modules[*]}" -- "${cur}"))
+    fi
+} &&
+    complete -F _rmmod rmmod
diff --git a/shell-completion/fish/rmmod.fish b/shell-completion/fish/rmmod.fish
new file mode 100644 (file)
index 0000000..94fb6d0
--- /dev/null
@@ -0,0 +1,16 @@
+# rmmod(8) completion                                       -*- shell-script -*-
+# SPDX-License-Identifier: LGPL-2.1-or-later
+#
+# SPDX-FileCopyrightText: 2024 Emil Velikov <emil.l.velikov@gmail.com>
+
+# globally disable file completions
+complete -c rmmod -f
+
+complete -c rmmod -s f -l force   -d "DANGEROUS: forces a module unload and may crash your machine"
+complete -c rmmod -s s -l syslog  -d 'print to syslog, not stderr'
+complete -c rmmod -s v -l verbose -d 'enables more messages'
+complete -c rmmod -s V -l version -d 'show version'
+complete -c rmmod -s h -l help    -d 'show this help'
+
+# provide an exclusive (x) list of required (r) answers (a)
+complete -c rmmod -x -ra "(lsmod | cut -f1 -d' ')"
diff --git a/shell-completion/zsh/_rmmod b/shell-completion/zsh/_rmmod
new file mode 100644 (file)
index 0000000..8582e76
--- /dev/null
@@ -0,0 +1,21 @@
+#compdef rmmod
+
+# rmmod(8) completion                                       -*- shell-script -*-
+# SPDX-License-Identifier: LGPL-2.1-or-later
+#
+# SPDX-FileCopyrightText: 2024 Emil Velikov <emil.l.velikov@gmail.com>
+
+(( $+functions[_rmmod_modules] )) || _rmmod_modules()
+{
+    local -a _modules
+    _modules=(${${(f)"$(_call_program modules lsmod)"}[2,-1]%% *})
+    _values 'modules' "$_modules[@]"
+}
+
+_arguments \
+    {-f,--force}'[DANGEROUS: forces a module unload and may crash your machine]' \
+    {-s,--syslog}'[print to syslog, not stderr]' \
+    {-v,--verbose}'[enables more messages]' \
+    {-V,--version}'[show version]' \
+    {-h,--help}'[show this help]' \
+    '*::modules:_rmmod_modules'