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

Richard W.M. Jones rjones at redhat.com
Fri Mar 24 09:00:43 UTC 2017


This only implements long and short options.

Thanks: Ming Xie.
---
 .gitignore                  |  1 +
 bash/Makefile.am            | 19 +++++++++++++------
 bash/virt-v2v-copy-to-local | 21 ++++++++++++++++-----
 tools/virt-win-reg          | 22 ++++++++++++++++++++--
 4 files changed, 50 insertions(+), 13 deletions(-)

diff --git a/.gitignore b/.gitignore
index c82745e..5c7cd53 100644
--- a/.gitignore
+++ b/.gitignore
@@ -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
diff --git a/bash/Makefile.am b/bash/Makefile.am
index 86fd816..983e8da 100644
--- a/bash/Makefile.am
+++ b/bash/Makefile.am
@@ -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 \
diff --git a/bash/virt-v2v-copy-to-local b/bash/virt-v2v-copy-to-local
index 4442da2..1296f26 100644
--- a/bash/virt-v2v-copy-to-local
+++ b/bash/virt-v2v-copy-to-local
@@ -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
diff --git a/tools/virt-win-reg b/tools/virt-win-reg
index 8ddc4f9..e173d71 100755
--- a/tools/virt-win-reg
+++ b/tools/virt-win-reg
@@ -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;
-- 
2.9.3




More information about the Libguestfs mailing list