Add missing va_end in argpar_vasprintf
[argpar.git] / argpar / argpar.h
CommitLineData
903a5b8a
SM
1#ifndef BABELTRACE_ARGPAR_H
2#define BABELTRACE_ARGPAR_H
3
4/*
5 * Copyright 2019 Philippe Proulx <pproulx@efficios.com>
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
24 */
25
903a5b8a
SM
26#include <stdbool.h>
27
903a5b8a 28/* Sentinel for an option descriptor array */
1c9a6bde 29#define ARGPAR_OPT_DESCR_SENTINEL { -1, '\0', NULL, false }
903a5b8a 30
7ac57709
SM
31/*
32 * ARGPAR_HIDDEN: if argpar is used in some shared library, we don't want them
33 * to be exported by that library, so mark them as "hidden".
34 *
35 * On Windows, symbols are local unless explicitly exported,
36 * see https://gcc.gnu.org/wiki/Visibility
37 */
38#if defined(_WIN32) || defined(__CYGWIN__)
39#define ARGPAR_HIDDEN
40#else
41#define ARGPAR_HIDDEN __attribute__((visibility("hidden")))
42#endif
43
903a5b8a 44/* Option descriptor */
1c9a6bde 45struct argpar_opt_descr {
903a5b8a
SM
46 /* Numeric ID for this option */
47 const int id;
48
49 /* Short option character, or `\0` */
50 const char short_name;
51
52 /* Long option name (without `--`), or `NULL` */
53 const char * const long_name;
54
55 /* True if this option has an argument */
56 const bool with_arg;
57};
58
59/* Item type */
1c9a6bde 60enum argpar_item_type {
903a5b8a 61 /* Option */
1c9a6bde 62 ARGPAR_ITEM_TYPE_OPT,
903a5b8a
SM
63
64 /* Non-option */
1c9a6bde 65 ARGPAR_ITEM_TYPE_NON_OPT,
903a5b8a
SM
66};
67
68/* Base item */
1c9a6bde
SM
69struct argpar_item {
70 enum argpar_item_type type;
903a5b8a
SM
71};
72
73/* Option item */
1c9a6bde
SM
74struct argpar_item_opt {
75 struct argpar_item base;
903a5b8a
SM
76
77 /* Corresponding descriptor */
1c9a6bde 78 const struct argpar_opt_descr *descr;
903a5b8a
SM
79
80 /* Argument, or `NULL` if none */
81 const char *arg;
82};
83
84/* Non-option item */
1c9a6bde
SM
85struct argpar_item_non_opt {
86 struct argpar_item base;
903a5b8a
SM
87
88 /*
89 * Complete argument, pointing to one of the entries of the
90 * original arguments (`argv`).
91 */
92 const char *arg;
93
94 /* Index of this argument amongst all original arguments (`argv`) */
95 unsigned int orig_index;
96
97 /* Index of this argument amongst other non-option arguments */
98 unsigned int non_opt_index;
99};
100
1c9a6bde
SM
101struct argpar_item_array {
102 /* Array of `struct argpar_item *`, or `NULL` on error */
103 struct argpar_item **items;
7ac57709 104
44e278df 105 /* Number of used slots in `items`. */
7ac57709
SM
106 unsigned int n_items;
107
44e278df 108 /* Number of allocated slots in `items`. */
7ac57709
SM
109 unsigned int n_alloc;
110};
111
1c9a6bde
SM
112/* What is returned by argpar_parse() */
113struct argpar_parse_ret {
114 /* Array of `struct argpar_item *`, or `NULL` on error */
115 struct argpar_item_array *items;
903a5b8a
SM
116
117 /* Error string, or `NULL` if none */
7ac57709 118 char *error;
903a5b8a
SM
119
120 /* Number of original arguments (`argv`) ingested */
121 unsigned int ingested_orig_args;
122};
123
124/*
125 * Parses the arguments `argv` of which the count is `argc` using the
1c9a6bde 126 * sentinel-terminated (use `ARGPAR_OPT_DESCR_SENTINEL`) option
903a5b8a
SM
127 * descriptor array `descrs`.
128 *
129 * This function considers ALL the elements of `argv`, including the
130 * first one, so that you would typically pass `argc - 1` and
131 * `&argv[1]` from what main() receives.
132 *
133 * This argument parser supports:
134 *
135 * * Short options without an argument, possibly tied together:
136 *
137 * -f -auf -n
138 *
139 * * Short options with argument:
140 *
141 * -b 45 -f/mein/file -xyzhello
142 *
143 * * Long options without an argument:
144 *
145 * --five-guys --burger-king --pizza-hut --subway
146 *
147 * * Long options with arguments:
148 *
149 * --security enable --time=18.56
150 *
151 * * Non-option arguments (anything else).
152 *
153 * This function does not accept `-` or `--` as arguments. The latter
154 * means "end of options" for many command-line tools, but this function
155 * is all about keeping the order of the arguments, so it does not mean
156 * much to put them at the end. This has the side effect that a
157 * non-option argument cannot have the form of an option, for example if
158 * you need to pass the exact relative path `--component`. In that case,
159 * you would need to pass `./--component`. There's no generic way to
160 * escape `-` for the moment.
161 *
162 * This function accepts duplicate options (the resulting array of items
163 * contains one entry for each instance).
164 *
165 * On success, this function returns an array of items
1c9a6bde
SM
166 * (`struct argpar_item *`). Each item is to be casted to the
167 * appropriate type (`struct argpar_item_opt *` or
168 * `struct argpar_item_non_opt *`) depending on its type.
903a5b8a
SM
169 *
170 * The returned array contains the items in the same order that the
171 * arguments were parsed, including non-option arguments. This means,
172 * for example, that for
173 *
174 * --hello --meow=23 /path/to/file -b
175 *
176 * the function returns an array of four items: two options, one
177 * non-option, and one option.
178 *
179 * In the returned structure, `ingested_orig_args` is the number of
180 * ingested arguments within `argv` to produce the resulting array of
181 * items. If `fail_on_unknown_opt` is true, then on success
182 * `ingested_orig_args` is equal to `argc`. Otherwise,
183 * `ingested_orig_args` contains the number of original arguments until
184 * an unknown _option_ occurs. For example, with
185 *
186 * --great --white contact nuance --shark nuclear
187 *
188 * if `--shark` is not described within `descrs` and
189 * `fail_on_unknown_opt` is false, then `ingested_orig_args` is 4 (two
190 * options, two non-options), whereas `argc` is 6.
191 *
192 * This makes it possible to know where a command name is, for example.
193 * With those arguments:
194 *
195 * --verbose --stuff=23 do-something --specific-opt -f -b
196 *
197 * and the descriptors for `--verbose` and `--stuff` only, the function
198 * returns the `--verbose` and `--stuff` option items, the
199 * `do-something` non-option item, and that three original arguments
200 * were ingested. This means you can start the next argument parsing
201 * stage, with option descriptors depending on the command name, at
202 * `&argv[3]`.
203 *
204 * Note that `ingested_orig_args` is not always equal to the number of
205 * returned items, as
206 *
207 * --hello -fdw
208 *
209 * for example contains two ingested original arguments, but four
210 * resulting items.
211 *
212 * On failure, the returned structure's `items` member is `NULL`, and
213 * the `error` string member contains details about the error.
214 *
215 * You can finalize the returned structure with
1c9a6bde 216 * argpar_parse_ret_fini().
903a5b8a 217 */
7ac57709 218ARGPAR_HIDDEN
1c9a6bde 219struct argpar_parse_ret argpar_parse(unsigned int argc,
903a5b8a 220 const char * const *argv,
1c9a6bde 221 const struct argpar_opt_descr *descrs,
903a5b8a
SM
222 bool fail_on_unknown_opt);
223
224/*
1c9a6bde 225 * Finalizes what is returned by argpar_parse().
903a5b8a 226 *
1c9a6bde 227 * It is safe to call argpar_parse() multiple times with the same
903a5b8a
SM
228 * structure.
229 */
7ac57709 230ARGPAR_HIDDEN
1c9a6bde 231void argpar_parse_ret_fini(struct argpar_parse_ret *ret);
903a5b8a
SM
232
233#endif /* BABELTRACE_ARGPAR_H */
This page took 0.030992 seconds and 4 git commands to generate.