zsh-workers
 help / color / mirror / code / Atom feed
From: Marko Myllynen <myllynen@redhat.com>
To: zsh workers <zsh-workers@zsh.org>
Subject: zsh virsh completion
Date: Mon, 11 Jul 2016 14:52:02 +0300	[thread overview]
Message-ID: <8eb6dce0-50d7-5ab2-503a-194c1de2e45d@redhat.com> (raw)

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


             reply	other threads:[~2016-07-11 12:14 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-07-11 11:52 Marko Myllynen [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=8eb6dce0-50d7-5ab2-503a-194c1de2e45d@redhat.com \
    --to=myllynen@redhat.com \
    --cc=zsh-workers@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).