Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

zsh virsh completion



Hi,

Below is a patch to implement basic completion for the virsh(1) [1]
command from libvirt [2], it completes all virsh commands and their
respective options.

I think it's pretty good and hopefully the caching approach is sane (of
course it'd be nice to get rid of that awk call but no biggie).

Two somewhat questions that came to my mind:

- virsh help <command> output is pretty much like --help output of any
other program, is there a trick to make the option descriptions also
available when completing virsh command options?

- would it make sense (or is it perhaps already possible somehow) to
optionally enable "best guess" completion for all commands which have
no command specific completion rules available yet? There are lots of
commands for which completing just the options captured from --help
output would already be hugely helpful. With some other commands (e.g.,
git) that would fall flat but for many smaller / trivial commands that
might be just well enough. (If there's this kind of feature already
available, then I've just missed that.)

Anyway, here's the patch, please apply if it looks good.

1) https://www.mankier.com/1/virsh
2) https://libvirt.org/

---
 Completion/Unix/Command/_virsh | 50 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 50 insertions(+)
 create mode 100644 Completion/Unix/Command/_virsh

diff --git a/Completion/Unix/Command/_virsh b/Completion/Unix/Command/_virsh
new file mode 100644
index 0000000..179ae86
--- /dev/null
+++ b/Completion/Unix/Command/_virsh
@@ -0,0 +1,50 @@
+#compdef virsh
+
+local curcontext="$curcontext" state expl ret=1
+
+_arguments -A "-*" -C -S -s -w \
+  '(- *)'{-h,--help}'[print help information and exit]' \
+  '(- *)'{-v,--version=short}'[print short version information and exit]' \
+  '(- *)'{-V,--version=long}'[print long version information and exit]' \
+  '(-c --connect)'{-c+,--connect}'[specify connection URI]:URI:_hosts' \
+  '(-d --debug)'{-d+,--debug}'[set debug level]:level:(0 1 2 3 4)' \
+  '(-e --escape)'{-e+,--escape}'[set escape sequence for console]:sequence' \
+  '(-k --keepalive-interval)'{-k+,--keepalive-interval}'[set keepalive interval]:interval' \
+  '(-K --keepalive-count)'{-K+,--keepalive-count}'[set keepalive count]:count' \
+  '(-l --log)'{-l+,--log}'[specify log file]:file:_files' \
+  '(-q --quiet)'{-q,--quiet}'[quiet mode]' \
+  '(-r --readonly)'{-r,--readonly}'[connect readonly]' \
+  '(-t --timing)'{-t,--timing}'[print timing information]' \
+  '1:command:->commands' \
+  '*:cmdopt:->cmdopts' \
+  && return 0
+
+# We accept only virsh command options after the first non-option argument
+# (i.e., the virsh command itself), this makes it so with the -A "-*" above
+[[ -z $state ]] && state=cmdopts
+
+if (( ! $+_cache_virsh_cmds )); then
+  _cache_virsh_cmds=( ${="$(virsh help 2>&1 | awk '!/:/ {print $1}')"} )
+fi
+if (( ! $+_cache_virsh_cmdopts )); then
+  typeset -gA _cache_virsh_cmdopts
+fi
+
+case $state in
+  commands)
+    _wanted commands expl 'virsh command' compadd -a _cache_virsh_cmds && ret=0
+  ;;
+  cmdopts)
+    local cmd
+    for (( i = 2; i <= $#words; i++ )); do
+      [[ -n "${_cache_virsh_cmds[(r)$words[$i]]}" ]] && cmd=$words[$i] && break
+    done
+    [[ -z $cmd ]] && return 1
+    if [[ -z $_cache_virsh_cmdopts[$cmd] ]]; then
+      _cache_virsh_cmdopts[$cmd]=${(M)${${${${=${(f)"$(virsh help $cmd 2>&1)"}}/\[}/\]}/\;}:#-[-0-9A-Za-z]*}
+    fi
+    _values -w options ${=_cache_virsh_cmdopts[$cmd]} && ret=0
+  ;;
+esac
+
+return ret

Thanks,

-- 
Marko Myllynen



Messages sorted by: Reverse Date, Date, Thread, Author