X-Git-Url: http://git.efficios.com/?p=argpar.git;a=blobdiff_plain;f=argpar%2Fargpar.h;h=67056c3aa9834d0af631c0cb3a358bef1bdc7a5b;hp=7a50f453d18d7a57c63ea744428ab38425af55e5;hb=f3ab5ca1c03d5ae622ac53b7c1da30ebd21db199;hpb=1c9a6bde7e12e1978e8e47764913a91b7407e9b5 diff --git a/argpar/argpar.h b/argpar/argpar.h index 7a50f45..67056c3 100644 --- a/argpar/argpar.h +++ b/argpar/argpar.h @@ -1,46 +1,125 @@ -#ifndef BABELTRACE_ARGPAR_H -#define BABELTRACE_ARGPAR_H +/* + * SPDX-License-Identifier: MIT + * + * Copyright (c) 2019-2021 Philippe Proulx + * Copyright (c) 2020-2021 Simon Marchi + */ + +#ifndef ARGPAR_ARGPAR_H +#define ARGPAR_ARGPAR_H + +#include /* - * Copyright 2019 Philippe Proulx + * argpar is a library which provides facilities for command-line + * argument parsing. + * + * Two APIs are available: + * + * Iterator API: + * Create a parsing iterator with argpar_iter_create(), then + * repeatedly call argpar_iter_next() to access the parsing results, + * until one of: + * + * * There are no more arguments. + * + * * The argument parser encounters an error (for example, an + * unknown option). + * + * * You need to stop. + * + * This API provides more parsing control than the next one. + * + * Single call API: + * Call argpar_parse(), which parses the arguments until one of: + * + * * There are no more arguments. + * + * * It encounters an argument parsing error. + * + * argpar_parse() returns a single array of parsing results. + * + * Both methods parse the arguments `argv` of which the count is `argc` + * using the sentinel-terminated (use `ARGPAR_OPT_DESCR_SENTINEL`) + * option descriptor array `descrs`. + * + * argpar considers ALL the elements of `argv`, including the first one, + * so that you would typically pass `argc - 1` and `&argv[1]` from what + * main() receives. + * + * The argpar parsers support: + * + * * Short options without an argument, possibly tied together: + * + * -f -auf -n + * + * * Short options with argument: + * + * -b 45 -f/mein/file -xyzhello + * + * * Long options without an argument: + * + * --five-guys --burger-king --pizza-hut --subway + * + * * Long options with arguments: + * + * --security enable --time=18.56 + * + * * Non-option arguments (anything else). * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: + * The argpar parsers don't accept `-` or `--` as arguments. The latter + * means "end of options" for many command-line tools, but this library + * is all about keeping the order of the arguments, so it doesn't mean + * much to put them at the end. This has the side effect that a + * non-option argument cannot have the form of an option, for example if + * you need to pass the exact relative path `--component`. In that case, + * you would need to pass `./--component`. There's no generic way to + * escape `-` as of this version. + * + * Both argpar_iter_create() and argpar_parse() accept duplicate options + * (they produce one item for each instance). * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. + * 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). * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. + * Both argpar_iter_create() and argpar_parse() produce the items in + * the same order that the arguments were parsed, including non-option + * arguments. This means, for example, that for: + * + * --hello --count=23 /path/to/file -ab --type file magie + * + * The produced items are, in this order: + * + * 1. Option item (`--hello`). + * 2. Option item (`--count` with argument `23`). + * 3. Non-option item (`/path/to/file`). + * 4. Option item (`-a`). + * 5. Option item (`-b`). + * 6. Option item (`--type` with argument `file`). + * 7. Non-option item (`magie`). */ -#include - /* Sentinel for an option descriptor array */ #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 https://gcc.gnu.org/wiki/Visibility + * 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 */ +struct argpar_iter; + /* Option descriptor */ struct argpar_opt_descr { /* Numeric ID for this option */ @@ -65,53 +144,69 @@ 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 + * finalize the whole structure. + */ struct argpar_item_array *items; /* Error string, or `NULL` if none */ @@ -122,63 +217,17 @@ struct argpar_parse_ret { }; /* - * Parses the arguments `argv` of which the count is `argc` using the - * sentinel-terminated (use `ARGPAR_OPT_DESCR_SENTINEL`) option - * descriptor array `descrs`. + * Parses arguments in `argv` until the end is reached or an error is + * encountered. * - * This function considers ALL the elements of `argv`, including the - * first one, so that you would typically pass `argc - 1` and - * `&argv[1]` from what main() receives. - * - * This argument parser supports: - * - * * Short options without an argument, possibly tied together: - * - * -f -auf -n - * - * * Short options with argument: - * - * -b 45 -f/mein/file -xyzhello - * - * * Long options without an argument: - * - * --five-guys --burger-king --pizza-hut --subway - * - * * Long options with arguments: - * - * --security enable --time=18.56 - * - * * Non-option arguments (anything else). - * - * This function does not accept `-` or `--` as arguments. The latter - * means "end of options" for many command-line tools, but this function - * is all about keeping the order of the arguments, so it does not mean - * much to put them at the end. This has the side effect that a - * non-option argument cannot have the form of an option, for example if - * you need to pass the exact relative path `--component`. In that case, - * you would need to pass `./--component`. There's no generic way to - * escape `-` for the moment. - * - * This function accepts duplicate options (the resulting array of items - * contains one entry for each instance). - * - * On success, this function returns an array of items - * (`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. - * - * The returned array contains the items in the same order that the - * arguments were parsed, including non-option arguments. This means, - * for example, that for - * - * --hello --meow=23 /path/to/file -b - * - * the function returns an array of four items: two options, one - * non-option, and one option. + * On success, this function returns an array of items (field `items` of + * `struct argpar_parse_ret`). * * In the returned structure, `ingested_orig_args` is the number of * ingested arguments within `argv` to produce the resulting array of - * items. If `fail_on_unknown_opt` is true, then on success + * items. + * + * If `fail_on_unknown_opt` is true, then on success * `ingested_orig_args` is equal to `argc`. Otherwise, * `ingested_orig_args` contains the number of original arguments until * an unknown _option_ occurs. For example, with @@ -209,11 +258,10 @@ struct argpar_parse_ret { * for example contains two ingested original arguments, but four * resulting items. * - * On failure, the returned structure's `items` member is `NULL`, and - * the `error` string member contains details about the error. + * On failure, the `items` member of the returned structure is `NULL`, + * and the `error` string member contains details about the error. * - * You can finalize the returned structure with - * argpar_parse_ret_fini(). + * Finalize the returned structure with argpar_parse_ret_fini(). */ ARGPAR_HIDDEN struct argpar_parse_ret argpar_parse(unsigned int argc, @@ -222,12 +270,114 @@ struct argpar_parse_ret argpar_parse(unsigned int argc, bool fail_on_unknown_opt); /* - * Finalizes what is returned by argpar_parse(). + * Finalizes what argpar_parse() returns. * - * It is safe to call argpar_parse() multiple times with the same - * structure. + * You may call argpar_parse() multiple times with the same structure. */ ARGPAR_HIDDEN void argpar_parse_ret_fini(struct argpar_parse_ret *ret); -#endif /* BABELTRACE_ARGPAR_H */ +/* + * Creates an argument parsing iterator. + * + * This function initializes the returned structure, but doesn't + * actually start parsing the arguments. + * + * `*argv` and `*descrs` must NOT change for the lifetime of the + * returned iterator (until you call argpar_iter_destroy()). + * + * 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, + const char * const *argv, + const struct argpar_opt_descr *descrs); + +/* + * Destroys `iter`, as returned by argpar_iter_create(). + */ +ARGPAR_HIDDEN +void argpar_iter_destroy(struct argpar_iter *iter); + +/* + * Return type of argpar_iter_next(). + */ +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 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_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. + * + * `ARGPAR_ITER_NEXT_STATUS_ERROR_MEMORY`: + * Memory error. + * + * * 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_next_status argpar_iter_next( + struct argpar_iter *iter, const struct argpar_item **item, + char **error); + +/* + * Returns the number of ingested elements from `argv`, as passed to + * argpar_iter_create() to create `*iter`, that were required to produce + * the previously returned items. + */ +ARGPAR_HIDDEN +unsigned int argpar_iter_ingested_orig_args(const struct argpar_iter *iter); + +/* + * Destroys `item`, as created by argpar_iter_next(). + */ +ARGPAR_HIDDEN +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; \ + } + +#endif /* ARGPAR_ARGPAR_H */