2 * Copyright (C) 2021 Simon Marchi <simon.marchi@efficios.com>
4 * SPDX-License-Identifier: GPL-2.0-only
8 #include "argpar-utils.h"
12 #include <common/error.h>
13 #include <common/string-utils/string-utils.h>
15 #define WHILE_PARSING_ARG_N_ARG_FMT "While parsing argument #%d (`%s`): "
18 * Given argpar error status `status` and error `error`, return a formatted
19 * error message describing the error.
21 * `argv` is the argument vector that was being parsed.
23 * `context_fmt`, if non-NULL, is formatted using `args` and prepended to the
26 * Add `argc_offset` the the argument index mentioned in the error message.
28 * The returned string must be freed by the caller.
31 char *format_arg_error_v(const struct argpar_error
*error
, int argc_offset
,
32 const char **argv
, const char *context_fmt
, va_list args
)
39 ret
= vasprintf(&str
, context_fmt
, args
);
42 * If vasprintf fails, the content of str is undefined,
43 * and we shouldn't try to free it.
49 ret
= strutils_append_str(&str
, ": ");
55 switch (argpar_error_type(error
))
57 case ARGPAR_ERROR_TYPE_MISSING_OPT_ARG
:
59 int orig_index
= argpar_error_orig_index(error
);
60 const char *arg
= argv
[orig_index
];
62 ret
= strutils_appendf(&str
,
63 WHILE_PARSING_ARG_N_ARG_FMT
"Missing required argument for option `%s`",
64 orig_index
+ 1 + argc_offset
, argv
[orig_index
], arg
);
71 case ARGPAR_ERROR_TYPE_UNEXPECTED_OPT_ARG
:
74 const struct argpar_opt_descr
*descr
=
75 argpar_error_opt_descr(error
, &is_short
);
76 int orig_index
= argpar_error_orig_index(error
);
77 const char *arg
= argv
[orig_index
];
80 ret
= strutils_appendf(&str
,
81 WHILE_PARSING_ARG_N_ARG_FMT
"Unexpected argument for option `-%c`",
82 orig_index
+ 1 + argc_offset
, arg
, descr
->short_name
);
84 ret
= strutils_appendf(&str
,
85 WHILE_PARSING_ARG_N_ARG_FMT
"Unexpected argument for option `--%s`",
86 orig_index
+ 1 + argc_offset
, arg
, descr
->long_name
);
95 case ARGPAR_ERROR_TYPE_UNKNOWN_OPT
:
97 int orig_index
= argpar_error_orig_index(error
);
98 const char *unknown_opt
= argpar_error_unknown_opt_name(error
);
100 ret
= strutils_appendf(&str
,
101 WHILE_PARSING_ARG_N_ARG_FMT
"Unknown option `%s`",
102 orig_index
+ 1 + argc_offset
, argv
[orig_index
], unknown_opt
);
123 enum parse_next_item_status
parse_next_item(struct argpar_iter
*iter
,
124 const struct argpar_item
**item
, int argc_offset
,
125 const char **argv
, bool unknown_opt_is_error
,
126 const char *context_fmt
, ...)
128 enum argpar_iter_next_status status
;
129 const struct argpar_error
*error
= NULL
;
130 enum parse_next_item_status ret
;
132 ARGPAR_ITEM_DESTROY_AND_RESET(*item
);
133 status
= argpar_iter_next(iter
, item
, &error
);
136 case ARGPAR_ITER_NEXT_STATUS_ERROR_MEMORY
:
137 ERR("Failed to get next argpar item.");
138 ret
= PARSE_NEXT_ITEM_STATUS_ERROR
;
140 case ARGPAR_ITER_NEXT_STATUS_ERROR
:
145 if (argpar_error_type(error
) == ARGPAR_ERROR_TYPE_UNKNOWN_OPT
&&
146 !unknown_opt_is_error
) {
147 ret
= PARSE_NEXT_ITEM_STATUS_END
;
151 va_start(args
, context_fmt
);
152 err_str
= format_arg_error_v(error
, argc_offset
, argv
,
160 ERR("%s", "Failed to format argpar error.");
163 ret
= PARSE_NEXT_ITEM_STATUS_ERROR
;
166 case ARGPAR_ITER_NEXT_STATUS_END
:
167 ret
= PARSE_NEXT_ITEM_STATUS_END
;
169 case ARGPAR_ITER_NEXT_STATUS_OK
:
170 ret
= PARSE_NEXT_ITEM_STATUS_OK
;
176 argpar_error_destroy(error
);