[libvirt] virsh bash completion file

Eric Blake eblake at redhat.com
Wed Oct 5 20:33:44 UTC 2011


On 10/05/2011 02:17 PM, Serge E. Hallyn wrote:
> Hi,
>
> I've been trying out a bash autocompletion file by Geoff Low (slight hack
> by me, don't blame him for my hack), and it's working pretty nicely.
> I'm not sure where to put it in the git tree, but it seems like it'd be
> nice to have upstream?

What I'd rather have upstream is:

virsh completion args...

which outputs one string per line of valid completions given the context 
of args.  Then we could leverage a single completion code both from bash 
and from the virsh interactive shell; also, you'd only have to write the 
bash completion routines once (figure out how to make it call into 
'virsh completion'), rather than chasing a moving target (update the 
bash completion every time new virsh commands and flags are added).

>
> VIRSH=$(which virsh)

Not quite right.  Rather, this should be an add-on to the 
bash-completion project, at which point you are writing a dropin for 
/etc/bash_completion.d/virsh, and where you start life with:

# helper functions...

have virsh &&
_virsh()
{
     ... main completion
} &&
complete -F _virsh virsh

>
> function get_main_option_list

Don't use this obsolete syntax.  Stick to the POSIX function syntax 
(look at existing files in /etc/bash_completion.d for pointers, or see 
my above example of _virsh(){}).

> {
>      # assume options are specified as [--option1 | --option2 | ...]
>      OPTIONS=$(${VIRSH} help | grep '|' | sed -e 's/.*\[\(.*\)\]/\1/g;s/|//g')

To some extent, bash_completion already offers some helper functions 
that can already do this work for most applications, without having to 
reinvent the wheel.

Furthermore, this doesn't work - it matches nothing:

$ virsh help | grep '|'
$

While I'd love to see better bash completion support, I think that we 
should be going about it by fixing virsh to make it easier to query what 
completions make sense, so I'm not going to spend much time further 
reviewing this.  Of course, others are free to use this in the interim, 
while we still lack decent virsh support.

-- 
Eric Blake   eblake at redhat.com    +1-801-349-2682
Libvirt virtualization library http://libvirt.org




More information about the libvir-list mailing list