diff options
author | Bobby Bingham <koorogi@koorogi.info> | 2015-01-05 21:16:13 -0600 |
---|---|---|
committer | Bobby Bingham <koorogi@koorogi.info> | 2015-09-15 22:43:32 -0500 |
commit | 81fdb90e1f2770e35ec10ddd41888bb39162ca51 (patch) | |
tree | e0d57737f23210409ba24c3cd8dc8cbd272d3cf7 /src | |
parent | f39b2b31fc8a604d7a3972cd15473bc8cab28684 (diff) |
improve command line error reporting
Diffstat (limited to 'src')
-rw-r--r-- | src/satmkboot.c | 68 |
1 files changed, 38 insertions, 30 deletions
diff --git a/src/satmkboot.c b/src/satmkboot.c index 6bd9823..2b7f308 100644 --- a/src/satmkboot.c +++ b/src/satmkboot.c @@ -118,62 +118,70 @@ static void usage(const char *progname) fprintf(stderr, "\t%-*s%s\n", width, "-o output", "Output file"); } +#define ERR_UNKNOWN_OPTION 0 +#define ERR_MISSING_OPTION 1 +#define ERR_DUPLICATE_INPUT 2 +#define ERR_DUPLICATE_OUTPUT 3 + +static const char *errmsgs[] = { + "Unknown extra argument", + "Missing required option", + "Duplicate -i parameter", + "Duplicate -o parameter", +}; + +static int show_arg_error(const char *progname, int prev_errors, int error) +{ + if (!(prev_errors & 1 << error)) + fprintf(stderr, "%s: %s\n", progname, errmsgs[error]); + return prev_errors | 1 << error; +} + static int process_args(int argc, char **argv) { - int fail = 0, help = 0; + int errors = 0, fail = 0, help = 0; int opt; extern char *optarg; extern int optind, optopt; - - while ((opt = getopt(argc, argv, ":hi:o:")) != -1) { + while ((opt = getopt(argc, argv, "hi:o:")) != -1) { switch (opt) { case 'h': help = 1; break; case 'i': - if (ipfile) { - fprintf(stderr, "Duplicate -i parameter\n"); - fail = 1; - } - ipfile = optarg; + if (ipfile) + errors = show_arg_error(argv[0], errors, ERR_DUPLICATE_INPUT); + else + ipfile = optarg; break; case 'o': - if (outfile) { - fprintf(stderr, "Duplicate -o parameter\n"); - fail = 1; - } - outfile = optarg; + if (outfile) + errors = show_arg_error(argv[0], errors, ERR_DUPLICATE_OUTPUT); + else + outfile = optarg; break; - case ':': - fprintf(stderr, "Option '%c' missing required parameter\n", optopt); - fail = 1; - break; default: - fprintf(stderr, "Unknown option '%c'\n", opt); fail = 1; } } if (help) { usage(argv[0]); - } else { - if (argv[optind]) { - fprintf(stderr, "Unknown extra arguments\n"); - fail = 1; - } - if (!ipfile || !outfile) { - fprintf(stderr, "Missing required option\n"); - fail = 1; - } + } else if (!fail) { + if (argv[optind]) + errors = show_arg_error(argv[0], errors, ERR_UNKNOWN_OPTION); + if (!ipfile || !outfile) + errors = show_arg_error(argv[0], errors, ERR_MISSING_OPTION); + fail = errors; + } - if (fail) { - fprintf(stderr, "Use -h option for help on correct usage.\n"); - } + if (fail) { + fprintf(stderr, "\nUse -h option for help on correct usage.\n"); } return fail | help; |