Encode error type in argpar_error
authorSimon Marchi <simon.marchi@efficios.com>
Tue, 24 Aug 2021 20:31:19 +0000 (16:31 -0400)
committerSimon Marchi <simon.marchi@efficios.com>
Wed, 25 Aug 2021 03:25:05 +0000 (23:25 -0400)
commit10aefab2119c89f4afa4f46a619eae20d6be7512
tree1df09f76c750cc3d5daa8797778dfd748fffe267
parent0a024790c9bd035f94db7919cafc0ffd051452f3
Encode error type in argpar_error

With the current API, when encountering an argument parsing error, the
error type (unknown option, missing option argument, unexpected option
argument) is encoded in the returned argpar_iter_next_status value.
In my experience it would be more useful if that information was encoded
in the argpar_error instead.

Imagine a helper function `format_arg_error` that formats an argument
parsing error based on a particular project's needs.  That function
needs to take the argpar_iter_next_status along with the argpar_error:

    char *format_arg_error(enum argpar_iter_next_status status,
    const struct argpar_error *error);

It would be simpler to have the argpar_error object know what type of
error was returned.  That makes the argpar_error contain all the
necessary information about the error.

So, the proposed changes are:

 - Replace ARGPAR_ITER_NEXT_STATUS_ERROR_* with a single
   ARGPAR_ITER_NEXT_STATUS_ERROR, indicating that an argument parsing
   error happened.
 - Add a new `enum argpar_error_type` type, to represent the 3 argument
   parsing error types.
 - Add a new `type` property to argpar_error, obtained through
   `argpar_error_get_type`.

Note that the argpar_error out parameter of the argpar_iter_next
function is optional.  This means that the user can currently receive an
error status (ARGPAR_ITER_NEXT_STATUS_ERROR_{UNKNOWN_OPT,
UNEXPECTED_OPT_ARG,MISSING_OP_ARG}) but not have any more details about
the error.  I don't think this is really useful, because the user of the
API wouldn't be able to do make a useful error message by knowing the
type of error but not having the corresponding argpar_error object.

API documentation left to Philippe Proulx.

Change-Id: Ice4db8852030ccd3ba9cbdf3717d9850b8ee7935
argpar/argpar.c
argpar/argpar.h
tests/test_argpar.c
This page took 0.023146 seconds and 4 git commands to generate.