wmix: changed command line parsing code to report properly incorrect options
The original code did not abort when an invalid option was provided or if there was non-options arguments on the command line. The new code will report all problems found and stop to have the user fix his command. It also remove some unnecessary pointer checks. Signed-off-by: Christophe CURIS <christophe.curis@free.fr>
This commit is contained in:
parent
676fc13cb2
commit
5b46b380e6
|
@ -71,12 +71,28 @@ void parse_cli_options(int argc, char **argv)
|
|||
{
|
||||
int opt;
|
||||
int count_exclude = 0;
|
||||
bool error_found;
|
||||
|
||||
opterr = 0; /* We take charge of printing the error message */
|
||||
config.verbose = false;
|
||||
while ((opt = getopt(argc, argv, "d:e:f:hm:v")) != EOF) {
|
||||
error_found = false;
|
||||
for (;;) {
|
||||
opt = getopt(argc, argv, ":d:e:f:hm:v");
|
||||
if (opt == -1)
|
||||
break;
|
||||
|
||||
switch (opt) {
|
||||
case '?':
|
||||
fprintf(stderr, "wmix:error: unknow option '-%c'\n", optopt);
|
||||
error_found = true;
|
||||
break;
|
||||
|
||||
case ':':
|
||||
fprintf(stderr, "wmix:error: missing argument for option '-%c'\n", optopt);
|
||||
error_found = true;
|
||||
break;
|
||||
|
||||
case 'd':
|
||||
if (optarg != NULL)
|
||||
config.display_name = strdup(optarg);
|
||||
break;
|
||||
|
||||
|
@ -89,7 +105,6 @@ void parse_cli_options(int argc, char **argv)
|
|||
break;
|
||||
|
||||
case 'f':
|
||||
if (optarg != NULL)
|
||||
if (config.file != NULL)
|
||||
free(config.file);
|
||||
config.file = strdup(optarg);
|
||||
|
@ -101,7 +116,6 @@ void parse_cli_options(int argc, char **argv)
|
|||
break;
|
||||
|
||||
case 'm':
|
||||
if (optarg != NULL)
|
||||
config.mixer_device = strdup(optarg);
|
||||
break;
|
||||
|
||||
|
@ -114,6 +128,14 @@ void parse_cli_options(int argc, char **argv)
|
|||
}
|
||||
}
|
||||
config.exclude_channel[count_exclude] = NULL;
|
||||
|
||||
if (optind < argc) {
|
||||
fprintf(stderr, "wmix:error: argument '%s' not understood\n", argv[optind]);
|
||||
error_found = true;
|
||||
}
|
||||
|
||||
if (error_found)
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
/*
|
||||
|
|
Loading…
Reference in a new issue