zsh-workers
 help / color / mirror / code / Atom feed
* zsh virsh completion
@ 2016-07-11 11:52 Marko Myllynen
  2016-07-11 15:03 ` Roman Neuhauser
                   ` (3 more replies)
  0 siblings, 4 replies; 25+ messages in thread
From: Marko Myllynen @ 2016-07-11 11:52 UTC (permalink / raw)
  To: zsh workers

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


^ permalink raw reply	[flat|nested] 25+ messages in thread

end of thread, other threads:[~2016-09-14 23:25 UTC | newest]

Thread overview: 25+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-07-11 11:52 zsh virsh completion Marko Myllynen
2016-07-11 15:03 ` Roman Neuhauser
2016-07-11 16:40 ` Bart Schaefer
2016-07-11 22:07 ` Oliver Kiddle
2016-07-12 10:23   ` Marko Myllynen
2016-07-13  4:59 ` Daniel Shahaf
2016-07-18 12:06   ` Marko Myllynen
2016-07-20  6:58     ` [PATCH] _virsh (Was: Re: zsh virsh completion) Daniel Shahaf
2016-07-20  8:36       ` Marko Myllynen
2016-07-21  6:57         ` Daniel Shahaf
2016-07-21 12:32           ` Marko Myllynen
2016-07-22  6:30             ` Daniel Shahaf
2016-07-22  8:17               ` Marko Myllynen
2016-07-21 16:12         ` Oliver Kiddle
2016-07-21 16:19           ` Marko Myllynen
2016-07-22  7:19           ` Daniel Shahaf
2016-08-31 21:15             ` Oliver Kiddle
2016-09-02  5:23               ` Daniel Shahaf
2016-09-02 15:02                 ` Oliver Kiddle
2016-09-04  4:01                   ` Daniel Shahaf
2016-09-07  6:39                     ` Bart Schaefer
2016-09-09 22:09                       ` Oliver Kiddle
2016-09-11  9:08                         ` Daniel Shahaf
2016-09-14 23:19                     ` Oliver Kiddle
2016-09-04 21:24                   ` Daniel Shahaf

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).