improve error message on bad cli argument (#371)

currently the error messages have the shortopt hardcoded in them, even
when the user actually entered a longopt:

	$ ./nsxiv --framerate v
	nsxiv: Invalid argument for option -A: v

and as far as I see, there's no way to *reliably* tell weather we're
processing a longopt or a shortopt. perhaps we can do some shenanigangs
with `optind` but that seems finicky at best.

and it seems like other coreutils which support longopt has similar issues:

	$ xargs --max-procs=z
	xargs: invalid number "z" for -P option

utils like `grep` and `head` seems to work-around it by not mentioning the flag:

	$ head --lines=z
	head: invalid number of lines: ‘z’
	$ grep --max-count=l
	grep: invalid max count

this patch does the same thing as `grep/head` and omits the flag from
the error message.

Closes: https://codeberg.org/nsxiv/nsxiv/issues/368

Reviewed-on: https://codeberg.org/nsxiv/nsxiv/pulls/371
Reviewed-by: Berke Kocaoğlu <berke.kocaoglu@metu.edu.tr>
This commit is contained in:
NRK 2022-09-28 18:15:09 +02:00
parent daee34477a
commit 85fffbb277

View File

@ -150,7 +150,7 @@ void parse_options(int argc, char **argv)
case 'A': case 'A':
n = strtol(op.optarg, &end, 0); n = strtol(op.optarg, &end, 0);
if (*end != '\0' || n <= 0 || n > INT_MAX) if (*end != '\0' || n <= 0 || n > INT_MAX)
error(EXIT_FAILURE, 0, "Invalid argument for option -A: %s", op.optarg); error(EXIT_FAILURE, 0, "Invalid framerate: %s", op.optarg);
_options.framerate = n; _options.framerate = n;
/* fall through */ /* fall through */
case 'a': case 'a':
@ -165,7 +165,7 @@ void parse_options(int argc, char **argv)
case 'e': case 'e':
n = strtol(op.optarg, &end, 0); n = strtol(op.optarg, &end, 0);
if (*end != '\0') if (*end != '\0')
error(EXIT_FAILURE, 0, "Invalid argument for option -e: %s", op.optarg); error(EXIT_FAILURE, 0, "Invalid window id: %s", op.optarg);
_options.embed = n; _options.embed = n;
break; break;
case 'f': case 'f':
@ -174,7 +174,7 @@ void parse_options(int argc, char **argv)
case 'G': case 'G':
n = strtol(op.optarg, &end, 0); n = strtol(op.optarg, &end, 0);
if (*end != '\0' || n < INT_MIN || n > INT_MAX) if (*end != '\0' || n < INT_MIN || n > INT_MAX)
error(EXIT_FAILURE, 0, "Invalid argument for option -G: %s", op.optarg); error(EXIT_FAILURE, 0, "Invalid gamma: %s", op.optarg);
_options.gamma = n; _options.gamma = n;
break; break;
case 'g': case 'g':
@ -189,7 +189,7 @@ void parse_options(int argc, char **argv)
case 'n': case 'n':
n = strtol(op.optarg, &end, 0); n = strtol(op.optarg, &end, 0);
if (*end != '\0' || n <= 0 || n > INT_MAX) if (*end != '\0' || n <= 0 || n > INT_MAX)
error(EXIT_FAILURE, 0, "Invalid argument for option -n: %s", op.optarg); error(EXIT_FAILURE, 0, "Invalid starting number: %s", op.optarg);
_options.startnum = n - 1; _options.startnum = n - 1;
break; break;
case 'N': case 'N':
@ -210,13 +210,13 @@ void parse_options(int argc, char **argv)
case 'S': case 'S':
f = strtof(op.optarg, &end) * 10.0f; f = strtof(op.optarg, &end) * 10.0f;
if (*end != '\0' || f <= 0 || f >= (float)UINT_MAX) if (*end != '\0' || f <= 0 || f >= (float)UINT_MAX)
error(EXIT_FAILURE, 0, "Invalid argument for option -S: %s", op.optarg); error(EXIT_FAILURE, 0, "Invalid slideshow delay: %s", op.optarg);
_options.slideshow = (unsigned int)f; _options.slideshow = (unsigned int)f;
break; break;
case 's': case 's':
s = strchr(scalemodes, op.optarg[0]); s = strchr(scalemodes, op.optarg[0]);
if (s == NULL || *s == '\0' || strlen(op.optarg) != 1) if (s == NULL || *s == '\0' || strlen(op.optarg) != 1)
error(EXIT_FAILURE, 0, "Invalid argument for option -s: %s", op.optarg); error(EXIT_FAILURE, 0, "Invalid scale mode: %s", op.optarg);
_options.scalemode = s - scalemodes; _options.scalemode = s - scalemodes;
break; break;
case 'T': case 'T':
@ -235,7 +235,7 @@ void parse_options(int argc, char **argv)
case 'z': case 'z':
n = strtol(op.optarg, &end, 0); n = strtol(op.optarg, &end, 0);
if (*end != '\0' || n <= 0) if (*end != '\0' || n <= 0)
error(EXIT_FAILURE, 0, "Invalid argument for option -z: %s", op.optarg); error(EXIT_FAILURE, 0, "Invalid zoom level: %s", op.optarg);
_options.scalemode = SCALE_ZOOM; _options.scalemode = SCALE_ZOOM;
_options.zoom = (float) n / 100.0f; _options.zoom = (float) n / 100.0f;
break; break;