Just like --long-options, it makes it possible to know which short options are supported by each tool; this can help improving the bash completion, for example. --- align/scan.c | 3 +++ builder/cmdline.ml | 1 + cat/cat.c | 3 +++ cat/filesystems.c | 3 +++ cat/log.c | 3 +++ cat/ls.c | 3 +++ customize/customize_main.ml | 1 + df/main.c | 3 +++ diff/diff.c | 3 +++ edit/edit.c | 3 +++ fish/fish.c | 3 +++ fish/options.c | 16 +++++++++++++++- fish/options.h | 1 + format/format.c | 3 +++ fuse/guestmount.c | 3 +++ inspector/inspector.c | 3 +++ make-fs/make-fs.c | 4 ++++ mllib/common_utils.ml | 9 ++++++++- mllib/common_utils.mli | 2 ++ p2v/main.c | 17 ++++++++++++++++- rescue/rescue.c | 3 +++ resize/resize.ml | 1 + sparsify/cmdline.ml | 1 + sysprep/main.ml | 1 + v2v/cmdline.ml | 1 + 25 files changed, 91 insertions(+), 3 deletions(-) diff --git a/align/scan.c b/align/scan.c index e803d8e..7da468f 100644 --- a/align/scan.c +++ b/align/scan.c @@ -115,6 +115,7 @@ main (int argc, char *argv[]) { "help", 0, 0, HELP_OPTION }, { "long-options", 0, 0, 0 }, { "quiet", 0, 0, 'q' }, + { "short-options", 0, 0, 0 }, { "uuid", 0, 0, 0, }, { "verbose", 0, 0, 'v' }, { "version", 0, 0, 'V' }, @@ -143,6 +144,8 @@ main (int argc, char *argv[]) case 0: /* options which are long only */ if (STREQ (long_options[option_index].name, "long-options")) display_long_options (long_options); + else if (STREQ (long_options[option_index].name, "short-options")) + display_short_options (options); else if (STREQ (long_options[option_index].name, "format")) { OPTION_format; } else if (STREQ (long_options[option_index].name, "uuid")) { diff --git a/builder/cmdline.ml b/builder/cmdline.ml index e21d5bb..bb7b1d0 100644 --- a/builder/cmdline.ml +++ b/builder/cmdline.ml @@ -135,6 +135,7 @@ let parse_cmdline () "--long", Arg.Unit list_set_long, " " ^ s_"Shortcut for --list-format short"; "--list-format", Arg.String list_set_format, "short|long|json" ^ " " ^ s_"Set the format for --list (default: short)"; + "--short-options", Arg.Unit display_short_options, " " ^ s_"List short options"; "--long-options", Arg.Unit display_long_options, " " ^ s_"List long options"; "--machine-readable", Arg.Set machine_readable, " " ^ s_"Make output machine readable"; "-m", Arg.Int set_memsize, "mb" ^ " " ^ s_"Set memory size"; diff --git a/cat/cat.c b/cat/cat.c index ec9c81f..fdfa3a9 100644 --- a/cat/cat.c +++ b/cat/cat.c @@ -99,6 +99,7 @@ main (int argc, char *argv[]) { "keys-from-stdin", 0, 0, 0 }, { "long-options", 0, 0, 0 }, { "mount", 1, 0, 'm' }, + { "short-options", 0, 0, 0 }, { "verbose", 0, 0, 'v' }, { "version", 0, 0, 'V' }, { 0, 0, 0, 0 } @@ -128,6 +129,8 @@ main (int argc, char *argv[]) case 0: /* options which are long only */ if (STREQ (long_options[option_index].name, "long-options")) display_long_options (long_options); + else if (STREQ (long_options[option_index].name, "short-options")) + display_short_options (options); else if (STREQ (long_options[option_index].name, "keys-from-stdin")) { keys_from_stdin = 1; } else if (STREQ (long_options[option_index].name, "echo-keys")) { diff --git a/cat/filesystems.c b/cat/filesystems.c index f9f887a..79d12bd 100644 --- a/cat/filesystems.c +++ b/cat/filesystems.c @@ -164,6 +164,7 @@ main (int argc, char *argv[]) { "physical-volumes", 0, 0, 0 }, { "physvols", 0, 0, 0 }, { "pvs", 0, 0, 0 }, + { "short-options", 0, 0, 0 }, { "uuid", 0, 0, 0 }, { "uuids", 0, 0, 0 }, { "verbose", 0, 0, 'v' }, @@ -197,6 +198,8 @@ main (int argc, char *argv[]) case 0: /* options which are long only */ if (STREQ (long_options[option_index].name, "long-options")) display_long_options (long_options); + else if (STREQ (long_options[option_index].name, "short-options")) + display_short_options (options); else if (STREQ (long_options[option_index].name, "keys-from-stdin")) { keys_from_stdin = 1; } else if (STREQ (long_options[option_index].name, "echo-keys")) { diff --git a/cat/log.c b/cat/log.c index 0d8dab0..9e8a7bf 100644 --- a/cat/log.c +++ b/cat/log.c @@ -105,6 +105,7 @@ main (int argc, char *argv[]) { "help", 0, 0, HELP_OPTION }, { "keys-from-stdin", 0, 0, 0 }, { "long-options", 0, 0, 0 }, + { "short-options", 0, 0, 0 }, { "verbose", 0, 0, 'v' }, { "version", 0, 0, 'V' }, { 0, 0, 0, 0 } @@ -130,6 +131,8 @@ main (int argc, char *argv[]) case 0: /* options which are long only */ if (STREQ (long_options[option_index].name, "long-options")) display_long_options (long_options); + else if (STREQ (long_options[option_index].name, "short-options")) + display_short_options (options); else if (STREQ (long_options[option_index].name, "keys-from-stdin")) { keys_from_stdin = 1; } else if (STREQ (long_options[option_index].name, "echo-keys")) { diff --git a/cat/ls.c b/cat/ls.c index 2cb4e74..b979928 100644 --- a/cat/ls.c +++ b/cat/ls.c @@ -151,6 +151,7 @@ main (int argc, char *argv[]) { "long-options", 0, 0, 0 }, { "mount", 1, 0, 'm' }, { "recursive", 0, 0, 'R' }, + { "short-options", 0, 0, 0 }, { "time", 0, 0, 0 }, { "times", 0, 0, 0 }, { "time-days", 0, 0, 0 }, @@ -190,6 +191,8 @@ main (int argc, char *argv[]) case 0: /* options which are long only */ if (STREQ (long_options[option_index].name, "long-options")) display_long_options (long_options); + else if (STREQ (long_options[option_index].name, "short-options")) + display_short_options (options); else if (STREQ (long_options[option_index].name, "keys-from-stdin")) { keys_from_stdin = 1; } else if (STREQ (long_options[option_index].name, "echo-keys")) { diff --git a/customize/customize_main.ml b/customize/customize_main.ml index 2830e8d..5bba71a 100644 --- a/customize/customize_main.ml +++ b/customize/customize_main.ml @@ -93,6 +93,7 @@ let main () "--dry-run", Arg.Set dryrun, " " ^ s_"Perform a dry run"; "--format", Arg.String set_format, s_"format" ^ " " ^ s_"Set format (default: auto)"; "--long-options", Arg.Unit display_long_options, " " ^ s_"List long options"; + "--short-options", Arg.Unit display_short_options, " " ^ s_"List short options"; "-m", Arg.Int set_memsize, "mb" ^ " " ^ s_"Set memory size"; "--memsize", Arg.Int set_memsize, "mb" ^ " " ^ s_"Set memory size"; "--network", Arg.Set network, " " ^ s_"Enable appliance network (default)"; diff --git a/df/main.c b/df/main.c index bddf2a9..09a58eb 100644 --- a/df/main.c +++ b/df/main.c @@ -115,6 +115,7 @@ main (int argc, char *argv[]) { "inodes", 0, 0, 'i' }, { "long-options", 0, 0, 0 }, { "one-per-guest", 0, 0, 0 }, + { "short-options", 0, 0, 0 }, { "uuid", 0, 0, 0 }, { "verbose", 0, 0, 'v' }, { "version", 0, 0, 'V' }, @@ -143,6 +144,8 @@ main (int argc, char *argv[]) case 0: /* options which are long only */ if (STREQ (long_options[option_index].name, "long-options")) display_long_options (long_options); + else if (STREQ (long_options[option_index].name, "short-options")) + display_short_options (options); else if (STREQ (long_options[option_index].name, "format")) { OPTION_format; } else if (STREQ (long_options[option_index].name, "csv")) { diff --git a/diff/diff.c b/diff/diff.c index b113f29..8b0fda0 100644 --- a/diff/diff.c +++ b/diff/diff.c @@ -167,6 +167,7 @@ main (int argc, char *argv[]) { "help", 0, 0, HELP_OPTION }, { "human-readable", 0, 0, 'h' }, { "keys-from-stdin", 0, 0, 0 }, + { "short-options", 0, 0, 0 }, { "time", 0, 0, 0 }, { "times", 0, 0, 0 }, { "time-days", 0, 0, 0 }, @@ -208,6 +209,8 @@ main (int argc, char *argv[]) case 0: /* options which are long only */ if (STREQ (long_options[option_index].name, "long-options")) display_long_options (long_options); + else if (STREQ (long_options[option_index].name, "short-options")) + display_short_options (options); else if (STREQ (long_options[option_index].name, "keys-from-stdin")) { keys_from_stdin = 1; } else if (STREQ (long_options[option_index].name, "echo-keys")) { diff --git a/edit/edit.c b/edit/edit.c index 20b2963..6281a2a 100644 --- a/edit/edit.c +++ b/edit/edit.c @@ -117,6 +117,7 @@ main (int argc, char *argv[]) { "keys-from-stdin", 0, 0, 0 }, { "long-options", 0, 0, 0 }, { "mount", 1, 0, 'm' }, + { "short-options", 0, 0, 0 }, { "verbose", 0, 0, 'v' }, { "version", 0, 0, 'V' }, { 0, 0, 0, 0 } @@ -145,6 +146,8 @@ main (int argc, char *argv[]) case 0: /* options which are long only */ if (STREQ (long_options[option_index].name, "long-options")) display_long_options (long_options); + else if (STREQ (long_options[option_index].name, "short-options")) + display_short_options (options); else if (STREQ (long_options[option_index].name, "keys-from-stdin")) { keys_from_stdin = 1; } else if (STREQ (long_options[option_index].name, "echo-keys")) { diff --git a/fish/fish.c b/fish/fish.c index 981a7d4..0432774 100644 --- a/fish/fish.c +++ b/fish/fish.c @@ -204,6 +204,7 @@ main (int argc, char *argv[]) { "ro", 0, 0, 'r' }, { "rw", 0, 0, 'w' }, { "selinux", 0, 0, 0 }, + { "short-options", 0, 0, 0 }, { "verbose", 0, 0, 'v' }, { "version", 0, 0, 'V' }, { 0, 0, 0, 0 } @@ -245,6 +246,8 @@ main (int argc, char *argv[]) case 0: /* options which are long only */ if (STREQ (long_options[option_index].name, "long-options")) display_long_options (long_options); + else if (STREQ (long_options[option_index].name, "short-options")) + display_short_options (options); else if (STREQ (long_options[option_index].name, "listen")) remote_control_listen = 1; else if (STREQ (long_options[option_index].name, "remote")) { diff --git a/fish/options.c b/fish/options.c index f7870a4..9ffcc6b 100644 --- a/fish/options.c +++ b/fish/options.c @@ -340,6 +340,20 @@ free_mps (struct mp *mp) free (mp); } +/* Implements the internal 'tool --short-options' flag, which just + * lists out the short options available. Used by bash completion. + */ +void +display_short_options (const char *format) +{ + while (*format) { + if (*format != ':') + printf ("-%c\n", *format); + ++format; + } + exit (EXIT_SUCCESS); +} + /* Implements the internal 'tool --long-options' flag, which just * lists out the long options available. Used by bash completion. */ @@ -347,7 +361,7 @@ void display_long_options (const struct option *long_options) { while (long_options->name) { - if (STRNEQ (long_options->name, "long-options")) + if (STRNEQ (long_options->name, "long-options") && STRNEQ (long_options->name, "short-options")) printf ("--%s\n", long_options->name); long_options++; } diff --git a/fish/options.h b/fish/options.h index 2c568e6..cf68122 100644 --- a/fish/options.h +++ b/fish/options.h @@ -138,6 +138,7 @@ extern char add_drives_handle (guestfs_h *g, struct drv *drv, char next_drive); extern void mount_mps (struct mp *mp); extern void free_drives (struct drv *drv); extern void free_mps (struct mp *mp); +extern void display_short_options (const char *) __attribute__((noreturn)); extern void display_long_options (const struct option *) __attribute__((noreturn)); #define OPTION_a \ diff --git a/format/format.c b/format/format.c index 1651f31..4e6069f 100644 --- a/format/format.c +++ b/format/format.c @@ -113,6 +113,7 @@ main (int argc, char *argv[]) { "long-options", 0, 0, 0 }, { "lvm", 2, 0, 0 }, { "partition", 2, 0, 0 }, + { "short-options", 0, 0, 0 }, { "verbose", 0, 0, 'v' }, { "version", 0, 0, 'V' }, { "wipe", 0, 0, 0 }, @@ -139,6 +140,8 @@ main (int argc, char *argv[]) case 0: /* options which are long only */ if (STREQ (long_options[option_index].name, "long-options")) display_long_options (long_options); + else if (STREQ (long_options[option_index].name, "short-options")) + display_short_options (options); else if (STREQ (long_options[option_index].name, "format")) { OPTION_format; } else if (STREQ (long_options[option_index].name, "filesystem")) { diff --git a/fuse/guestmount.c b/fuse/guestmount.c index 519bba3..f952c47 100644 --- a/fuse/guestmount.c +++ b/fuse/guestmount.c @@ -173,6 +173,7 @@ main (int argc, char *argv[]) { "ro", 0, 0, 'r' }, { "rw", 0, 0, 'w' }, { "selinux", 0, 0, 0 }, + { "short-options", 0, 0, 0 }, { "trace", 0, 0, 'x' }, { "verbose", 0, 0, 'v' }, { "version", 0, 0, 'V' }, @@ -220,6 +221,8 @@ main (int argc, char *argv[]) case 0: /* options which are long only */ if (STREQ (long_options[option_index].name, "long-options")) display_long_options (long_options); + else if (STREQ (long_options[option_index].name, "short-options")) + display_short_options (options); else if (STREQ (long_options[option_index].name, "dir-cache-timeout")) dir_cache_timeout = atoi (optarg); else if (STREQ (long_options[option_index].name, "fuse-help")) diff --git a/inspector/inspector.c b/inspector/inspector.c index 71795ce..5b99efa 100644 --- a/inspector/inspector.c +++ b/inspector/inspector.c @@ -111,6 +111,7 @@ main (int argc, char *argv[]) { "help", 0, 0, HELP_OPTION }, { "keys-from-stdin", 0, 0, 0 }, { "long-options", 0, 0, 0 }, + { "short-options", 0, 0, 0 }, { "verbose", 0, 0, 'v' }, { "version", 0, 0, 'V' }, { "xpath", 1, 0, 0 }, @@ -137,6 +138,8 @@ main (int argc, char *argv[]) case 0: /* options which are long only */ if (STREQ (long_options[option_index].name, "long-options")) display_long_options (long_options); + else if (STREQ (long_options[option_index].name, "short-options")) + display_short_options (options); else if (STREQ (long_options[option_index].name, "keys-from-stdin")) { keys_from_stdin = 1; } else if (STREQ (long_options[option_index].name, "echo-keys")) { diff --git a/make-fs/make-fs.c b/make-fs/make-fs.c index 406aa1d..841c202 100644 --- a/make-fs/make-fs.c +++ b/make-fs/make-fs.c @@ -59,6 +59,7 @@ static const struct option long_options[] = { { "label", 1, 0, 0 }, { "long-options", 0, 0, 0 }, { "partition", 2, 0, 0 }, + { "short-options", 0, 0, 0 }, { "size", 1, 0, 's' }, { "type", 1, 0, 't' }, { "verbose", 0, 0, 'v' }, @@ -125,6 +126,9 @@ main (int argc, char *argv[]) if (STREQ (long_options[option_index].name, "long-options")) { display_long_options (long_options); } + else if (STREQ (long_options[option_index].name, "short-options")) { + display_short_options (options); + } else if (STREQ (long_options[option_index].name, "floppy")) { size_str = "1440K"; partition = "mbr"; diff --git a/mllib/common_utils.ml b/mllib/common_utils.ml index 83ebd3a..89d8412 100644 --- a/mllib/common_utils.ml +++ b/mllib/common_utils.ml @@ -421,8 +421,15 @@ let skip_dashes str let compare_command_line_args a b compare (String.lowercase (skip_dashes a)) (String.lowercase (skip_dashes b)) -(* Implements `--long-options'. *) +(* Implement `--short-options' and `--long-options'. *) let long_options = ref ([] : (Arg.key * Arg.spec * Arg.doc) list) +let display_short_options () + List.iter ( + fun (arg, _, _) -> + if string_prefix arg "-" && not (string_prefix arg "--") then + printf "%s\n" arg + ) !long_options; + exit 0 let display_long_options () List.iter ( fun (arg, _, _) -> diff --git a/mllib/common_utils.mli b/mllib/common_utils.mli index 112648a..bcdeecf 100644 --- a/mllib/common_utils.mli +++ b/mllib/common_utils.mli @@ -92,6 +92,8 @@ val compare_command_line_args : string -> string -> int (** Compare command line arguments for equality, ignoring any leading [-]s. *) val long_options : (Arg.key * Arg.spec * Arg.doc) list ref +val display_short_options : unit -> 'a +(** Implements [--short-options]. *) val display_long_options : unit -> 'a (** Implements [--long-options]. *) diff --git a/p2v/main.c b/p2v/main.c index a93ca1c..226f574 100644 --- a/p2v/main.c +++ b/p2v/main.c @@ -53,6 +53,7 @@ static const struct option long_options[] = { { "help", 0, 0, HELP_OPTION }, { "cmdline", 1, 0, 0 }, { "long-options", 0, 0, 0 }, + { "short-options", 0, 0, 0 }, { "verbose", 0, 0, 'v' }, { "version", 0, 0, 'V' }, { 0, 0, 0, 0 } @@ -83,10 +84,21 @@ usage (int status) /* XXX Copied from fish/options.c. */ static void +display_short_options (const char *format) +{ + while (*format) { + if (*format != ':') + printf ("-%c\n", *format); + ++format; + } + exit (EXIT_SUCCESS); +} + +static void display_long_options (const struct option *long_options) { while (long_options->name) { - if (STRNEQ (long_options->name, "long-options")) + if (STRNEQ (long_options->name, "long-options") && STRNEQ (long_options->name, "short-options")) printf ("--%s\n", long_options->name); long_options++; } @@ -119,6 +131,9 @@ main (int argc, char *argv[]) if (STREQ (long_options[option_index].name, "long-options")) { display_long_options (long_options); } + else if (STREQ (long_options[option_index].name, "short-options")) { + display_short_options (options); + } else if (STREQ (long_options[option_index].name, "cmdline")) { cmdline = strdup (optarg); } diff --git a/rescue/rescue.c b/rescue/rescue.c index 00187a4..0295634 100644 --- a/rescue/rescue.c +++ b/rescue/rescue.c @@ -113,6 +113,7 @@ main (int argc, char *argv[]) { "rw", 0, 0, 'w' }, { "scratch", 2, 0, 0 }, { "selinux", 0, 0, 0 }, + { "short-options", 0, 0, 0 }, { "smp", 1, 0, 0 }, { "suggest", 0, 0, 0 }, { "verbose", 0, 0, 'v' }, @@ -145,6 +146,8 @@ main (int argc, char *argv[]) case 0: /* options which are long only */ if (STREQ (long_options[option_index].name, "long-options")) display_long_options (long_options); + else if (STREQ (long_options[option_index].name, "short-options")) + display_short_options (options); else if (STREQ (long_options[option_index].name, "selinux")) { if (guestfs_set_selinux (g, 1) == -1) exit (EXIT_FAILURE); diff --git a/resize/resize.ml b/resize/resize.ml index f54b11f..363e2b6 100644 --- a/resize/resize.ml +++ b/resize/resize.ml @@ -194,6 +194,7 @@ let main () "--no-extra-partition", Arg.Clear extra_partition, " " ^ s_"Don't create extra partition"; "--format", Arg.Set_string format, s_"format" ^ " " ^ s_"Format of input disk"; "--ignore", Arg.String (add ignores), s_"part" ^ " " ^ s_"Ignore partition"; + "--short-options", Arg.Unit display_short_options, " " ^ s_"List short options"; "--long-options", Arg.Unit display_long_options, " " ^ s_"List long options"; "--lv-expand", Arg.String (add lv_expands), s_"lv" ^ " " ^ s_"Expand logical volume"; "--LV-expand", Arg.String (add lv_expands), s_"lv" ^ ditto; diff --git a/sparsify/cmdline.ml b/sparsify/cmdline.ml index ec58dc1..88bab72 100644 --- a/sparsify/cmdline.ml +++ b/sparsify/cmdline.ml @@ -73,6 +73,7 @@ let parse_cmdline () "--ignore", Arg.String (add ignores), s_"fs" ^ " " ^ s_"Ignore filesystem"; "--in-place", Arg.Set in_place, " " ^ s_"Modify the disk image in-place"; "--inplace", Arg.Set in_place, ditto; + "--short-options", Arg.Unit display_short_options, " " ^ s_"List short options"; "--long-options", Arg.Unit display_long_options, " " ^ s_"List long options"; "--machine-readable", Arg.Set machine_readable, " " ^ s_"Make output machine readable"; "-o", Arg.Set_string option, s_"option" ^ " " ^ s_"Add qemu-img options"; diff --git a/sysprep/main.ml b/sysprep/main.ml index 057f4cb..1fe3725 100644 --- a/sysprep/main.ml +++ b/sysprep/main.ml @@ -138,6 +138,7 @@ let main () "--enable", Arg.String set_enable, s_"operations" ^ " " ^ s_"Enable specific operations"; "--format", Arg.String set_format, s_"format" ^ " " ^ s_"Set format (default: auto)"; "--list-operations", Arg.Unit list_operations, " " ^ s_"List supported operations"; + "--short-options", Arg.Unit display_short_options, " " ^ s_"List short options"; "--long-options", Arg.Unit display_long_options, " " ^ s_"List long options"; "--mount-options", Arg.Set_string mount_opts, s_"opts" ^ " " ^ s_"Set mount options (eg /:noatime;/var:rw,noatime)"; "--no-selinux-relabel", Arg.Unit (fun () -> ()), diff --git a/v2v/cmdline.ml b/v2v/cmdline.ml index 69a627f..85757e6 100644 --- a/v2v/cmdline.ml +++ b/v2v/cmdline.ml @@ -154,6 +154,7 @@ let parse_cmdline () "-ic", Arg.Set_string input_conn, "uri " ^ s_"Libvirt URI"; "-if", Arg.Set_string input_format, "format " ^ s_"Input format (for -i disk)"; + "--short-options", Arg.Unit display_short_options, " " ^ s_"List short options"; "--long-options", Arg.Unit display_long_options, " " ^ s_"List long options"; "--machine-readable", Arg.Set machine_readable, " " ^ s_"Make output machine readable"; "-n", Arg.String add_network, "in:out " ^ s_"Map network 'in' to 'out'"; -- 1.9.3
Pino Toscano
2014-Nov-26 12:06 UTC
[Libguestfs] [PATCH] bash: execute --long-options only when needed
Delay the execution of $tool --long-options only when really using its output. --- bash/guestfish | 3 +-- bash/guestmount | 3 +-- bash/virt-alignment-scan | 23 ++++++++++++----------- bash/virt-rescue | 3 +-- bash/virt-resize | 9 +++++---- 5 files changed, 20 insertions(+), 21 deletions(-) diff --git a/bash/guestfish b/bash/guestfish index 513fbe2..46d7847 100644 --- a/bash/guestfish +++ b/bash/guestfish @@ -35,8 +35,6 @@ _guestfish () _init_completion -s || return - longopts="$(guestfish --long-options)" - # See if user has specified certain options anywhere on the # command line before the current word. while [ $c -lt $COMP_CWORD ]; do @@ -60,6 +58,7 @@ _guestfish () case "$cur" in --*) # --options + longopts="$(guestfish --long-options)" COMPREPLY=( $(compgen -W "$longopts" -- "$cur") ) return ;; *) diff --git a/bash/guestmount b/bash/guestmount index 9d91f7a..db79521 100644 --- a/bash/guestmount +++ b/bash/guestmount @@ -35,8 +35,6 @@ _guestmount () _init_completion -s || return - longopts="$(guestmount --long-options)" - # See if user has specified certain options anywhere on the # command line before the current word. while [ $c -lt $COMP_CWORD ]; do @@ -57,6 +55,7 @@ _guestmount () case "$cur" in --*) # --options + longopts="$(guestmount --long-options)" COMPREPLY=( $(compgen -W "$longopts" -- "$cur") ) return ;; *) diff --git a/bash/virt-alignment-scan b/bash/virt-alignment-scan index 3c77baf..cef8365 100644 --- a/bash/virt-alignment-scan +++ b/bash/virt-alignment-scan @@ -31,7 +31,7 @@ _guestfs_virsh_list () _guestfs_virttools () { local cur prev words cword split - local longopts="$1" flag_ro="$2" doms + local longopts tool="$1" flag_ro="$2" doms _init_completion -s || return @@ -45,6 +45,7 @@ _guestfs_virttools () case "$cur" in --*) # --options + longopts="$($tool --long-options)" COMPREPLY=( $(compgen -W "$longopts" -- "$cur") ) return ;; *) @@ -55,60 +56,60 @@ _guestfs_virttools () _virt_alignment_scan () { - _guestfs_virttools "$(virt-alignment-scan --long-options)" 1 + _guestfs_virttools "virt-alignment-scan" 1 } && complete -o default -F _virt_alignment_scan virt-alignment-scan _virt_cat () { - _guestfs_virttools "$(virt-cat --long-options)" 1 + _guestfs_virttools "virt-cat" 1 } && complete -o default -F _virt_cat virt-cat _virt_df () { - _guestfs_virttools "$(virt-df --long-options)" 1 + _guestfs_virttools "virt-df" 1 } && complete -o default -F _virt_df virt-df _virt_edit () { - _guestfs_virttools "$(virt-edit --long-options)" 0 + _guestfs_virttools "virt-edit" 0 } && complete -o default -F _virt_edit virt-edit _virt_filesystems () { - _guestfs_virttools "$(virt-filesystems --long-options)" 1 + _guestfs_virttools "virt-filesystems" 1 } && complete -o default -F _virt_filesystems virt-filesystems _virt_format () { - _guestfs_virttools "$(virt-format --long-options)" 0 + _guestfs_virttools "virt-format" 0 } && complete -o default -F _virt_format virt-format _virt_inspector () { - _guestfs_virttools "$(virt-inspector --long-options)" 1 + _guestfs_virttools "virt-inspector" 1 } && complete -o default -F _virt_inspector virt-inspector _virt_ls () { - _guestfs_virttools "$(virt-ls --long-options)" 1 + _guestfs_virttools "virt-ls" 1 } && complete -o default -F _virt_ls virt-ls _virt_sysprep () { - _guestfs_virttools "$(virt-sysprep --long-options)" 0 + _guestfs_virttools "virt-sysprep" 0 } && complete -o default -F _virt_sysprep virt-sysprep _virt_log () { - _guestfs_virttools "$(virt-log --long-options)" 1 + _guestfs_virttools "virt-log" 1 } && complete -o default -F _virt_log virt-log diff --git a/bash/virt-rescue b/bash/virt-rescue index 7987bb3..bb9ca6d 100644 --- a/bash/virt-rescue +++ b/bash/virt-rescue @@ -35,8 +35,6 @@ _virt_rescue () _init_completion -s || return - longopts="$(virt-rescue --long-options)" - # See if user has specified certain options anywhere on the # command line before the current word. while [ $c -lt $COMP_CWORD ]; do @@ -57,6 +55,7 @@ _virt_rescue () case "$cur" in --*) # --options + longopts="$(virt-rescue --long-options)" COMPREPLY=( $(compgen -W "$longopts" -- "$cur") ) return ;; *) diff --git a/bash/virt-resize b/bash/virt-resize index 39ef982..d5ac2f5 100644 --- a/bash/virt-resize +++ b/bash/virt-resize @@ -19,13 +19,14 @@ _guestfs_options_only () { local cur prev words cword split - local longopts="$1" + local longopts tool="$1" _init_completion -s || return case "$cur" in --*) # --options + longopts="$($tool --long-options)" COMPREPLY=( $(compgen -W "$longopts" -- "$cur") ) return ;; *) @@ -36,18 +37,18 @@ _guestfs_options_only () _virt_builder () { - _guestfs_options_only "$(virt-builder --long-options)" + _guestfs_options_only "virt-builder" } && complete -o default -F _virt_builder virt-builder _virt_resize () { - _guestfs_options_only "$(virt-resize --long-options)" + _guestfs_options_only "virt-resize" } && complete -o default -F _virt_resize virt-resize _virt_sparsify () { - _guestfs_options_only "$(virt-sparsify --long-options)" + _guestfs_options_only "virt-sparsify" } && complete -o default -F _virt_sparsify virt-sparsify -- 1.9.3
Propose both short and long options when trying to complete a single dash ("-"). --- bash/guestfish | 8 +++++++- bash/guestmount | 8 +++++++- bash/virt-alignment-scan | 8 +++++++- bash/virt-rescue | 8 +++++++- bash/virt-resize | 8 +++++++- 5 files changed, 35 insertions(+), 5 deletions(-) diff --git a/bash/guestfish b/bash/guestfish index 46d7847..eaec40f 100644 --- a/bash/guestfish +++ b/bash/guestfish @@ -31,7 +31,7 @@ _guestfs_virsh_list () _guestfish () { local cur prev words cword split - local longopts flag_ro=0 c=1 word cmds doms + local shortopts longopts flag_ro=0 c=1 word cmds doms _init_completion -s || return @@ -61,6 +61,12 @@ _guestfish () longopts="$(guestfish --long-options)" COMPREPLY=( $(compgen -W "$longopts" -- "$cur") ) return ;; + -*) + # -o and --options + shortopts="$(guestfish --short-options)" + longopts="$(guestfish --long-options)" + COMPREPLY=( $(compgen -W "$shortopts $longopts" -- "$cur") ) + return ;; *) # Might be a guestfish command. cmds=$(guestfish -h| head -n -1 | tail -n +2 | awk '{print $1}') diff --git a/bash/guestmount b/bash/guestmount index db79521..1bdb656 100644 --- a/bash/guestmount +++ b/bash/guestmount @@ -31,7 +31,7 @@ _guestfs_virsh_list () _guestmount () { local cur prev words cword split - local longopts flag_ro=0 c=1 word doms + local shortopts longopts flag_ro=0 c=1 word doms _init_completion -s || return @@ -58,6 +58,12 @@ _guestmount () longopts="$(guestmount --long-options)" COMPREPLY=( $(compgen -W "$longopts" -- "$cur") ) return ;; + -*) + # -o and --options + shortopts="$(guestmount --short-options)" + longopts="$(guestmount --long-options)" + COMPREPLY=( $(compgen -W "$shortopts $longopts" -- "$cur") ) + return ;; *) COMPREPLY=( $(compgen "$cur") ) return ;; diff --git a/bash/virt-alignment-scan b/bash/virt-alignment-scan index cef8365..a2e337d 100644 --- a/bash/virt-alignment-scan +++ b/bash/virt-alignment-scan @@ -31,7 +31,7 @@ _guestfs_virsh_list () _guestfs_virttools () { local cur prev words cword split - local longopts tool="$1" flag_ro="$2" doms + local shortopts longopts tool="$1" flag_ro="$2" doms _init_completion -s || return @@ -48,6 +48,12 @@ _guestfs_virttools () longopts="$($tool --long-options)" COMPREPLY=( $(compgen -W "$longopts" -- "$cur") ) return ;; + -*) + # -o and --options + shortopts="$($tool --short-options)" + longopts="$($tool --long-options)" + COMPREPLY=( $(compgen -W "$shortopts $longopts" -- "$cur") ) + return ;; *) COMPREPLY=( $(compgen "$cur") ) return ;; diff --git a/bash/virt-rescue b/bash/virt-rescue index bb9ca6d..da22e3c 100644 --- a/bash/virt-rescue +++ b/bash/virt-rescue @@ -31,7 +31,7 @@ _guestfs_virsh_list () _virt_rescue () { local cur prev words cword split - local longopts flag_ro=0 c=1 word doms + local shortopts longopts flag_ro=0 c=1 word doms _init_completion -s || return @@ -58,6 +58,12 @@ _virt_rescue () longopts="$(virt-rescue --long-options)" COMPREPLY=( $(compgen -W "$longopts" -- "$cur") ) return ;; + -*) + # -o and --options + shortopts="$(virt-rescue --short-options)" + longopts="$(virt-rescue --long-options)" + COMPREPLY=( $(compgen -W "$shortopts $longopts" -- "$cur") ) + return ;; *) COMPREPLY=( $(compgen "$cur") ) return ;; diff --git a/bash/virt-resize b/bash/virt-resize index d5ac2f5..447f0cf 100644 --- a/bash/virt-resize +++ b/bash/virt-resize @@ -19,7 +19,7 @@ _guestfs_options_only () { local cur prev words cword split - local longopts tool="$1" + local shortopts longopts tool="$1" _init_completion -s || return @@ -29,6 +29,12 @@ _guestfs_options_only () longopts="$($tool --long-options)" COMPREPLY=( $(compgen -W "$longopts" -- "$cur") ) return ;; + -*) + # -o and --options + shortopts="$($tool --short-options)" + longopts="$($tool --long-options)" + COMPREPLY=( $(compgen -W "$shortopts $longopts" -- "$cur") ) + return ;; *) COMPREPLY=( $(compgen "$cur") ) return ;; -- 1.9.3
Pino Toscano
2014-Nov-26 12:06 UTC
[Libguestfs] [PATCH] bash: complete some params for resize, sparsify, and builder
Hardcode the list of fixed arguments for some of the parameters of these tools. --- bash/virt-resize | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/bash/virt-resize b/bash/virt-resize index 447f0cf..7c879e1 100644 --- a/bash/virt-resize +++ b/bash/virt-resize @@ -23,6 +23,21 @@ _guestfs_options_only () _init_completion -s || return + case "$prev" in + --align-first) + COMPREPLY=( $( compgen -W "never always auto" -- "$cur") ) + return ;; + --check-tmpdir) + COMPREPLY=( $( compgen -W "ignore i continue cont c warn warning w fail f error" -- "$cur") ) + return ;; + --list-format) + COMPREPLY=( $( compgen -W "short long json" -- "$cur") ) + return ;; + --password-crypto) + COMPREPLY=( $( compgen -W "md5 sha256 sha512" -- "$cur") ) + return ;; + esac + case "$cur" in --*) # --options -- 1.9.3
Pino Toscano
2014-Nov-26 12:06 UTC
[Libguestfs] [PATCH] bash: add a completion module for virt-v2v
--- bash/Makefile.am | 3 ++- bash/virt-v2v | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 bash/virt-v2v diff --git a/bash/Makefile.am b/bash/Makefile.am index ee9c10c..2c8e722 100644 --- a/bash/Makefile.am +++ b/bash/Makefile.am @@ -22,7 +22,8 @@ scripts = \ guestmount \ virt-alignment-scan \ virt-rescue \ - virt-resize + virt-resize \ + virt-v2v symlinks = \ virt-builder \ diff --git a/bash/virt-v2v b/bash/virt-v2v new file mode 100644 index 0000000..a70971b --- /dev/null +++ b/bash/virt-v2v @@ -0,0 +1,60 @@ +# virt-v2v bash completion script -*- shell-script -*- +# Copyright (C) 2014 Red Hat Inc. +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# 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 () +{ + local cur prev words cword split + local shortopts longopts items + + _init_completion -s || return + + case "$prev" in + -i) + items="$(virt-v2v --machine-readable | awk -F':' '/input:/ {print $2;}')" + COMPREPLY=( $( compgen -W "$items" -- "$cur") ) + return ;; + -o) + items="$(virt-v2v --machine-readable | awk -F':' '/output:/ {print $2;}')" + COMPREPLY=( $( compgen -W "$items" -- "$cur") ) + return ;; + -oa) + COMPREPLY=( $( compgen -W "sparse preallocated" -- "$cur") ) + return ;; + --vmtype) + COMPREPLY=( $( compgen -W "server desktop" -- "$cur") ) + return ;; + + esac + + case "$cur" in + --*) + # --options + longopts="$(virt-v2v --long-options)" + COMPREPLY=( $(compgen -W "$longopts" -- "$cur") ) + return ;; + -*) + # -o and --options + shortopts="$(virt-v2v --short-options)" + longopts="$(virt-v2v --long-options)" + COMPREPLY=( $(compgen -W "$shortopts $longopts" -- "$cur") ) + return ;; + *) + COMPREPLY=( $(compgen "$cur") ) + return ;; + esac +} && +complete -o default -F _virt_v2v virt-v2v -- 1.9.3
Richard W.M. Jones
2014-Nov-26 19:10 UTC
Re: [Libguestfs] [PATCH] tools: implement --short-options
ACK series. Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones Read my programming and virtualization blog: http://rwmj.wordpress.com virt-builder quickly builds VMs from scratch http://libguestfs.org/virt-builder.1.html
Possibly Parallel Threads
- [PATCH 0/5] bash completion: Add missing bash completion scripts (RHBZ#1367738).
- [PATCH 5/5] bash completion: Add missing bash completion scripts (RHBZ#1367738).
- [PATCH] bash: Implement tab completion for virt-win-reg (RHBZ#1367738).
- Re: [PATCH 5/5] bash completion: Add missing bash completion scripts (RHBZ#1367738).
- [PATCH 1/3] test-tool: implement --short-options & --long-options