2 * Copyright (C) 2021 Simon Marchi <simon.marchi@efficios.com>
4 * SPDX-License-Identifier: GPL-2.0-only
8 #ifndef COMMON_ARGPAR_UTILS_H
9 #define COMMON_ARGPAR_UTILS_H
17 #include <common/macros.h>
18 #include <common/argpar/argpar.h>
19 #include <common/string-utils/format.h>
21 #define WHILE_PARSING_ARG_N_ARG_FMT "While parsing argument #%d (`%s`): "
23 enum parse_next_item_status
25 PARSE_NEXT_ITEM_STATUS_OK
= 0,
26 PARSE_NEXT_ITEM_STATUS_END
= 1,
27 PARSE_NEXT_ITEM_STATUS_ERROR
= -1,
31 * Parse the next argpar item using `iter`.
33 * The item in `*item` is always freed and cleared on entry.
35 * If an item is parsed successfully, return the new item in `*item` and return
36 * PARSE_NEXT_ITEM_STATUS_OK.
38 * If the end of the argument list is reached, return
39 * PARSE_NEXT_ITEM_STATUS_END.
41 * On error, print a descriptive error message and return
42 * PARSE_NEXT_ITEM_STATUS_ERROR. If `context_fmt` is non-NULL, it is formatted
43 * using the following arguments and prepended to the error message.
44 * Add `argc_offset` to the argument index mentioned in the error message.
46 * If `unknown_opt_is_error` is true, an unknown option is considered an error.
47 * Otherwise, it is considered as the end of the argument list.
49 ATTR_FORMAT_PRINTF(6, 7)
50 enum parse_next_item_status
parse_next_item(struct argpar_iter
*iter
,
51 const struct argpar_item
**item
, int argc_offset
,
52 const char **argv
, bool unknown_opt_is_error
,
53 const char *context_fmt
, ...);