Correctly report errors in command-line option values

Hrvoje Niksic <[email protected]> Thu, 05 May 2005 20:42:23 +0200
Newsgroups gmane.comp.web.wget.patches
Message-ID <[email protected]>
Wget 1.10-alpha3 shows slightly misleading messages for invalid values
of command-line options.  For example:

$ wget --limit-rate=10kwfejop
wget: limitrate: Invalid byte value `10kwfejop'

"limitrate" refers to the `.wgetrc' command equivalent to the
`--limit-rate' option, but that may not be apparent to the user,
especially if he is running Wget from a script.

Fortunately it turns out that this is trivial to fix.  The code
already passes a "command name" which is not used for anything except
for error messages.  All we have to do is make main.c propagate the
command name to the init.c code.  With this patch, the error looks
like this:

$ wget --limit-rate=10kwfejop
wget: --limit-rate: Invalid byte value `10kwfejop'


2005-05-05  Hrvoje Niksic  <[email protected]>

	* main.c (main): Propagate option name to setoptval.

	* init.c (setoptval): Accept another argument, OPTNAME.  Propagate
	that argument as the option name independently of the actual
	command, determined by command_by_name(com).

Index: src/init.c
===================================================================
RCS file: /pack/anoncvs/wget/src/init.c,v
retrieving revision 1.111
diff -u -r1.111 init.c
--- src/init.c	2005/05/05 18:20:17	1.111
+++ src/init.c	2005/05/05 18:39:28
@@ -627,10 +627,16 @@
    to accept COMIND directly.  */
 
 void
-setoptval (const char *com, const char *val)
+setoptval (const char *com, const char *val, const char *optname)
 {
+  /* Prepend "--" to OPTNAME. */
+  char *dd_optname = (char *) alloca (2 + strlen (optname) + 1);
+  dd_optname[0] = '-';
+  dd_optname[1] = '-';
+  strcpy (dd_optname + 2, optname);
+
   assert (val != NULL);
-  if (!setval_internal (command_by_name (com), com, val))
+  if (!setval_internal (command_by_name (com), dd_optname, val))
     exit (2);
 }
 
Index: src/init.h
===================================================================
RCS file: /pack/anoncvs/wget/src/init.h,v
retrieving revision 1.4
diff -u -r1.4 init.h
--- src/init.h	2003/09/21 00:41:49	1.4
+++ src/init.h	2005/05/05 18:39:28
@@ -32,7 +32,7 @@
 
 void initialize PARAMS ((void));
 void run_command PARAMS ((const char *));
-void setoptval PARAMS ((const char *, const char *));
+void setoptval PARAMS ((const char *, const char *, const char *));
 char *home_dir PARAMS ((void));
 void cleanup PARAMS ((void));
 
Index: src/main.c
===================================================================
RCS file: /pack/anoncvs/wget/src/main.c,v
retrieving revision 1.134
diff -u -r1.134 main.c
--- src/main.c	2005/04/27 22:08:28	1.134
+++ src/main.c	2005/05/05 18:39:30
@@ -721,17 +721,17 @@
       switch (opt->type)
 	{
 	case OPT_VALUE:
-	  setoptval (opt->data, optarg);
+	  setoptval (opt->data, optarg, opt->long_name);
 	  break;
 	case OPT_BOOLEAN:
 	  if (optarg)
 	    /* The user has specified a value -- use it. */
-	    setoptval (opt->data, optarg);
+	    setoptval (opt->data, optarg, opt->long_name);
 	  else
 	    {
 	      /* NEG is true for `--no-FOO' style boolean options. */
 	      int neg = val & BOOLEAN_NEG_MARKER;
-	      setoptval (opt->data, neg ? "0" : "1");
+	      setoptval (opt->data, neg ? "0" : "1", opt->long_name);
 	    }
 	  break;
 	case OPT_FUNCALL:
@@ -741,7 +741,7 @@
 	  }
 	  break;
 	case OPT__APPEND_OUTPUT:
-	  setoptval ("logfile", optarg);
+	  setoptval ("logfile", optarg, opt->long_name);
 	  append_to_log = 1;
 	  break;
 	case OPT__EXECUTE:
@@ -757,19 +757,19 @@
 	      switch (*p)
 		{
 		case 'v':
-		  setoptval ("verbose", "0");
+		  setoptval ("verbose", "0", opt->long_name);
 		  break;
 		case 'H':
-		  setoptval ("addhostdir", "0");
+		  setoptval ("addhostdir", "0", opt->long_name);
 		  break;
 		case 'd':
-		  setoptval ("dirstruct", "0");
+		  setoptval ("dirstruct", "0", opt->long_name);
 		  break;
 		case 'c':
-		  setoptval ("noclobber", "1");
+		  setoptval ("noclobber", "1", opt->long_name);
 		  break;
 		case 'p':
-		  setoptval ("noparent", "1");
+		  setoptval ("noparent", "1", opt->long_name);
 		  break;
 		default:
 		  printf (_("%s: illegal option -- `-n%c'\n"), exec_name, *p);
@@ -792,11 +792,11 @@
 		      || (TOLOWER (optarg[0]) == 'o'
 			  && TOLOWER (optarg[1]) == 'n'));
 	    setoptval (opt->type == OPT__PARENT ? "noparent" : "noclobber",
-		       flag ? "0" : "1");
+		       flag ? "0" : "1", opt->long_name);
 	    break;
 	  }
 	case OPT__DONT_REMOVE_LISTING:
-	  setoptval ("removelisting", "0");
+	  setoptval ("removelisting", "0", opt->long_name);
 	  break;
 	}