2 * SPDX-License-Identifier: MIT
4 * Copyright (c) 2019-2021 Philippe Proulx <pproulx@efficios.com>
5 * Copyright (c) 2020-2021 Simon Marchi <simon.marchi@efficios.com>
8 #ifndef ARGPAR_ARGPAR_H
9 #define ARGPAR_ARGPAR_H
14 * argpar is a library which provides facilities for command-line
17 * Two APIs are available:
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:
24 * * There are no more arguments.
26 * * The argument parser encounters an error (for example, an
31 * This API provides more parsing control than the next one.
34 * Call argpar_parse(), which parses the arguments until one of:
36 * * There are no more arguments.
38 * * It encounters an argument parsing error.
40 * argpar_parse() returns a single array of parsing results.
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`.
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
50 * The argpar parsers support:
52 * * Short options without an argument, possibly tied together:
56 * * Short options with argument:
58 * -b 45 -f/mein/file -xyzhello
60 * * Long options without an argument:
62 * --five-guys --burger-king --pizza-hut --subway
64 * * Long options with arguments:
66 * --security enable --time=18.56
68 * * Non-option arguments (anything else).
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.
79 * Both argpar_iter_create() and argpar_parse() accept duplicate options
80 * (they produce one item for each instance).
82 * A returned parsing item has the type `struct argpar_item *`. Each
83 * item is to be casted to the appropriate type
84 * (`struct argpar_item_opt *` or `struct argpar_item_non_opt *`)
85 * depending on its `type` member.
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:
91 * --hello --count=23 /path/to/file -ab --type file magie
93 * The produced items are, in this order:
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`).
104 /* Sentinel for an option descriptor array */
105 #define ARGPAR_OPT_DESCR_SENTINEL { -1, '\0', NULL, false }
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".
111 * On Windows, symbols are local unless explicitly exported; see
112 * <https://gcc.gnu.org/wiki/Visibility>.
114 #if defined(_WIN32) || defined(__CYGWIN__)
115 # define ARGPAR_HIDDEN
117 # define ARGPAR_HIDDEN __attribute__((visibility("hidden")))
120 /* Forward-declaration for the opaque type */
123 /* Option descriptor */
124 struct argpar_opt_descr
{
125 /* Numeric ID for this option */
128 /* Short option character, or `\0` */
129 const char short_name
;
131 /* Long option name (without `--`), or `NULL` */
132 const char * const long_name
;
134 /* True if this option has an argument */
139 enum argpar_item_type
{
141 ARGPAR_ITEM_TYPE_OPT
,
144 ARGPAR_ITEM_TYPE_NON_OPT
,
149 enum argpar_item_type type
;
153 struct argpar_item_opt
{
154 struct argpar_item base
;
156 /* Corresponding descriptor */
157 const struct argpar_opt_descr
*descr
;
159 /* Argument, or `NULL` if none */
163 /* Non-option item */
164 struct argpar_item_non_opt
{
165 struct argpar_item base
;
168 * Complete argument, pointing to one of the entries of the
169 * original arguments (`argv`).
173 /* Index of this argument amongst all original arguments (`argv`) */
174 unsigned int orig_index
;
176 /* Index of this argument amongst other non-option arguments */
177 unsigned int non_opt_index
;
180 struct argpar_item_array
{
181 const struct argpar_item
**items
;
183 /* Number of used slots in `items` */
184 unsigned int n_items
;
186 /* Number of allocated slots in `items` */
187 unsigned int n_alloc
;
190 /* What is returned by argpar_parse() */
191 struct argpar_parse_ret
{
193 * Array of parsing items, or `NULL` on error.
195 * Do NOT destroy those items manually with
196 * argpar_iter_destroy(): call argpar_parse_ret_fini() to
197 * finalize the whole structure.
199 struct argpar_item_array
*items
;
201 /* Error string, or `NULL` if none */
204 /* Number of original arguments (`argv`) ingested */
205 unsigned int ingested_orig_args
;
209 * Parses arguments in `argv` until the end is reached or an error is
212 * On success, this function returns an array of items (field `items` of
213 * `struct argpar_parse_ret`).
215 * In the returned structure, `ingested_orig_args` is the number of
216 * ingested arguments within `argv` to produce the resulting array of
219 * If `fail_on_unknown_opt` is true, then on success
220 * `ingested_orig_args` is equal to `argc`. Otherwise,
221 * `ingested_orig_args` contains the number of original arguments until
222 * an unknown _option_ occurs. For example, with
224 * --great --white contact nuance --shark nuclear
226 * if `--shark` is not described within `descrs` and
227 * `fail_on_unknown_opt` is false, then `ingested_orig_args` is 4 (two
228 * options, two non-options), whereas `argc` is 6.
230 * This makes it possible to know where a command name is, for example.
231 * With those arguments:
233 * --verbose --stuff=23 do-something --specific-opt -f -b
235 * and the descriptors for `--verbose` and `--stuff` only, the function
236 * returns the `--verbose` and `--stuff` option items, the
237 * `do-something` non-option item, and that three original arguments
238 * were ingested. This means you can start the next argument parsing
239 * stage, with option descriptors depending on the command name, at
242 * Note that `ingested_orig_args` is not always equal to the number of
247 * for example contains two ingested original arguments, but four
250 * On failure, the `items` member of the returned structure is `NULL`,
251 * and the `error` string member contains details about the error.
253 * Finalize the returned structure with argpar_parse_ret_fini().
256 struct argpar_parse_ret
argpar_parse(unsigned int argc
,
257 const char * const *argv
,
258 const struct argpar_opt_descr
*descrs
,
259 bool fail_on_unknown_opt
);
262 * Finalizes what argpar_parse() returns.
264 * You may call argpar_parse() multiple times with the same structure.
267 void argpar_parse_ret_fini(struct argpar_parse_ret
*ret
);
270 * Creates an argument parsing iterator.
272 * This function initializes the returned structure, but doesn't
273 * actually start parsing the arguments.
275 * `*argv` and `*descrs` must NOT change for the lifetime of the
276 * returned iterator (until you call argpar_iter_destroy()).
278 * Call argpar_iter_parse_next() with the returned iterator to obtain
279 * the next parsing result (item).
282 struct argpar_iter
*argpar_iter_create(unsigned int argc
,
283 const char * const *argv
,
284 const struct argpar_opt_descr
*descrs
);
287 * Destroys `iter`, as returned by argpar_iter_create().
290 void argpar_iter_destroy(struct argpar_iter
*iter
);
293 * Return type of argpar_iter_parse_next().
295 enum argpar_iter_parse_next_status
{
296 ARGPAR_ITER_PARSE_NEXT_STATUS_OK
,
297 ARGPAR_ITER_PARSE_NEXT_STATUS_END
,
298 ARGPAR_ITER_PARSE_NEXT_STATUS_ERROR_UNKNOWN_OPT
,
299 ARGPAR_ITER_PARSE_NEXT_STATUS_ERROR
,
303 * Parses and returns the next item from `iter`.
305 * On success, this function sets `*item` to an item which describes the
306 * next option or non-option argument and returns
307 * `ARGPAR_ITER_PARSE_NEXT_STATUS_OK`. Destroy `*item` with
308 * argpar_item_destroy().
310 * If there are no more items to return, this function returns
311 * `ARGPAR_ITER_PARSE_NEXT_STATUS_END`.
313 * On failure (status codes
314 * `ARGPAR_ITER_PARSE_NEXT_STATUS_ERROR_UNKNOWN_OPT` and
315 * `ARGPAR_ITER_PARSE_NEXT_STATUS_ERROR`), this function sets `*error`
316 * to a descriptive error string. Free `*error` with free().
318 * Create an argument parsing iterator with argpar_iter_create().
320 enum argpar_iter_parse_next_status
argpar_iter_parse_next(
321 struct argpar_iter
*iter
, const struct argpar_item
**item
,
325 * Returns the number of ingested elements from `argv`, as passed to
326 * argpar_iter_create() to create `*iter`, that were required to produce
327 * the previously returned items.
330 unsigned int argpar_iter_get_ingested_orig_args(const struct argpar_iter
*iter
);
333 * Destroys `item`, as created by argpar_iter_parse_next().
336 void argpar_item_destroy(const struct argpar_item
*item
);
339 * Destroys `_item` (`const struct argpar_item *`) and sets it to
342 #define ARGPAR_ITEM_DESTROY_AND_RESET(_item) \
344 argpar_item_destroy(_item); \
348 #endif /* ARGPAR_ARGPAR_H */