[virt-tools-list] [PATCH] Bash completion for virt-manager utilities

Cole Robinson crobinso at redhat.com
Tue Oct 27 19:22:05 UTC 2015


On 10/24/2015 12:27 PM, Abhijeet Kasurde wrote:
> Added bash completion script for virt-xml, virt-install, virt-clone and
> virt-convert
> 
> Signed-off-by: Abhijeet Kasurde <akasurde at redhat.com>
> ---
>  data/bash.completion/virt-manager.bash | 82 ++++++++++++++++++++++++++++++++++
>  setup.py                               |  1 +
>  2 files changed, 83 insertions(+)
>  create mode 100644 data/bash.completion/virt-manager.bash
> 

Cool! Thanks for this. I only played with it a bit, but a couple comments.

- Name the file virt-install.bash instead
- Just put it in data/, not data/bash.completion
- There's lots of boilerplate, really the only difference between the
functions is the opt list. I suggest adding a function like _process_opts(),
and the callers just pass in their specific opts list.

One more comment inline

> diff --git a/data/bash.completion/virt-manager.bash b/data/bash.completion/virt-manager.bash
> new file mode 100644
> index 0000000..1ebc4ab
> --- /dev/null
> +++ b/data/bash.completion/virt-manager.bash
> @@ -0,0 +1,82 @@
> +_virt_install()
> +{
> +    local cur prev opts
> +    COMPREPLY=()
> +    cur="${COMP_WORDS[COMP_CWORD]}"
> +    prev="${COMP_WORDS[COMP_CWORD-1]}"
> +    opts="-h --help --version --connect -n --name --memory --vcpus --cpu \
> +        --metadata --cdrom -l --location --pxe --import --livecd --boot -x \
> +        --extra-args --initrd-inject --os-variant --idmap --disk --network \
> +        --graphics --controller --input --serial --parallel --channel -w \
> +        --console --hostdev --filesystem --sound --watchdog --video \
> +        --smartcard --redirdev --memballoon --tpm --rng --panic \
> +        --security --numatune --memtune --blkiotune --memorybacking \
> +        --features --clock --pm --events --resource -v --hvm -p --paravirt \
> +        --container --virt-type --arch --machine --autostart \
> +        --wait --noautoconsole --noreboot --print-xml --dry-run \
> +        --check --quiet --debug -q -d"
> +
> +    if [[ ${cur} == -* ]]; then
> +        COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
> +        return 0
> +    fi
> +}
> +
> +_virt_clone()
> +{
> +    local cur prev opts
> +    COMPREPLY=()
> +    cur="${COMP_WORDS[COMP_CWORD]}"
> +    prev="${COMP_WORDS[COMP_CWORD-1]}"
> +    opts="-h --help --version --connect -o --original --original-xml --auto-clone \
> +        -n --name --reflink -f --file --force-copy --nonsparse --preserve-data -m \
> +        --mac --replace --print-xml --check -q --quiet -d --debug"
> +
> +    if [[ ${cur} == -* ]]; then
> +        COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
> +        return 0
> +    fi
> +}
> +
> +_virt_convert()
> +{
> +    local cur prev opts
> +    COMPREPLY=()
> +    cur="${COMP_WORDS[COMP_CWORD]}"
> +    prev="${COMP_WORDS[COMP_CWORD-1]}"
> +    opts="-h --help --version --connect -i --input-format -D --disk-format \
> +        --destination --noautoconsole --print-xml --dry-run -q --quiet -d  \
> +        --debug"
> +
> +    if [[ ${cur} == -* ]]; then
> +        COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
> +        return 0
> +    fi
> +}
> +
> +_virt_xml()
> +{
> +    local cur prev opts
> +    COMPREPLY=()
> +    cur="${COMP_WORDS[COMP_CWORD]}"
> +    prev="${COMP_WORDS[COMP_CWORD-1]}"
> +    opts="-h --help --version -c --connect --edit --remove-device --add-device \
> +        --build-xml --update --define --print-diff --print-xml --confirm --disk \
> +        -w --network --graphics --metadata --memory --vcpus --cpu --security \
> +        --numatune --memtune --blkiotune --memorybacking --features --clock \
> +        --pm --events --resource --boot --idmap --controller --input --serial \
> +        --parallel --channel --console --hostdev --filesystem --sound \
> +        --watchdog --video --smartcard --redirdev --memballoon --tpm --rng \
> +        --panic -q --quiet -d --debug"
> +
> +    echo $COMP_CWORD

This looks like leftover debugging, but if you unify the code it will go away

> +    if [[ ${cur} == -* ]]; then
> +        COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
> +        return 0
> +    fi
> +}
> +
> +complete -F _virt_install virt-install
> +complete -F _virt_clone virt-clone
> +complete -F _virt_convert virt-convert
> +complete -F _virt_xml virt-xml
> diff --git a/setup.py b/setup.py
> index 72f4d0f..d48d1b5 100755
> --- a/setup.py
> +++ b/setup.py
> @@ -637,6 +637,7 @@ setup(
>           glob.glob("virtcli/*.py") + glob.glob("virtcli/cli.cfg")),
>          ("share/virt-manager/virtinst", glob.glob("virtinst/*.py")),
>          ("share/virt-manager/virtconv", glob.glob("virtconv/*.py")),
> +        ("/etc/bash_completion.d", ["data/bash.completion/virt-manager.bash"]),
>      ],
>  
>      cmdclass={
> 


Future work as a note to self:

- Wire it up in the RPM
- Consider generating the option list rather than needing to update it
manually. Probably not hard to add a hidden cli option like 'virt-install
--completion-options' that spits out a list to stderr that we can just stuff
in the script at build time
- Investigate what it would take to add support for command sub options, and
differentiating between boolean and non-boolean options, then file
virt-manager bugs for it

- Cole




More information about the virt-tools-list mailing list