lttng: list valid condition / action names if missing or unknown
[lttng-tools.git] / src / common / argpar-utils / argpar-utils.c
CommitLineData
ea76a8bb
SM
1/*
2 * Copyright (C) 2021 Simon Marchi <simon.marchi@efficios.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 */
7
8#include "argpar-utils.h"
9
10#include <stdio.h>
11
12#include <common/error.h>
13#include <common/string-utils/string-utils.h>
14
ea76a8bb
SM
15/*
16 * Given argpar error status `status` and error `error`, return a formatted
17 * error message describing the error.
18 *
19 * `argv` is the argument vector that was being parsed.
20 *
21 * `context_fmt`, if non-NULL, is formatted using `args` and prepended to the
22 * error message.
23 *
5f5ce3e6
SM
24 * Add `argc_offset` the the argument index mentioned in the error message.
25 *
ea76a8bb
SM
26 * The returned string must be freed by the caller.
27 */
5f5ce3e6
SM
28static
29char *format_arg_error_v(const struct argpar_error *error, int argc_offset,
ea76a8bb
SM
30 const char **argv, const char *context_fmt, va_list args)
31{
32 char *str = NULL;
33 char *str_ret = NULL;
34 int ret;
35
36 if (context_fmt) {
37 ret = vasprintf(&str, context_fmt, args);
38 if (ret == -1) {
39 /*
40 * If vasprintf fails, the content of str is undefined,
41 * and we shouldn't try to free it.
42 */
43 str = NULL;
44 goto end;
45 }
46
47 ret = strutils_append_str(&str, ": ");
48 if (ret < 0) {
49 goto end;
50 }
51 }
52
53 switch (argpar_error_type(error))
54 {
55 case ARGPAR_ERROR_TYPE_MISSING_OPT_ARG:
56 {
57 int orig_index = argpar_error_orig_index(error);
58 const char *arg = argv[orig_index];
59
60 ret = strutils_appendf(&str,
61 WHILE_PARSING_ARG_N_ARG_FMT "Missing required argument for option `%s`",
5f5ce3e6 62 orig_index + 1 + argc_offset, argv[orig_index], arg);
ea76a8bb
SM
63 if (ret < 0) {
64 goto end;
65 }
66
67 break;
68 }
69 case ARGPAR_ERROR_TYPE_UNEXPECTED_OPT_ARG:
70 {
71 bool is_short;
72 const struct argpar_opt_descr *descr =
73 argpar_error_opt_descr(error, &is_short);
74 int orig_index = argpar_error_orig_index(error);
75 const char *arg = argv[orig_index];
76
77 if (is_short) {
78 ret = strutils_appendf(&str,
79 WHILE_PARSING_ARG_N_ARG_FMT "Unexpected argument for option `-%c`",
5f5ce3e6 80 orig_index + 1 + argc_offset, arg, descr->short_name);
ea76a8bb
SM
81 } else {
82 ret = strutils_appendf(&str,
83 WHILE_PARSING_ARG_N_ARG_FMT "Unexpected argument for option `--%s`",
5f5ce3e6 84 orig_index + 1 + argc_offset, arg, descr->long_name);
ea76a8bb
SM
85 }
86
87 if (ret < 0) {
88 goto end;
89 }
90
91 break;
92 }
93 case ARGPAR_ERROR_TYPE_UNKNOWN_OPT:
94 {
ffcdaa42 95 int orig_index = argpar_error_orig_index(error);
ea76a8bb
SM
96 const char *unknown_opt = argpar_error_unknown_opt_name(error);
97
98 ret = strutils_appendf(&str,
ffcdaa42 99 WHILE_PARSING_ARG_N_ARG_FMT "Unknown option `%s`",
5f5ce3e6 100 orig_index + 1 + argc_offset, argv[orig_index], unknown_opt);
ea76a8bb
SM
101
102 if (ret < 0) {
103 goto end;
104 }
105
106 break;
107 }
108 default:
109 abort ();
110 }
111
112 str_ret = str;
113 str = NULL;
114
115end:
116 free(str);
117 return str_ret;
118}
119
120LTTNG_HIDDEN
121enum parse_next_item_status parse_next_item(struct argpar_iter *iter,
5f5ce3e6
SM
122 const struct argpar_item **item, int argc_offset,
123 const char **argv, bool unknown_opt_is_error,
074060e8 124 const struct argpar_error **error_out,
5f5ce3e6 125 const char *context_fmt, ...)
ea76a8bb
SM
126{
127 enum argpar_iter_next_status status;
128 const struct argpar_error *error = NULL;
129 enum parse_next_item_status ret;
130
131 ARGPAR_ITEM_DESTROY_AND_RESET(*item);
132 status = argpar_iter_next(iter, item, &error);
133
134 switch (status) {
135 case ARGPAR_ITER_NEXT_STATUS_ERROR_MEMORY:
136 ERR("Failed to get next argpar item.");
074060e8 137 ret = PARSE_NEXT_ITEM_STATUS_ERROR_MEMORY;
ea76a8bb
SM
138 break;
139 case ARGPAR_ITER_NEXT_STATUS_ERROR:
140 {
141 va_list args;
142 char *err_str;
143
144 if (argpar_error_type(error) == ARGPAR_ERROR_TYPE_UNKNOWN_OPT &&
145 !unknown_opt_is_error) {
146 ret = PARSE_NEXT_ITEM_STATUS_END;
147 break;
148 }
149
150 va_start(args, context_fmt);
5f5ce3e6
SM
151 err_str = format_arg_error_v(error, argc_offset, argv,
152 context_fmt, args);
ea76a8bb
SM
153 va_end(args);
154
155 if (err_str) {
156 ERR("%s", err_str);
157 free(err_str);
158 } else {
159 ERR("%s", "Failed to format argpar error.");
160 }
161
162 ret = PARSE_NEXT_ITEM_STATUS_ERROR;
163 break;
164 }
165 case ARGPAR_ITER_NEXT_STATUS_END:
166 ret = PARSE_NEXT_ITEM_STATUS_END;
167 break;
168 case ARGPAR_ITER_NEXT_STATUS_OK:
169 ret = PARSE_NEXT_ITEM_STATUS_OK;
170 break;
171 default:
172 abort();
173 }
174
074060e8
SM
175 if (error_out) {
176 argpar_error_destroy(*error_out);
177 *error_out = error;
178 error = NULL;
179 }
180
ea76a8bb
SM
181 argpar_error_destroy(error);
182
183 return ret;
184}
This page took 0.029541 seconds and 4 git commands to generate.