| 1 | /* |
| 2 | * Copyright (C) 2021 Simon Marchi <simon.marchi@efficios.com> |
| 3 | * |
| 4 | * SPDX-License-Identifier: GPL-2.0-only |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | #ifndef COMMON_ARGPAR_UTILS_H |
| 9 | #define COMMON_ARGPAR_UTILS_H |
| 10 | |
| 11 | #ifdef __cplusplus |
| 12 | extern "C" { |
| 13 | #endif |
| 14 | |
| 15 | #include <stdarg.h> |
| 16 | |
| 17 | #include <common/macros.h> |
| 18 | #include <common/argpar/argpar.h> |
| 19 | #include <common/string-utils/format.h> |
| 20 | |
| 21 | enum parse_next_item_status |
| 22 | { |
| 23 | PARSE_NEXT_ITEM_STATUS_OK = 0, |
| 24 | PARSE_NEXT_ITEM_STATUS_END = 1, |
| 25 | PARSE_NEXT_ITEM_STATUS_ERROR = -1, |
| 26 | }; |
| 27 | |
| 28 | /* |
| 29 | * Parse the next argpar item using `iter`. |
| 30 | * |
| 31 | * The item in `*item` is always freed and cleared on entry. |
| 32 | * |
| 33 | * If an item is parsed successfully, return the new item in `*item` and return |
| 34 | * PARSE_NEXT_ITEM_STATUS_OK. |
| 35 | * |
| 36 | * If the end of the argument list is reached, return |
| 37 | * PARSE_NEXT_ITEM_STATUS_END. |
| 38 | * |
| 39 | * On error, print a descriptive error message and return |
| 40 | * PARSE_NEXT_ITEM_STATUS_ERROR. If `context_fmt` is non-NULL, it is formatted |
| 41 | * using the following arguments and prepended to the error message. |
| 42 | * |
| 43 | * If `unknown_opt_is_error` is true, an unknown option is considered an error. |
| 44 | * Otherwise, it is considered as the end of the argument list. |
| 45 | */ |
| 46 | ATTR_FORMAT_PRINTF(5, 6) |
| 47 | enum parse_next_item_status parse_next_item(struct argpar_iter *iter, |
| 48 | const struct argpar_item **item, const char **argv, |
| 49 | bool unknown_opt_is_error, const char *context_fmt, ...); |
| 50 | |
| 51 | #ifdef __cplusplus |
| 52 | } |
| 53 | #endif |
| 54 | #endif |