argpar.c: add error enumerators to `enum parse_orig_arg_opt_ret`
[argpar.git] / argpar / argpar.h
CommitLineData
903a5b8a 1/*
03e1579f 2 * SPDX-License-Identifier: MIT
903a5b8a 3 *
fc07e526
SM
4 * Copyright (c) 2019-2021 Philippe Proulx <pproulx@efficios.com>
5 * Copyright (c) 2020-2021 Simon Marchi <simon.marchi@efficios.com>
903a5b8a
SM
6 */
7
fe5a18f8
PP
8#ifndef ARGPAR_ARGPAR_H
9#define ARGPAR_ARGPAR_H
03e1579f 10
903a5b8a
SM
11#include <stdbool.h>
12
fc07e526
SM
13/*
14 * argpar is a library which provides facilities for command-line
15 * argument parsing.
16 *
17 * Two APIs are available:
18 *
19 * Iterator API:
20 * Create a parsing iterator with argpar_iter_create(), then
21 * repeatedly call argpar_iter_parse_next() to access the parsing
22 * results, until one of:
23 *
24 * * There are no more arguments.
25 *
26 * * The argument parser encounters an error (for example, an
27 * unknown option).
28 *
29 * * You need to stop.
30 *
31 * This API provides more parsing control than the next one.
32 *
33 * Single call API:
34 * Call argpar_parse(), which parses the arguments until one of:
35 *
36 * * There are no more arguments.
37 *
38 * * It encounters an argument parsing error.
39 *
40 * argpar_parse() returns a single array of parsing results.
41 *
42 * Both methods parse the arguments `argv` of which the count is `argc`
43 * using the sentinel-terminated (use `ARGPAR_OPT_DESCR_SENTINEL`)
44 * option descriptor array `descrs`.
45 *
46 * argpar considers ALL the elements of `argv`, including the first one,
47 * so that you would typically pass `argc - 1` and `&argv[1]` from what
48 * main() receives.
49 *
50 * The argpar parsers support:
51 *
52 * * Short options without an argument, possibly tied together:
53 *
54 * -f -auf -n
55 *
56 * * Short options with argument:
57 *
58 * -b 45 -f/mein/file -xyzhello
59 *
60 * * Long options without an argument:
61 *
62 * --five-guys --burger-king --pizza-hut --subway
63 *
64 * * Long options with arguments:
65 *
66 * --security enable --time=18.56
67 *
68 * * Non-option arguments (anything else).
69 *
70 * The argpar parsers don't accept `-` or `--` as arguments. The latter
71 * means "end of options" for many command-line tools, but this library
72 * is all about keeping the order of the arguments, so it doesn't mean
73 * much to put them at the end. This has the side effect that a
74 * non-option argument cannot have the form of an option, for example if
75 * you need to pass the exact relative path `--component`. In that case,
76 * you would need to pass `./--component`. There's no generic way to
77 * escape `-` as of this version.
78 *
79 * Both argpar_iter_create() and argpar_parse() accept duplicate options
80 * (they produce one item for each instance).
81 *
d4539a90
PP
82 * A returned parsing item has the type `const struct argpar_item *`.
83 * Get the type (option or non-option) of an item with
84 * argpar_item_type(). Each item type has its set of dedicated methods
85 * (`argpar_item_opt_` and `argpar_item_non_opt_` prefixes).
fc07e526
SM
86 *
87 * Both argpar_iter_create() and argpar_parse() produce the items in
88 * the same order that the arguments were parsed, including non-option
89 * arguments. This means, for example, that for:
90 *
91 * --hello --count=23 /path/to/file -ab --type file magie
92 *
93 * The produced items are, in this order:
94 *
95 * 1. Option item (`--hello`).
96 * 2. Option item (`--count` with argument `23`).
97 * 3. Non-option item (`/path/to/file`).
98 * 4. Option item (`-a`).
99 * 5. Option item (`-b`).
100 * 6. Option item (`--type` with argument `file`).
101 * 7. Non-option item (`magie`).
102 */
103
903a5b8a 104/* Sentinel for an option descriptor array */
1c9a6bde 105#define ARGPAR_OPT_DESCR_SENTINEL { -1, '\0', NULL, false }
903a5b8a 106
7ac57709 107/*
fb12ac67
PP
108 * If argpar is used in some shared library, we don't want said library
109 * to export its symbols, so mark them as "hidden".
7ac57709 110 *
fc07e526
SM
111 * On Windows, symbols are local unless explicitly exported; see
112 * <https://gcc.gnu.org/wiki/Visibility>.
7ac57709
SM
113 */
114#if defined(_WIN32) || defined(__CYGWIN__)
fb12ac67 115# define ARGPAR_HIDDEN
7ac57709 116#else
fb12ac67 117# define ARGPAR_HIDDEN __attribute__((visibility("hidden")))
7ac57709
SM
118#endif
119
fc07e526
SM
120/* Forward-declaration for the opaque type */
121struct argpar_iter;
122
903a5b8a 123/* Option descriptor */
1c9a6bde 124struct argpar_opt_descr {
903a5b8a
SM
125 /* Numeric ID for this option */
126 const int id;
127
128 /* Short option character, or `\0` */
129 const char short_name;
130
131 /* Long option name (without `--`), or `NULL` */
132 const char * const long_name;
133
134 /* True if this option has an argument */
135 const bool with_arg;
136};
137
138/* Item type */
1c9a6bde 139enum argpar_item_type {
903a5b8a 140 /* Option */
1c9a6bde 141 ARGPAR_ITEM_TYPE_OPT,
903a5b8a
SM
142
143 /* Non-option */
1c9a6bde 144 ARGPAR_ITEM_TYPE_NON_OPT,
903a5b8a
SM
145};
146
d4539a90
PP
147/* Parsing item, as created by argpar_parse() and argpar_iter_parse_next() */
148struct argpar_item;
903a5b8a 149
d4539a90
PP
150/*
151 * Returns the type of the parsing item `item`.
152 */
153ARGPAR_HIDDEN
154enum argpar_item_type argpar_item_type(const struct argpar_item *item);
903a5b8a 155
d4539a90
PP
156/*
157 * Returns the option descriptor of the option parsing item `item`.
158 */
159ARGPAR_HIDDEN
160const struct argpar_opt_descr *argpar_item_opt_descr(
161 const struct argpar_item *item);
903a5b8a 162
d4539a90
PP
163/*
164 * Returns the argument of the option parsing item `item`, or `NULL` if
165 * none.
166 */
167ARGPAR_HIDDEN
168const char *argpar_item_opt_arg(const struct argpar_item *item);
903a5b8a 169
d4539a90
PP
170/*
171 * Returns the complete argument, pointing to one of the entries of the
172 * original arguments (`argv`), of the non-option parsing item `item`.
173 */
174ARGPAR_HIDDEN
175const char *argpar_item_non_opt_arg(const struct argpar_item *item);
903a5b8a 176
d4539a90
PP
177/*
178 * Returns the original index, within ALL the original arguments
179 * (`argv`), of the non-option parsing item `item`.
180 */
181ARGPAR_HIDDEN
182unsigned int argpar_item_non_opt_orig_index(const struct argpar_item *item);
903a5b8a 183
d4539a90
PP
184/*
185 * Returns the index, within the non-option arguments, of the non-option
186 * parsing item `item`.
187 */
188ARGPAR_HIDDEN
189unsigned int argpar_item_non_opt_non_opt_index(const struct argpar_item *item);
903a5b8a 190
1c9a6bde 191struct argpar_item_array {
fb12ac67 192 const struct argpar_item **items;
7ac57709 193
fb12ac67 194 /* Number of used slots in `items` */
7ac57709
SM
195 unsigned int n_items;
196
fb12ac67 197 /* Number of allocated slots in `items` */
7ac57709
SM
198 unsigned int n_alloc;
199};
200
1c9a6bde
SM
201/* What is returned by argpar_parse() */
202struct argpar_parse_ret {
fc07e526 203 /*
fb12ac67 204 * Array of parsing items, or `NULL` on error.
fc07e526
SM
205 *
206 * Do NOT destroy those items manually with
207 * argpar_iter_destroy(): call argpar_parse_ret_fini() to
208 * finalize the whole structure.
209 */
1c9a6bde 210 struct argpar_item_array *items;
903a5b8a
SM
211
212 /* Error string, or `NULL` if none */
7ac57709 213 char *error;
903a5b8a
SM
214
215 /* Number of original arguments (`argv`) ingested */
216 unsigned int ingested_orig_args;
217};
218
219/*
fc07e526
SM
220 * Parses arguments in `argv` until the end is reached or an error is
221 * encountered.
903a5b8a 222 *
fc07e526
SM
223 * On success, this function returns an array of items (field `items` of
224 * `struct argpar_parse_ret`).
903a5b8a
SM
225 *
226 * In the returned structure, `ingested_orig_args` is the number of
227 * ingested arguments within `argv` to produce the resulting array of
fc07e526
SM
228 * items.
229 *
230 * If `fail_on_unknown_opt` is true, then on success
903a5b8a
SM
231 * `ingested_orig_args` is equal to `argc`. Otherwise,
232 * `ingested_orig_args` contains the number of original arguments until
233 * an unknown _option_ occurs. For example, with
234 *
235 * --great --white contact nuance --shark nuclear
236 *
237 * if `--shark` is not described within `descrs` and
238 * `fail_on_unknown_opt` is false, then `ingested_orig_args` is 4 (two
239 * options, two non-options), whereas `argc` is 6.
240 *
241 * This makes it possible to know where a command name is, for example.
242 * With those arguments:
243 *
244 * --verbose --stuff=23 do-something --specific-opt -f -b
245 *
246 * and the descriptors for `--verbose` and `--stuff` only, the function
247 * returns the `--verbose` and `--stuff` option items, the
248 * `do-something` non-option item, and that three original arguments
249 * were ingested. This means you can start the next argument parsing
250 * stage, with option descriptors depending on the command name, at
251 * `&argv[3]`.
252 *
253 * Note that `ingested_orig_args` is not always equal to the number of
254 * returned items, as
255 *
256 * --hello -fdw
257 *
258 * for example contains two ingested original arguments, but four
259 * resulting items.
260 *
fc07e526
SM
261 * On failure, the `items` member of the returned structure is `NULL`,
262 * and the `error` string member contains details about the error.
903a5b8a 263 *
fc07e526 264 * Finalize the returned structure with argpar_parse_ret_fini().
903a5b8a 265 */
7ac57709 266ARGPAR_HIDDEN
1c9a6bde 267struct argpar_parse_ret argpar_parse(unsigned int argc,
903a5b8a 268 const char * const *argv,
1c9a6bde 269 const struct argpar_opt_descr *descrs,
903a5b8a
SM
270 bool fail_on_unknown_opt);
271
272/*
fc07e526 273 * Finalizes what argpar_parse() returns.
903a5b8a 274 *
fc07e526 275 * You may call argpar_parse() multiple times with the same structure.
903a5b8a 276 */
7ac57709 277ARGPAR_HIDDEN
1c9a6bde 278void argpar_parse_ret_fini(struct argpar_parse_ret *ret);
903a5b8a 279
fc07e526
SM
280/*
281 * Creates an argument parsing iterator.
282 *
283 * This function initializes the returned structure, but doesn't
284 * actually start parsing the arguments.
285 *
286 * `*argv` and `*descrs` must NOT change for the lifetime of the
287 * returned iterator (until you call argpar_iter_destroy()).
288 *
289 * Call argpar_iter_parse_next() with the returned iterator to obtain
290 * the next parsing result (item).
291 */
292ARGPAR_HIDDEN
293struct argpar_iter *argpar_iter_create(unsigned int argc,
294 const char * const *argv,
295 const struct argpar_opt_descr *descrs);
296
297/*
298 * Destroys `iter`, as returned by argpar_iter_create().
299 */
300ARGPAR_HIDDEN
301void argpar_iter_destroy(struct argpar_iter *iter);
302
303/*
304 * Return type of argpar_iter_parse_next().
305 */
306enum argpar_iter_parse_next_status {
307 ARGPAR_ITER_PARSE_NEXT_STATUS_OK,
308 ARGPAR_ITER_PARSE_NEXT_STATUS_END,
309 ARGPAR_ITER_PARSE_NEXT_STATUS_ERROR_UNKNOWN_OPT,
310 ARGPAR_ITER_PARSE_NEXT_STATUS_ERROR,
311};
312
313/*
314 * Parses and returns the next item from `iter`.
315 *
316 * On success, this function sets `*item` to an item which describes the
317 * next option or non-option argument and returns
318 * `ARGPAR_ITER_PARSE_NEXT_STATUS_OK`. Destroy `*item` with
319 * argpar_item_destroy().
320 *
321 * If there are no more items to return, this function returns
322 * `ARGPAR_ITER_PARSE_NEXT_STATUS_END`.
323 *
324 * On failure (status codes
325 * `ARGPAR_ITER_PARSE_NEXT_STATUS_ERROR_UNKNOWN_OPT` and
37bb2b1f
PP
326 * `ARGPAR_ITER_PARSE_NEXT_STATUS_ERROR`), this function sets `*error`,
327 * if not `NULL`, to a descriptive error string. Free `*error` with
328 * free().
fc07e526
SM
329 *
330 * Create an argument parsing iterator with argpar_iter_create().
331 */
332enum argpar_iter_parse_next_status argpar_iter_parse_next(
333 struct argpar_iter *iter, const struct argpar_item **item,
334 char **error);
335
336/*
337 * Returns the number of ingested elements from `argv`, as passed to
338 * argpar_iter_create() to create `*iter`, that were required to produce
339 * the previously returned items.
340 */
341ARGPAR_HIDDEN
342unsigned int argpar_iter_get_ingested_orig_args(const struct argpar_iter *iter);
343
344/*
345 * Destroys `item`, as created by argpar_iter_parse_next().
346 */
347ARGPAR_HIDDEN
348void argpar_item_destroy(const struct argpar_item *item);
349
350/*
351 * Destroys `_item` (`const struct argpar_item *`) and sets it to
352 * `NULL`.
353 */
fb12ac67
PP
354#define ARGPAR_ITEM_DESTROY_AND_RESET(_item) \
355 { \
356 argpar_item_destroy(_item); \
357 _item = NULL; \
fc07e526
SM
358 }
359
fe5a18f8 360#endif /* ARGPAR_ARGPAR_H */
This page took 0.052891 seconds and 4 git commands to generate.