tools: improve reporting for option errors (RHBZ#1316041)

Improve the error messages produced by C-based tools in case of issues
with the command line options:
- explicitly mention to use -a/-d (and -A/-D in virt-diff)
- when extra arguments are found, mention the correct way to pass
  options to certain command line switches (like --format)
- in virt-inspector, give a cleaner error message when neither -i nor
  any -m is specified

In all the cases, keep the extra notice to use 'TOOL --help' to get more
help with it.
This commit is contained in:
Pino Toscano
2016-05-05 14:25:28 +02:00
parent cd689c9d3a
commit dc02e8985f
10 changed files with 87 additions and 22 deletions

View File

@@ -224,8 +224,11 @@ main (int argc, char *argv[])
CHECK_OPTION_format_consumed;
/* User must have specified some drives. */
if (drvs == NULL)
if (drvs == NULL) {
fprintf (stderr, _("%s: error: you must specify at least one -a or -d option.\n"),
guestfs_int_program_name);
usage (EXIT_FAILURE);
}
/* Add drives, inspect and mount. */
add_drives (drvs, 'a');

View File

@@ -291,8 +291,13 @@ main (int argc, char *argv[])
assert (live == 0);
/* Must be no extra arguments on the command line. */
if (optind != argc)
if (optind != argc) {
fprintf (stderr, _("%s: error: extra argument '%s' on command line.\n"
"Make sure to specify the argument for --format "
"like '--format=%s'.\n"),
guestfs_int_program_name, argv[optind], argv[optind]);
usage (EXIT_FAILURE);
}
CHECK_OPTION_format_consumed;
@@ -329,8 +334,11 @@ main (int argc, char *argv[])
title = 0;
/* User must have specified some drives. */
if (drvs == NULL)
if (drvs == NULL) {
fprintf (stderr, _("%s: error: you must specify at least one -a or -d option.\n"),
guestfs_int_program_name);
usage (EXIT_FAILURE);
}
/* Add drives. */
add_drives (drvs, 'a');

View File

@@ -184,14 +184,22 @@ main (int argc, char *argv[])
assert (live == 0);
/* User must not specify more arguments on the command line. */
if (optind != argc)
if (optind != argc) {
fprintf (stderr, _("%s: error: extra argument '%s' on command line.\n"
"Make sure to specify the argument for --format "
"like '--format=%s'.\n"),
guestfs_int_program_name, argv[optind], argv[optind]);
usage (EXIT_FAILURE);
}
CHECK_OPTION_format_consumed;
/* User must have specified some drives. */
if (drvs == NULL)
if (drvs == NULL) {
fprintf (stderr, _("%s: error: you must specify at least one -a or -d option.\n"),
guestfs_int_program_name);
usage (EXIT_FAILURE);
}
/* Add drives, inspect and mount. Note that inspector is always true,
* and there is no -m option.

View File

@@ -340,8 +340,11 @@ main (int argc, char *argv[])
usage (EXIT_FAILURE);
/* User must have specified some drives. */
if (drvs == NULL)
if (drvs == NULL) {
fprintf (stderr, _("%s: error: you must specify at least one -a or -d option.\n"),
guestfs_int_program_name);
usage (EXIT_FAILURE);
}
/* Add drives, inspect and mount. */
add_drives (drvs, 'a');

View File

@@ -311,10 +311,14 @@ main (int argc, char *argv[])
}
}
if (drvs == NULL || drvs2 == NULL) {
fprintf (stderr,
_("%s: you must specify some -a|-A|-d|-D options, see %s(1)\n"),
guestfs_int_program_name, guestfs_int_program_name);
if (drvs == NULL) {
fprintf (stderr, _("%s: error: you must specify at least one -a or -d option.\n"),
guestfs_int_program_name);
usage (EXIT_FAILURE);
}
if (drvs2 == NULL) {
fprintf (stderr, _("%s: error: you must specify at least one -A or -D option.\n"),
guestfs_int_program_name);
usage (EXIT_FAILURE);
}
@@ -324,8 +328,13 @@ main (int argc, char *argv[])
if (human && csv)
error (EXIT_FAILURE, 0, _("you cannot use -h and --csv options together."));
if (optind != argc)
error (EXIT_FAILURE, 0, _("extra arguments on the command line"));
if (optind != argc) {
fprintf (stderr, _("%s: error: extra argument '%s' on command line.\n"
"Make sure to specify the argument for --checksum or --format "
"like '--format=%s'.\n"),
guestfs_int_program_name, argv[optind], argv[optind]);
usage (EXIT_FAILURE);
}
/* These are really constants, but they have to be variables for the
* options parsing code. Assert here that they have known-good

View File

@@ -253,8 +253,11 @@ main (int argc, char *argv[])
CHECK_OPTION_format_consumed;
/* User must have specified some drives. */
if (drvs == NULL)
if (drvs == NULL) {
fprintf (stderr, _("%s: error: you must specify at least one -a or -d option.\n"),
guestfs_int_program_name);
usage (EXIT_FAILURE);
}
/* Add drives. */
add_drives (drvs, 'a');

View File

@@ -219,14 +219,22 @@ main (int argc, char *argv[])
assert (live == 0);
/* Must be no extra arguments on the command line. */
if (optind != argc)
if (optind != argc) {
fprintf (stderr, _("%s: error: extra argument '%s' on command line.\n"
"Make sure to specify the argument for --format, --lvm "
"or --partition like '--format=%s'.\n"),
guestfs_int_program_name, argv[optind], argv[optind]);
usage (EXIT_FAILURE);
}
CHECK_OPTION_format_consumed;
/* The user didn't specify any drives to format. */
if (drvs == NULL)
if (drvs == NULL) {
fprintf (stderr, _("%s: error: you must specify at least one -a option.\n"),
guestfs_int_program_name);
usage (EXIT_FAILURE);
}
/* Because the libguestfs kernel can get stuck rereading the
* partition table after things have been erased, we sometimes need

View File

@@ -311,9 +311,16 @@ main (int argc, char *argv[])
/* Check we have the right options. */
if (!live) {
if (!drvs || !(mps || inspector))
error (EXIT_FAILURE, 0,
_("must have at least one -a/-d and at least one -m/-i option"));
if (drvs == NULL) {
fprintf (stderr, _("%s: error: you must specify at least one -a or -d option.\n"),
guestfs_int_program_name);
usage (EXIT_FAILURE);
}
if (!(mps || inspector)) {
fprintf (stderr, _("%s: error: you must specify either -i at least one -m option.\n"),
guestfs_int_program_name);
usage (EXIT_FAILURE);
}
} else {
size_t count_d = 0, count_other = 0;
struct drv *drv;

View File

@@ -233,8 +233,13 @@ main (int argc, char *argv[])
assert (live == 0);
/* Must be no extra arguments on the command line. */
if (optind != argc)
if (optind != argc) {
fprintf (stderr, _("%s: error: extra argument '%s' on command line.\n"
"Make sure to specify the argument for --format "
"like '--format=%s'.\n"),
guestfs_int_program_name, argv[optind], argv[optind]);
usage (EXIT_FAILURE);
}
CHECK_OPTION_format_consumed;
@@ -252,8 +257,11 @@ main (int argc, char *argv[])
}
/* User must have specified some drives. */
if (drvs == NULL)
if (drvs == NULL) {
fprintf (stderr, _("%s: error: you must specify at least one -a or -d option.\n"),
guestfs_int_program_name);
usage (EXIT_FAILURE);
}
/* Add drives, inspect and mount. Note that inspector is always true,
* and there is no -m option.

View File

@@ -275,14 +275,22 @@ main (int argc, char *argv[])
assert (live == 0);
/* Must be no extra arguments on the command line. */
if (optind != argc)
if (optind != argc) {
fprintf (stderr, _("%s: error: extra argument '%s' on command line.\n"
"Make sure to specify the argument for --format or --scratch "
"like '--format=%s'.\n"),
guestfs_int_program_name, argv[optind], argv[optind]);
usage (EXIT_FAILURE);
}
CHECK_OPTION_format_consumed;
/* User must have specified some drives. */
if (drvs == NULL)
if (drvs == NULL) {
fprintf (stderr, _("%s: error: you must specify at least one -a or -d option.\n"),
guestfs_int_program_name);
usage (EXIT_FAILURE);
}
/* Setting "direct mode" is required for the rescue appliance. */
if (guestfs_set_direct (g, 1) == -1)