From fd5b37444a16e85606d56ab27ef0f84ddc20df78 Mon Sep 17 00:00:00 2001 From: katherine Date: Sun, 25 Mar 2018 00:59:34 -0700 Subject: add error-printing function --- doc/example.c | 34 +++------------------------------- 1 file changed, 3 insertions(+), 31 deletions(-) (limited to 'doc') diff --git a/doc/example.c b/doc/example.c index e62e73f..50d8173 100644 --- a/doc/example.c +++ b/doc/example.c @@ -35,38 +35,10 @@ int main(int argc, char **argv) result = simple_opt_parse(argc, argv, options); - /* handle errors */ - switch (result.result_type) { - case SIMPLE_OPT_RESULT_UNRECOGNISED_OPTION: - fprintf(stderr, "err: unrecognised option `%s`\n", - result.option_string); + /* catch any errors and print a default result */ + if (result.result_type != SIMPLE_OPT_RESULT_SUCCESS) { + simple_opt_print_error(stderr, argv[0], options, result); return 1; - - case SIMPLE_OPT_RESULT_BAD_ARG: - fprintf(stderr, "err: bad argument `%s` passed to option `%s`\n", - result.argument_string, result.option_string); - return 1; - - case SIMPLE_OPT_RESULT_MISSING_ARG: - fprintf(stderr, "err: argument expected for option `%s`\n", - result.option_string); - return 1; - - case SIMPLE_OPT_RESULT_OPT_ARG_TOO_LONG: - fprintf(stderr, "internal err: argument passed to option `%s` is too long\n", - result.option_string); - return 1; - - case SIMPLE_OPT_RESULT_TOO_MANY_ARGS: - fprintf(stderr, "internal err: too many cli arguments passed\n"); - return 1; - - case SIMPLE_OPT_RESULT_MALFORMED_OPTION_STRUCT: - fprintf(stderr, "internal err: malformed option struct\n"); - return 1; - - default: - break; } /* if the help flag was passed, print usage */ -- cgit v1.2.3