X-Git-Url: http://git.efficios.com/?p=argpar.git;a=blobdiff_plain;f=argpar%2Fargpar.h;h=67056c3aa9834d0af631c0cb3a358bef1bdc7a5b;hp=45dd84ed9d493d0bfb5208bfada2a7ed25750938;hb=f3ab5ca1c03d5ae622ac53b7c1da30ebd21db199;hpb=fc07e5264804963a3de6d1b929a9eb4fd37e49d4 diff --git a/argpar/argpar.h b/argpar/argpar.h index 45dd84e..67056c3 100644 --- a/argpar/argpar.h +++ b/argpar/argpar.h @@ -5,8 +5,8 @@ * Copyright (c) 2020-2021 Simon Marchi */ -#ifndef BABELTRACE_ARGPAR_H -#define BABELTRACE_ARGPAR_H +#ifndef ARGPAR_ARGPAR_H +#define ARGPAR_ARGPAR_H #include @@ -18,8 +18,8 @@ * * Iterator API: * Create a parsing iterator with argpar_iter_create(), then - * repeatedly call argpar_iter_parse_next() to access the parsing - * results, until one of: + * repeatedly call argpar_iter_next() to access the parsing results, + * until one of: * * * There are no more arguments. * @@ -79,10 +79,10 @@ * Both argpar_iter_create() and argpar_parse() accept duplicate options * (they produce one item for each instance). * - * A returned parsing item has the type `struct argpar_item *`. Each - * item is to be casted to the appropriate type - * (`struct argpar_item_opt *` or `struct argpar_item_non_opt *`) - * depending on its `type` member. + * A returned parsing item has the type `const struct argpar_item *`. + * Get the type (option or non-option) of an item with + * argpar_item_type(). Each item type has its set of dedicated methods + * (`argpar_item_opt_` and `argpar_item_non_opt_` prefixes). * * Both argpar_iter_create() and argpar_parse() produce the items in * the same order that the arguments were parsed, including non-option @@ -105,16 +105,16 @@ #define ARGPAR_OPT_DESCR_SENTINEL { -1, '\0', NULL, false } /* - * ARGPAR_HIDDEN: if argpar is used in some shared library, we don't - * want them to be exported by that library, so mark them as "hidden". + * If argpar is used in some shared library, we don't want said library + * to export its symbols, so mark them as "hidden". * * On Windows, symbols are local unless explicitly exported; see * . */ #if defined(_WIN32) || defined(__CYGWIN__) -#define ARGPAR_HIDDEN +# define ARGPAR_HIDDEN #else -#define ARGPAR_HIDDEN __attribute__((visibility("hidden"))) +# define ARGPAR_HIDDEN __attribute__((visibility("hidden"))) #endif /* Forward-declaration for the opaque type */ @@ -144,54 +144,64 @@ enum argpar_item_type { ARGPAR_ITEM_TYPE_NON_OPT, }; -/* Base item */ -struct argpar_item { - enum argpar_item_type type; -}; - -/* Option item */ -struct argpar_item_opt { - struct argpar_item base; +/* Parsing item, as created by argpar_parse() and argpar_iter_next() */ +struct argpar_item; - /* Corresponding descriptor */ - const struct argpar_opt_descr *descr; +/* + * Returns the type of the parsing item `item`. + */ +ARGPAR_HIDDEN +enum argpar_item_type argpar_item_type(const struct argpar_item *item); - /* Argument, or `NULL` if none */ - const char *arg; -}; +/* + * Returns the option descriptor of the option parsing item `item`. + */ +ARGPAR_HIDDEN +const struct argpar_opt_descr *argpar_item_opt_descr( + const struct argpar_item *item); -/* Non-option item */ -struct argpar_item_non_opt { - struct argpar_item base; +/* + * Returns the argument of the option parsing item `item`, or `NULL` if + * none. + */ +ARGPAR_HIDDEN +const char *argpar_item_opt_arg(const struct argpar_item *item); - /* - * Complete argument, pointing to one of the entries of the - * original arguments (`argv`). - */ - const char *arg; +/* + * Returns the complete argument, pointing to one of the entries of the + * original arguments (`argv`), of the non-option parsing item `item`. + */ +ARGPAR_HIDDEN +const char *argpar_item_non_opt_arg(const struct argpar_item *item); - /* Index of this argument amongst all original arguments (`argv`) */ - unsigned int orig_index; +/* + * Returns the original index, within ALL the original arguments + * (`argv`), of the non-option parsing item `item`. + */ +ARGPAR_HIDDEN +unsigned int argpar_item_non_opt_orig_index(const struct argpar_item *item); - /* Index of this argument amongst other non-option arguments */ - unsigned int non_opt_index; -}; +/* + * Returns the index, within the non-option arguments, of the non-option + * parsing item `item`. + */ +ARGPAR_HIDDEN +unsigned int argpar_item_non_opt_non_opt_index(const struct argpar_item *item); struct argpar_item_array { - /* Array of `struct argpar_item *`, or `NULL` on error */ - struct argpar_item **items; + const struct argpar_item **items; - /* Number of used slots in `items`. */ + /* Number of used slots in `items` */ unsigned int n_items; - /* Number of allocated slots in `items`. */ + /* Number of allocated slots in `items` */ unsigned int n_alloc; }; /* What is returned by argpar_parse() */ struct argpar_parse_ret { /* - * Array of `struct argpar_item *`, or `NULL` on error. + * Array of parsing items, or `NULL` on error. * * Do NOT destroy those items manually with * argpar_iter_destroy(): call argpar_parse_ret_fini() to @@ -276,8 +286,8 @@ void argpar_parse_ret_fini(struct argpar_parse_ret *ret); * `*argv` and `*descrs` must NOT change for the lifetime of the * returned iterator (until you call argpar_iter_destroy()). * - * Call argpar_iter_parse_next() with the returned iterator to obtain - * the next parsing result (item). + * Call argpar_iter_next() with the returned iterator to obtain the next + * parsing result (item). */ ARGPAR_HIDDEN struct argpar_iter *argpar_iter_create(unsigned int argc, @@ -291,34 +301,58 @@ ARGPAR_HIDDEN void argpar_iter_destroy(struct argpar_iter *iter); /* - * Return type of argpar_iter_parse_next(). + * Return type of argpar_iter_next(). */ -enum argpar_iter_parse_next_status { - ARGPAR_ITER_PARSE_NEXT_STATUS_OK, - ARGPAR_ITER_PARSE_NEXT_STATUS_END, - ARGPAR_ITER_PARSE_NEXT_STATUS_ERROR_UNKNOWN_OPT, - ARGPAR_ITER_PARSE_NEXT_STATUS_ERROR, +enum argpar_iter_next_status { + ARGPAR_ITER_NEXT_STATUS_OK, + ARGPAR_ITER_NEXT_STATUS_END, + ARGPAR_ITER_NEXT_STATUS_ERROR_UNKNOWN_OPT, + ARGPAR_ITER_NEXT_STATUS_ERROR_MISSING_OPT_ARG, + ARGPAR_ITER_NEXT_STATUS_ERROR_INVALID_ARG, + ARGPAR_ITER_NEXT_STATUS_ERROR_UNEXPECTED_OPT_ARG, + ARGPAR_ITER_NEXT_STATUS_ERROR_MEMORY, }; /* * Parses and returns the next item from `iter`. * - * On success, this function sets `*item` to an item which describes the - * next option or non-option argument and returns - * `ARGPAR_ITER_PARSE_NEXT_STATUS_OK`. Destroy `*item` with - * argpar_item_destroy(). + * On success, this function: + * + * * Sets `*item` to a parsing item which describes the next option + * or non-option argument. + * + * Destroy `*item` with argpar_item_destroy(). + * + * * Returns `ARGPAR_ITER_NEXT_STATUS_OK`. * * If there are no more items to return, this function returns - * `ARGPAR_ITER_PARSE_NEXT_STATUS_END`. + * `ARGPAR_ITER_NEXT_STATUS_END`. + * + * On failure, this function: + * + * * Returns one of: + * + * `ARGPAR_ITER_NEXT_STATUS_ERROR_UNKNOWN_OPT`: + * Unknown option (not found in `descrs` as passed to + * argpar_iter_create() to create `iter`). + * + * `ARGPAR_ITER_NEXT_STATUS_ERROR_MISSING_OPT_ARG`: + * Missing option argument. + * + * `ARGPAR_ITER_NEXT_STATUS_ERROR_INVALID_ARG`: + * Invalid argument. + * + * `ARGPAR_ITER_NEXT_STATUS_ERROR_UNEXPECTED_OPT_ARG`: + * Unexpected option argument. * - * On failure (status codes - * `ARGPAR_ITER_PARSE_NEXT_STATUS_ERROR_UNKNOWN_OPT` and - * `ARGPAR_ITER_PARSE_NEXT_STATUS_ERROR`), this function sets `*error` - * to a descriptive error string. Free `*error` with free(). + * `ARGPAR_ITER_NEXT_STATUS_ERROR_MEMORY`: + * Memory error. * - * Create an argument parsing iterator with argpar_iter_create(). + * * Except for the `ARGPAR_ITER_NEXT_STATUS_ERROR_MEMORY` status, + * sets `*error`, if not `NULL`, to a descriptive error string. + * Free `*error` with free(). */ -enum argpar_iter_parse_next_status argpar_iter_parse_next( +enum argpar_iter_next_status argpar_iter_next( struct argpar_iter *iter, const struct argpar_item **item, char **error); @@ -328,10 +362,10 @@ enum argpar_iter_parse_next_status argpar_iter_parse_next( * the previously returned items. */ ARGPAR_HIDDEN -unsigned int argpar_iter_get_ingested_orig_args(const struct argpar_iter *iter); +unsigned int argpar_iter_ingested_orig_args(const struct argpar_iter *iter); /* - * Destroys `item`, as created by argpar_iter_parse_next(). + * Destroys `item`, as created by argpar_iter_next(). */ ARGPAR_HIDDEN void argpar_item_destroy(const struct argpar_item *item); @@ -340,10 +374,10 @@ void argpar_item_destroy(const struct argpar_item *item); * Destroys `_item` (`const struct argpar_item *`) and sets it to * `NULL`. */ -#define ARGPAR_ITEM_DESTROY_AND_RESET(_item) \ - { \ - argpar_item_destroy(_item); \ - _item = NULL; \ +#define ARGPAR_ITEM_DESTROY_AND_RESET(_item) \ + { \ + argpar_item_destroy(_item); \ + _item = NULL; \ } -#endif /* BABELTRACE_ARGPAR_H */ +#endif /* ARGPAR_ARGPAR_H */