diff options
author | Amit Kucheria <amit.kucheria@linaro.org> | 2014-05-28 19:30:22 +0530 |
---|---|---|
committer | Amit Kucheria <amit.kucheria@linaro.org> | 2014-06-01 15:43:49 +0530 |
commit | 967a8a75a7e6637afc4a71f0e90c1256c7b7b95a (patch) | |
tree | 6ef64d586cf731d24fc1a0c029941493de9d5779 | |
parent | 2129191e8946c71e8b41cf8ecabf8cb8e0119676 (diff) |
Make getopt_long handling more robust
Adding ':' as first character of optstring to getopt_long allows separating
error message for invalid option and invalid option arguments
Signed-off-by: Amit Kucheria <amit.kucheria@linaro.org>
Acked-by: Daniel Lezcano <daniel.lezcano@linaro.org>
-rw-r--r-- | idlestat.c | 15 |
1 files changed, 10 insertions, 5 deletions
@@ -985,7 +985,7 @@ int getoptions(int argc, char *argv[], struct idledebug_options *options) int optindex = 0; - c = getopt_long(argc, argv, "dhi:mo:t:V", + c = getopt_long(argc, argv, ":dhi:mo:t:V", long_options, &optindex); if (c == -1) break; @@ -1014,11 +1014,16 @@ int getoptions(int argc, char *argv[], struct idledebug_options *options) version(argv[0]); exit(0); break; - case '?': - fprintf(stderr, "%s: Unknown option %c'.\n", + case 0: /* getopt_long() set a variable, just keep going */ + break; + case ':': /* missing option argument */ + fprintf(stderr, "%s: option `-%c' requires an argument\n", argv[0], optopt); - /* fall through */ + return -1; + case '?': /* invalid option */ default: + fprintf(stderr, "%s: Unknown option `-%c'.\n", + argv[0], optopt); return -1; } } @@ -1027,7 +1032,7 @@ int getoptions(int argc, char *argv[], struct idledebug_options *options) fprintf(stderr, "dump values must be a positive value\n"); if (NULL == options->filename) { - fprintf(stderr, "expected filename\n"); + fprintf(stderr, "expected -o <filename>\n"); return -1; } |