[Pkg-wmaker-commits] [wmix] 21/44: wmix: changed command line parsing code to report properly incorrect options
Doug Torrance
dtorrance-guest at moszumanska.debian.org
Fri Sep 29 10:40:13 UTC 2017
This is an automated email from the git hooks/post-receive script.
dtorrance-guest pushed a commit to branch upstream
in repository wmix.
commit 06fe47d32a8ccfec600740f8d0a7959325c73ea2
Author: Christophe CURIS <christophe.curis at free.fr>
Date: Sat Jun 7 21:21:49 2014 +0200
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 at free.fr>
---
config.c | 38 ++++++++++++++++++++++++++++++--------
1 file changed, 30 insertions(+), 8 deletions(-)
diff --git a/config.c b/config.c
index 42f9e6c..ffd2b60 100644
--- a/config.c
+++ b/config.c
@@ -71,13 +71,29 @@ 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);
+ config.display_name = strdup(optarg);
break;
case 'e':
@@ -89,9 +105,8 @@ void parse_cli_options(int argc, char **argv)
break;
case 'f':
- if (optarg != NULL)
- if (config.file != NULL)
- free(config.file);
+ if (config.file != NULL)
+ free(config.file);
config.file = strdup(optarg);
break;
@@ -101,8 +116,7 @@ void parse_cli_options(int argc, char **argv)
break;
case 'm':
- if (optarg != NULL)
- config.mixer_device = strdup(optarg);
+ config.mixer_device = strdup(optarg);
break;
case 'v':
@@ -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);
}
/*
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-wmaker/wmix.git
More information about the Pkg-wmaker-commits
mailing list