Add --long-options option to most tools.

For example:

$ guestfish --long-options
--add
--cmd-help
--connect
--csh
--domain
--echo-keys
[etc.]

The idea of this is to make it easier to write a bash completion
script that accurately expands --<TAB> options for each command.
This commit is contained in:
Richard W.M. Jones
2013-03-28 13:07:22 +00:00
parent b98de580c9
commit 6c9a7fe561
17 changed files with 82 additions and 15 deletions

View File

@@ -117,6 +117,7 @@ main (int argc, char *argv[])
{ "domain", 1, 0, 'd' },
{ "format", 2, 0, 0 },
{ "help", 0, 0, HELP_OPTION },
{ "long-options", 0, 0, 0 },
{ "quiet", 0, 0, 'q' },
{ "uuid", 0, 0, 0, },
{ "verbose", 0, 0, 'v' },
@@ -145,7 +146,9 @@ main (int argc, char *argv[])
switch (c) {
case 0: /* options which are long only */
if (STREQ (long_options[option_index].name, "format")) {
if (STREQ (long_options[option_index].name, "long-options"))
display_long_options (long_options);
else if (STREQ (long_options[option_index].name, "format")) {
if (!optarg || STREQ (optarg, ""))
format = NULL;
else

View File

@@ -101,6 +101,7 @@ main (int argc, char *argv[])
{ "format", 2, 0, 0 },
{ "help", 0, 0, HELP_OPTION },
{ "keys-from-stdin", 0, 0, 0 },
{ "long-options", 0, 0, 0 },
{ "verbose", 0, 0, 'v' },
{ "version", 0, 0, 'V' },
{ 0, 0, 0, 0 }
@@ -126,7 +127,9 @@ main (int argc, char *argv[])
switch (c) {
case 0: /* options which are long only */
if (STREQ (long_options[option_index].name, "keys-from-stdin")) {
if (STREQ (long_options[option_index].name, "long-options"))
display_long_options (long_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")) {
echo_keys = 1;

View File

@@ -156,9 +156,10 @@ main (int argc, char *argv[])
{ "help", 0, 0, HELP_OPTION },
{ "human-readable", 0, 0, 'h' },
{ "keys-from-stdin", 0, 0, 0 },
{ "long", 0, 0, 'l' },
{ "logical-volumes", 0, 0, 0 },
{ "logvols", 0, 0, 0 },
{ "long", 0, 0, 'l' },
{ "long-options", 0, 0, 0 },
{ "lvs", 0, 0, 0 },
{ "no-title", 0, 0, 0 },
{ "parts", 0, 0, 0 },
@@ -199,7 +200,9 @@ main (int argc, char *argv[])
switch (c) {
case 0: /* options which are long only */
if (STREQ (long_options[option_index].name, "keys-from-stdin")) {
if (STREQ (long_options[option_index].name, "long-options"))
display_long_options (long_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")) {
echo_keys = 1;

View File

@@ -155,6 +155,7 @@ main (int argc, char *argv[])
{ "human-readable", 0, 0, 'h' },
{ "keys-from-stdin", 0, 0, 0 },
{ "long", 0, 0, 'l' },
{ "long-options", 0, 0, 0 },
{ "recursive", 0, 0, 'R' },
{ "time", 0, 0, 0 },
{ "times", 0, 0, 0 },
@@ -191,7 +192,9 @@ main (int argc, char *argv[])
switch (c) {
case 0: /* options which are long only */
if (STREQ (long_options[option_index].name, "keys-from-stdin")) {
if (STREQ (long_options[option_index].name, "long-options"))
display_long_options (long_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")) {
echo_keys = 1;

View File

@@ -117,6 +117,7 @@ main (int argc, char *argv[])
{ "help", 0, 0, HELP_OPTION },
{ "human-readable", 0, 0, 'h' },
{ "inodes", 0, 0, 'i' },
{ "long-options", 0, 0, 0 },
{ "one-per-guest", 0, 0, 0 },
{ "uuid", 0, 0, 0 },
{ "verbose", 0, 0, 'v' },
@@ -144,7 +145,9 @@ main (int argc, char *argv[])
switch (c) {
case 0: /* options which are long only */
if (STREQ (long_options[option_index].name, "format")) {
if (STREQ (long_options[option_index].name, "long-options"))
display_long_options (long_options);
else if (STREQ (long_options[option_index].name, "format")) {
if (!optarg || STREQ (optarg, ""))
format = NULL;
else

View File

@@ -122,6 +122,7 @@ main (int argc, char *argv[])
{ "format", 2, 0, 0 },
{ "help", 0, 0, HELP_OPTION },
{ "keys-from-stdin", 0, 0, 0 },
{ "long-options", 0, 0, 0 },
{ "verbose", 0, 0, 'v' },
{ "version", 0, 0, 'V' },
{ 0, 0, 0, 0 }
@@ -146,7 +147,9 @@ main (int argc, char *argv[])
switch (c) {
case 0: /* options which are long only */
if (STREQ (long_options[option_index].name, "keys-from-stdin")) {
if (STREQ (long_options[option_index].name, "long-options"))
display_long_options (long_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")) {
echo_keys = 1;

View File

@@ -191,6 +191,7 @@ main (int argc, char *argv[])
{ "keys-from-stdin", 0, 0, 0 },
{ "listen", 0, 0, 0 },
{ "live", 0, 0, 0 },
{ "long-options", 0, 0, 0 },
{ "mount", 1, 0, 'm' },
{ "network", 0, 0, 0 },
{ "new", 1, 0, 'N' },
@@ -250,7 +251,9 @@ main (int argc, char *argv[])
switch (c) {
case 0: /* options which are long only */
if (STREQ (long_options[option_index].name, "listen"))
if (STREQ (long_options[option_index].name, "long-options"))
display_long_options (long_options);
else if (STREQ (long_options[option_index].name, "listen"))
remote_control_listen = 1;
else if (STREQ (long_options[option_index].name, "remote")) {
if (optarg) {

View File

@@ -22,6 +22,7 @@
#include <stdlib.h>
#include <string.h>
#include <libintl.h>
#include <getopt.h>
#include "guestfs.h"
@@ -196,3 +197,17 @@ free_mps (struct mp *mp)
free (mp);
}
/* Implements the internal 'tool --long-options' flag, which just
* lists out the long options available. Used by bash completion.
*/
void
display_long_options (const struct option *long_options)
{
while (long_options->name) {
if (STRNEQ (long_options->name, "long-options"))
printf ("--%s\n", long_options->name);
long_options++;
}
exit (EXIT_SUCCESS);
}

View File

@@ -19,6 +19,8 @@
#ifndef OPTIONS_H
#define OPTIONS_H
#include <getopt.h>
#include "guestfs-internal-frontend.h"
/* Provided by guestfish or guestmount. */
@@ -100,6 +102,7 @@ extern char add_drives (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_long_options (const struct option *) __attribute__((noreturn));
/* in virt.c */
extern int add_libvirt_drives (const char *guest);

View File

@@ -112,6 +112,7 @@ main (int argc, char *argv[])
{ "filesystem", 1, 0, 0 },
{ "format", 2, 0, 0 },
{ "help", 0, 0, HELP_OPTION },
{ "long-options", 0, 0, 0 },
{ "lvm", 2, 0, 0 },
{ "partition", 2, 0, 0 },
{ "verbose", 0, 0, 'v' },
@@ -140,7 +141,9 @@ main (int argc, char *argv[])
switch (c) {
case 0: /* options which are long only */
if (STREQ (long_options[option_index].name, "format")) {
if (STREQ (long_options[option_index].name, "long-options"))
display_long_options (long_options);
else if (STREQ (long_options[option_index].name, "format")) {
if (!optarg || STREQ (optarg, ""))
format = NULL;
else

View File

@@ -161,6 +161,7 @@ main (int argc, char *argv[])
{ "inspector", 0, 0, 'i' },
{ "keys-from-stdin", 0, 0, 0 },
{ "live", 0, 0, 0 },
{ "long-options", 0, 0, 0 },
{ "mount", 1, 0, 'm' },
{ "no-fork", 0, 0, 0 },
{ "no-sync", 0, 0, 'n' },
@@ -216,7 +217,9 @@ main (int argc, char *argv[])
switch (c) {
case 0: /* options which are long only */
if (STREQ (long_options[option_index].name, "dir-cache-timeout"))
if (STREQ (long_options[option_index].name, "long-options"))
display_long_options (long_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"))
fuse_help ();

View File

@@ -114,6 +114,7 @@ main (int argc, char *argv[])
{ "format", 2, 0, 0 },
{ "help", 0, 0, HELP_OPTION },
{ "keys-from-stdin", 0, 0, 0 },
{ "long-options", 0, 0, 0 },
{ "verbose", 0, 0, 'v' },
{ "version", 0, 0, 'V' },
{ "xpath", 1, 0, 0 },
@@ -139,7 +140,9 @@ main (int argc, char *argv[])
switch (c) {
case 0: /* options which are long only */
if (STREQ (long_options[option_index].name, "keys-from-stdin")) {
if (STREQ (long_options[option_index].name, "long-options"))
display_long_options (long_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")) {
echo_keys = 1;

View File

@@ -110,6 +110,7 @@ main (int argc, char *argv[])
{ "domain", 1, 0, 'd' },
{ "format", 2, 0, 0 },
{ "help", 0, 0, HELP_OPTION },
{ "long-options", 0, 0, 0 },
{ "memsize", 1, 0, 'm' },
{ "network", 0, 0, 0 },
{ "ro", 0, 0, 'r' },
@@ -147,7 +148,9 @@ main (int argc, char *argv[])
switch (c) {
case 0: /* options which are long only */
if (STREQ (long_options[option_index].name, "selinux")) {
if (STREQ (long_options[option_index].name, "long-options"))
display_long_options (long_options);
else if (STREQ (long_options[option_index].name, "selinux")) {
if (guestfs_set_selinux (g, 1) == -1)
exit (EXIT_FAILURE);
} else if (STREQ (long_options[option_index].name, "append")) {

View File

@@ -246,3 +246,13 @@ 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'. *)
let long_options = ref ([] : (Arg.key * Arg.spec * Arg.doc) list)
let display_long_options () =
List.iter (
fun (arg, _, _) ->
if string_prefix arg "--" && arg <> "--long-options" then
printf "%s\n" arg
) !long_options;
exit 0

View File

@@ -1,5 +1,5 @@
(* virt-resize
* Copyright (C) 2010-2012 Red Hat Inc.
* Copyright (C) 2010-2013 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
@@ -91,6 +91,7 @@ let infile, outfile, align_first, alignment, copy_boot_loader,
"--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";
"--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" ^ " -\"-";
"--lvexpand", Arg.String (add lv_expands), s_"lv" ^ " -\"-";
@@ -109,6 +110,7 @@ let infile, outfile, align_first, alignment, copy_boot_loader,
"-V", Arg.Unit display_version, " " ^ s_"Display version and exit";
"--version", Arg.Unit display_version, " -\"-";
] in
long_options := argspec;
let disks = ref [] in
let anon_fun s = disks := s :: !disks in
let usage_msg =

View File

@@ -1,5 +1,5 @@
(* virt-sparsify
* Copyright (C) 2011-2012 Red Hat Inc.
* Copyright (C) 2011-2013 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
@@ -62,6 +62,7 @@ let indisk, outdisk, compress, convert, debug_gc,
"--debug-gc", Arg.Set debug_gc, " " ^ s_"Debug GC and memory allocations";
"--format", Arg.Set_string format, s_"format" ^ " " ^ s_"Format of input disk";
"--ignore", Arg.String (add ignores), s_"fs" ^ " " ^ s_"Ignore filesystem";
"--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";
"-q", Arg.Set quiet, " " ^ s_"Quiet output";
@@ -73,6 +74,7 @@ let indisk, outdisk, compress, convert, debug_gc,
"-x", Arg.Set trace, " " ^ s_"Enable tracing of libguestfs calls";
"--zero", Arg.String (add zeroes), s_"fs" ^ " " ^ s_"Zero filesystem";
] in
long_options := argspec;
let disks = ref [] in
let anon_fun s = disks := s :: !disks in
let usage_msg =

View File

@@ -1,5 +1,5 @@
(* virt-sysprep
* Copyright (C) 2012 Red Hat Inc.
* Copyright (C) 2012-2013 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
@@ -109,6 +109,7 @@ let debug_gc, operations, g, selinux_relabel, quiet =
"--enable", Arg.String set_enable, s_"operations" ^ " " ^ s_"Enable specific operations";
"--format", Arg.Set_string format, s_"format" ^ " " ^ s_"Set format (default: auto)";
"--list-operations", Arg.Unit list_operations, " " ^ s_"List supported operations";
"--long-options", Arg.Unit display_long_options, " " ^ s_"List long options";
"-q", Arg.Set quiet, " " ^ s_"Don't print log messages";
"--quiet", Arg.Set quiet, " " ^ s_"Don't print log messages";
"--selinux-relabel", Arg.Unit force_selinux_relabel, " " ^ s_"Force SELinux relabel";
@@ -123,6 +124,7 @@ let debug_gc, operations, g, selinux_relabel, quiet =
let args =
List.sort (fun (a,_,_) (b,_,_) -> compare_command_line_args a b) args in
let argspec = Arg.align args in
long_options := argspec;
let anon_fun _ = raise (Arg.Bad (s_"extra parameter on the command line")) in
let usage_msg =
sprintf (f_"\