From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 16306 invoked by alias); 11 Jul 2016 12:14:22 -0000 Mailing-List: contact zsh-workers-help@zsh.org; run by ezmlm Precedence: bulk X-No-Archive: yes List-Id: Zsh Workers List List-Post: List-Help: X-Seq: 38825 Received: (qmail 11461 invoked from network); 11 Jul 2016 12:14:21 -0000 X-Spam-Checker-Version: SpamAssassin 3.4.1 (2015-04-28) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.1 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:reply-to:to:from:subject:organization:message-id :date:user-agent:mime-version:content-transfer-encoding; bh=JWnQFIwvpZvV2HY6NZVG8evjDKjW4J6UCjCkb/X8TzQ=; b=kd+qgGbjfgQb9rhDRAffolRzmP9xYbgeq6lPF5uy0hlcjmOJXtMR/FkI1ui5evPT9g uMstA+KbcaPt10QHXSRXBPHTjcQKEX6iJ+RrwReYAOASWZaFB4Cou5FnsToNfJhA9cJQ w5ddzyesTDZyf27GSocnrJFFzPyv+Nuzuc6JovXh4DrPNynYHCZkNQ9idXAaBSSqFhP7 exDE8nZA4M2zU1M5HXiK1fUCnfRxCnstKZGlNJWihwIR4lZunUeGzW7MoXAtmX5VqBWd 7NP8DFLGOUnrYvjecGWs9HJuh6/9/h2aafNE59EkDQ3LOv9pC9/YoXiolXkuThljPaTH v7Yg== X-Gm-Message-State: ALyK8tICvsozIrDcrGhUNlZDnCvxVLA2OCWdKcWjt9uy4SHAFN5/ll32FX0coauEfBFtzrx0 X-Received: by 10.28.35.86 with SMTP id j83mr17214236wmj.18.1468237925633; Mon, 11 Jul 2016 04:52:05 -0700 (PDT) Reply-To: Marko Myllynen To: zsh workers From: Marko Myllynen Subject: zsh virsh completion Organization: Red Hat Message-ID: <8eb6dce0-50d7-5ab2-503a-194c1de2e45d@redhat.com> Date: Mon, 11 Jul 2016 14:52:02 +0300 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.2 MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit 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 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