bash: Implement tab completion for virt-win-reg (RHBZ#1367738).

This only implements long and short options.

Thanks: Ming Xie.
This commit is contained in:
Richard W.M. Jones
2017-03-24 08:59:53 +00:00
parent 7cf85ace19
commit 5acd69ed6c
4 changed files with 50 additions and 13 deletions

1
.gitignore vendored
View File

@@ -73,6 +73,7 @@ Makefile.in
/bash/virt-tail
/bash/virt-tar-in
/bash/virt-tar-out
/bash/virt-win-reg
/build-aux/.gitignore
/build-aux/ar-lib
/build-aux/compile

View File

@@ -51,7 +51,8 @@ symlinks = \
virt-sysprep \
virt-tail \
virt-tar-in \
virt-tar-out
virt-tar-out \
virt-win-reg
# Note: Don't distribute the symbolic links, only the real files.
EXTRA_DIST = \
@@ -62,11 +63,17 @@ EXTRA_DIST = \
CLEANFILES += \
$(symlinks)
# Any tool that has --short-options and --long-options is handled by
# this common script. However this script cannot deal with commands
# that use --ro/--rw (eg. virt-rescue), nor commands that have lots of
# exceptions (eg. guestfish). Those tools have to be handled
# individually.
# Any tool that has --short-options and --long-options only is handled
# by this common script.
virt-win-reg:
rm -f $@
$(LN_S) virt-v2v-copy-to-local $@
# Any tool that has --short-options and --long-options and a few
# common options like -d is handled by this common script. However
# this script cannot deal with commands that use --ro/--rw
# (eg. virt-rescue), nor commands that have lots of exceptions
# (eg. guestfish). Those tools have to be handled individually.
guestunmount \
virt-builder virt-cat virt-customize virt-df virt-dib virt-diff \
virt-edit virt-filesystems virt-format virt-get-kernel virt-inspector \

View File

@@ -15,28 +15,39 @@
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
_virt_v2v_copy_to_local ()
_guestfs_options_only ()
{
local cur prev words cword split
local shortopts longopts items
local shortopts longopts tool="$1"
_init_completion -s || return
case "$cur" in
--*)
# --options
longopts="$(virt-v2v-copy-to-local --long-options)"
longopts="$($tool --long-options)"
COMPREPLY=( $(compgen -W "$longopts" -- "$cur") )
return ;;
-*)
# -o and --options
shortopts="$(virt-v2v-copy-to-local --short-options)"
longopts="$(virt-v2v-copy-to-local --long-options)"
shortopts="$($tool --short-options)"
longopts="$($tool --long-options)"
COMPREPLY=( $(compgen -W "$shortopts $longopts" -- "$cur") )
return ;;
*)
COMPREPLY=( $(compgen "$cur") )
return ;;
esac
}
_virt_v2v_copy_to_local ()
{
_guestfs_options_only "virt-v2v-copy-to-local"
} &&
complete -o default -F _virt_v2v_copy_to_local virt-v2v-copy-to-local
_virt_win_reg ()
{
_guestfs_options_only "virt-win-reg"
} &&
complete -o default -F _virt_win_reg virt-win-reg

View File

@@ -221,7 +221,7 @@ passed into another program or stored in another Registry.
=cut
GetOptions ("help|?" => \$help,
my %opts = ("help|?" => \$help,
"version" => \$version,
"connect|c=s" => \$uri,
"debug|d" => \$debug,
@@ -229,7 +229,9 @@ GetOptions ("help|?" => \$help,
"merge" => \$merge,
"encoding=s" => \$encoding,
"unsafe-printable-strings" => \$unsafe_printable_strings,
) or pod2usage (2);
"long-options" => \&display_long_options,
"short-options" => \&display_short_options);
GetOptions (%opts) or pod2usage (2);
pod2usage (1) if $help;
if ($version) {
my $g = Sys::Guestfs->new ();
@@ -238,6 +240,22 @@ if ($version) {
exit
}
sub display_long_options
{
foreach (sort keys %opts) {
if (m/^(.*?)([\|=].*)?$/ && !/-options$/) { print "--$1\n" }
}
exit
}
sub display_short_options
{
foreach (sort keys %opts) {
if (m/\|(.)/) { print "-$1\n" }
}
exit
}
# virt-win-reg only takes a single disk image ...
die __"no libvirt domain name or disk image given\n" if @ARGV == 0;
my $domname_or_image = shift @ARGV;