On 01/24/2016 10:17 PM, James Youngman wrote:
> * xargs/xargs.c (parse_num): Specify the value passed for the
> invalid argument to make it easier to debug things when bug
> reports don't specify the actual command line.
> ---
> xargs/xargs.c | 12 ++++++------
> 1 file changed, 6 insertions(+), 6 deletions(-)
>
> diff --git a/xargs/xargs.c b/xargs/xargs.c
> index 18393cd..fd9b313 100644
> --- a/xargs/xargs.c
> +++ b/xargs/xargs.c
> @@ -1610,15 +1610,15 @@ parse_num (char *str, int option, long int min, long
> int max, int fatal)
> val = strtol (str, &eptr, 10);
> if (eptr == str || *eptr)
> {
> - fprintf (stderr, _("%s: invalid number for -%c option\n"),
> - program_name, option);
> + fprintf (stderr, _("%s: invalid number \"%s\" for -%c option\n"),
> + program_name, str, option);
> usage (stderr);
> exit (EXIT_FAILURE);
> }
> else if (val < min)
> {
> - fprintf (stderr, _("%s: value for -%c option should be >= %ld\n"),
> - program_name, option, min);
> + fprintf (stderr, _("%s: value %s for -%c option should be >= %ld\n"),
> + program_name, str, option, min);
> if (fatal)
> {
> usage (stderr);
> @@ -1631,8 +1631,8 @@ parse_num (char *str, int option, long int min, long
> int max, int fatal)
> }
> else if (max >= 0 && val > max)
> {
> - fprintf (stderr, _("%s: value for -%c option should be <= %ld\n"),
> - program_name, option, max);
> + fprintf (stderr, _("%s: value %s for -%c option should be <= %ld\n"),
> + program_name, str, option, max);
> if (fatal)
> {
> usage (stderr);
>
Nice one!
Nevertheless, the user will maybe still miss that line,
because of the following big usage message.
What about something like the following - shorter ?
$ xargs/xargs -s ABC
xargs/xargs: invalid number "ABC" for -s option
Try 'xargs/xargs --help' for more information.
The attached patch is a draft to do this (on top of your patch).
Have a nice day,
Berny
>From 033b2dfb9eb12cd1d4d02953d5ed33d4a449521e Mon Sep 17 00:00:00 2001
From: Bernhard Voelker <[email protected]>
Date: Sun, 24 Jan 2016 23:01:32 +0100
Subject: [PATCH] xargs: emit a short hint to --help rather than full usage
* xargs/xargs.c (usage): Change argument from FILE* to int, now taking
the exit code. Unless called with EXIT_SUCCESS, just emit a short
hint to try again with the --help option rather than emitting the
full usage. Afterward, exit the program with the given status.
(parse_num, main): Update all callers, removing the now-obsolete
return or exit().
---
xargs/xargs.c | 45 ++++++++++++++++++++-------------------------
1 file changed, 20 insertions(+), 25 deletions(-)
diff --git a/xargs/xargs.c b/xargs/xargs.c
index fd9b313..ed7d5bc 100644
--- a/xargs/xargs.c
+++ b/xargs/xargs.c
@@ -223,7 +223,7 @@ static void wait_for_proc_all (void);
static void increment_proc_max (int);
static void decrement_proc_max (int);
static long parse_num (char *str, int option, long min, long max, int fatal);
-static void usage (FILE * stream);
+static void usage (int status);
static char
@@ -531,8 +531,7 @@ main (int argc, char **argv)
break;
case 'h':
- usage (stdout);
- return 0;
+ usage (EXIT_SUCCESS);
case 'I': /* POSIX */
case 'i': /* deprecated */
@@ -651,8 +650,7 @@ main (int argc, char **argv)
break;
default:
- usage (stderr);
- return 1;
+ usage (EXIT_FAILURE);
}
}
@@ -1612,7 +1610,7 @@ parse_num (char *str, int option, long int min, long int max, int fatal)
{
fprintf (stderr, _("%s: invalid number \"%s\" for -%c option\n"),
program_name, str, option);
- usage (stderr);
+ usage (EXIT_FAILURE);
exit (EXIT_FAILURE);
}
else if (val < min)
@@ -1620,40 +1618,36 @@ parse_num (char *str, int option, long int min, long int max, int fatal)
fprintf (stderr, _("%s: value %s for -%c option should be >= %ld\n"),
program_name, str, option, min);
if (fatal)
- {
- usage (stderr);
- exit (EXIT_FAILURE);
- }
- else
- {
- val = min;
- }
+ usage (EXIT_FAILURE);
+
+ val = min;
}
else if (max >= 0 && val > max)
{
fprintf (stderr, _("%s: value %s for -%c option should be <= %ld\n"),
program_name, str, option, max);
if (fatal)
- {
- usage (stderr);
- exit (EXIT_FAILURE);
- }
- else
- {
- val = max;
- }
+ usage (EXIT_FAILURE);
+
+ val = max;
}
return val;
}
static void
-usage (FILE *stream)
+usage (int status)
{
- fprintf (stream,
+ if (status != EXIT_SUCCESS)
+ {
+ fprintf (stderr, _("Try '%s --help' for more information.\n"), program_name);
+ exit (status);
+ }
+
+ fprintf (stdout,
_("Usage: %s [OPTION]... COMMAND [INITIAL-ARGS]...\n"),
program_name);
-#define HTL(t) fputs (t, stream);
+#define HTL(t) fputs (t, stdout);
HTL (_("Run COMMAND with arguments INITIAL-ARGS and more arguments read from input.\n"
"\n"));
@@ -1695,4 +1689,5 @@ usage (FILE *stream)
HTL (_(" --version output version information and exit\n"));
HTL (_("\n"
"Report bugs to <[email protected]>.\n"));
+ exit (status);
}
--
2.1.4