argpar.c: add error enumerators to `enum parse_orig_arg_opt_ret`
authorPhilippe Proulx <eeppeliteloop@gmail.com>
Tue, 1 Jun 2021 13:49:13 +0000 (09:49 -0400)
committerPhilippe Proulx <eeppeliteloop@gmail.com>
Thu, 3 Jun 2021 17:41:01 +0000 (13:41 -0400)
This patch changes `enum parse_orig_arg_opt_ret` to add

* `PARSE_ORIG_ARG_OPT_RET_ERROR_MISSING_OPT_ARG`
* `PARSE_ORIG_ARG_OPT_RET_ERROR_INVALID_OPT`
* `PARSE_ORIG_ARG_OPT_RET_ERROR_UNEXPECTED_OPT_ARG`
* `PARSE_ORIG_ARG_OPT_RET_ERROR_MEMORY`

and remove the general `PARSE_ORIG_ARG_OPT_RET_ERROR`.

No functional changes intended.

This will make it easier to switch to rich error reporting instead of
a formatted error string.

Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Change-Id: I2e68bea8b14ea1c0b16433df0cb8c01c1252283d

argpar/argpar.c

index a37efd000c9edf2edbbf65c5c02f2069e47ee151..72caa49dfd79c1272d47cb3a1279c766444d4abb 100644 (file)
@@ -398,8 +398,11 @@ end:
 
 enum parse_orig_arg_opt_ret {
        PARSE_ORIG_ARG_OPT_RET_OK,
-       PARSE_ORIG_ARG_OPT_RET_ERROR_UNKNOWN_OPT = -2,
-       PARSE_ORIG_ARG_OPT_RET_ERROR = -1,
+       PARSE_ORIG_ARG_OPT_RET_ERROR_UNKNOWN_OPT = -1,
+       PARSE_ORIG_ARG_OPT_RET_ERROR_MISSING_OPT_ARG = -2,
+       PARSE_ORIG_ARG_OPT_RET_ERROR_INVALID_OPT = -3,
+       PARSE_ORIG_ARG_OPT_RET_ERROR_UNEXPECTED_OPT_ARG = -4,
+       PARSE_ORIG_ARG_OPT_RET_ERROR_MEMORY = -5,
 };
 
 static
@@ -417,6 +420,7 @@ enum parse_orig_arg_opt_ret parse_short_opts(const char * const short_opts,
 
        if (strlen(short_opts) == 0) {
                try_append_string_printf(error, "Invalid argument");
+               ret = PARSE_ORIG_ARG_OPT_RET_ERROR_INVALID_OPT;
                goto error;
        }
 
@@ -427,9 +431,9 @@ enum parse_orig_arg_opt_ret parse_short_opts(const char * const short_opts,
        /* Find corresponding option descriptor */
        descr = find_descr(descrs, *iter->short_opt_ch, NULL);
        if (!descr) {
-               ret = PARSE_ORIG_ARG_OPT_RET_ERROR_UNKNOWN_OPT;
                try_append_string_printf(error, "Unknown option `-%c`",
                        *iter->short_opt_ch);
+               ret = PARSE_ORIG_ARG_OPT_RET_ERROR_UNKNOWN_OPT;
                goto error;
        }
 
@@ -453,6 +457,7 @@ enum parse_orig_arg_opt_ret parse_short_opts(const char * const short_opts,
                                "Missing required argument for option `-%c`",
                                *iter->short_opt_ch);
                        used_next_orig_arg = false;
+                       ret = PARSE_ORIG_ARG_OPT_RET_ERROR_MISSING_OPT_ARG;
                        goto error;
                }
        }
@@ -460,6 +465,7 @@ enum parse_orig_arg_opt_ret parse_short_opts(const char * const short_opts,
        /* Create and append option argument */
        opt_item = create_opt_item(descr, opt_arg);
        if (!opt_item) {
+               ret = PARSE_ORIG_ARG_OPT_RET_ERROR_MEMORY;
                goto error;
        }
 
@@ -480,9 +486,7 @@ enum parse_orig_arg_opt_ret parse_short_opts(const char * const short_opts,
        goto end;
 
 error:
-       if (ret == PARSE_ORIG_ARG_OPT_RET_OK) {
-               ret = PARSE_ORIG_ARG_OPT_RET_ERROR;
-       }
+       ARGPAR_ASSERT(ret != PARSE_ORIG_ARG_OPT_RET_OK);
 
 end:
        return ret;
@@ -515,6 +519,7 @@ enum parse_orig_arg_opt_ret parse_long_opt(const char * const long_opt_arg,
 
        if (strlen(long_opt_arg) == 0) {
                try_append_string_printf(error, "Invalid argument");
+               ret = PARSE_ORIG_ARG_OPT_RET_ERROR_INVALID_OPT;
                goto error;
        }
 
@@ -527,6 +532,7 @@ enum parse_orig_arg_opt_ret parse_long_opt(const char * const long_opt_arg,
                if (long_opt_name_size > max_len) {
                        try_append_string_printf(error,
                                "Invalid argument `--%s`", long_opt_arg);
+                       ret = PARSE_ORIG_ARG_OPT_RET_ERROR_INVALID_OPT;
                        goto error;
                }
 
@@ -555,6 +561,7 @@ enum parse_orig_arg_opt_ret parse_long_opt(const char * const long_opt_arg,
                                try_append_string_printf(error,
                                        "Missing required argument for option `--%s`",
                                        long_opt_name);
+                               ret = PARSE_ORIG_ARG_OPT_RET_ERROR_MISSING_OPT_ARG;
                                goto error;
                        }
 
@@ -568,6 +575,7 @@ enum parse_orig_arg_opt_ret parse_long_opt(const char * const long_opt_arg,
                 */
                try_append_string_printf(error,
                        "Unexpected argument for option `--%s`", long_opt_name);
+               ret = PARSE_ORIG_ARG_OPT_RET_ERROR_UNEXPECTED_OPT_ARG;
                goto error;
        }
 
@@ -587,9 +595,7 @@ enum parse_orig_arg_opt_ret parse_long_opt(const char * const long_opt_arg,
        goto end;
 
 error:
-       if (ret == PARSE_ORIG_ARG_OPT_RET_OK) {
-               ret = PARSE_ORIG_ARG_OPT_RET_ERROR;
-       }
+       ARGPAR_ASSERT(ret != PARSE_ORIG_ARG_OPT_RET_OK);
 
 end:
        return ret;
@@ -728,7 +734,10 @@ enum argpar_iter_parse_next_status argpar_iter_parse_next(
                        orig_arg);
                status = ARGPAR_ITER_PARSE_NEXT_STATUS_ERROR_UNKNOWN_OPT;
                break;
-       case PARSE_ORIG_ARG_OPT_RET_ERROR:
+       case PARSE_ORIG_ARG_OPT_RET_ERROR_MISSING_OPT_ARG:
+       case PARSE_ORIG_ARG_OPT_RET_ERROR_INVALID_OPT:
+       case PARSE_ORIG_ARG_OPT_RET_ERROR_UNEXPECTED_OPT_ARG:
+       case PARSE_ORIG_ARG_OPT_RET_ERROR_MEMORY:
                try_prepend_while_parsing_arg_to_error(error, iter->i,
                        orig_arg);
                status = ARGPAR_ITER_PARSE_NEXT_STATUS_ERROR;
This page took 0.024548 seconds and 4 git commands to generate.