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
16 See the \ref api module.
18 @addtogroup api argpar API
21 argpar is a library which provides an iterator-based API to parse
22 command-line arguments.
24 The argpar parser supports:
28 Short options without an argument, possibly tied together:
35 Short options with arguments:
38 -b 45 -f/mein/file -xyzhello
42 Long options without an argument:
45 --five-guys --burger-king --pizza-hut --subway
49 Long options with arguments (two original arguments or a single
50 one with a <code>=</code> character):
53 --security enable --time=18.56
57 Non-option arguments (anything else, including
58 <code>-</code> and <code>\--</code>).
60 A non-option argument cannot have the form of an option, for example
61 if you need to pass the exact relative path
62 <code>\--component</code>. In that case, you would need to pass
63 <code>./\--component</code>. There's no generic way to escape
64 <code>-</code> as of this version.
67 Create a parsing iterator with argpar_iter_create(), then repeatedly
68 call argpar_iter_next() to access the parsing results (items), until one
71 - There are no more arguments.
73 - The argument parser encounters an error (for example, an unknown
78 argpar_iter_create() accepts duplicate option descriptors in
79 \p descrs (argpar_iter_next() produces one item for each
82 A parsing item (the result of argpar_iter_next()) has the type
85 Get the type (option or non-option) of an item with
86 argpar_item_type(). Each item type has its set of dedicated functions
87 (\c argpar_item_opt_ and \c argpar_item_non_opt_ prefixes).
89 argpar_iter_next() produces the items in the same order that it parses
90 original arguments, including non-option arguments. This means, for
94 --hello --count=23 /path/to/file -ab --type file -- magie
97 argpar_iter_next() produces the following items, in this order:
99 -# Option item (<code>\--hello</code>).
100 -# Option item (<code>\--count</code> with argument <code>23</code>).
101 -# Non-option item (<code>/path/to/file</code>).
102 -# Option item (<code>-a</code>).
103 -# Option item (<code>-b</code>).
104 -# Option item (<code>\--type</code> with argument <code>file</code>).
105 -# Non-option item (<code>\--</code>).
106 -# Non-option item (<code>magie</code>).
110 * If argpar is used in some shared library, we don't want said library
111 * to export its symbols, so mark them as "hidden".
113 * On Windows, symbols are local unless explicitly exported; see
114 * <https://gcc.gnu.org/wiki/Visibility>.
116 #if defined(_WIN32) || defined(__CYGWIN__)
117 # define ARGPAR_HIDDEN
119 # define ARGPAR_HIDDEN __attribute__((visibility("hidden")))
122 struct argpar_opt_descr
;
131 Type of a parsing item, as returned by argpar_item_type().
133 enum argpar_item_type
{
135 ARGPAR_ITEM_TYPE_OPT
,
138 ARGPAR_ITEM_TYPE_NON_OPT
,
145 Opaque parsing item type
147 argpar_iter_next() sets a pointer to such a type.
153 Returns the type of the parsing item \p item.
156 Parsing item of which to get the type.
162 \p item is not \c NULL.
164 /// @cond hidden_macro
167 enum argpar_item_type
argpar_item_type(const struct argpar_item
*item
);
171 Returns the option descriptor of the option parsing item \p item.
174 Option parsing item of which to get the option descriptor.
177 Option descriptor of \p item.
180 \p item is not \c NULL.
182 \p item has the type #ARGPAR_ITEM_TYPE_OPT.
184 /// @cond hidden_macro
187 const struct argpar_opt_descr
*argpar_item_opt_descr(
188 const struct argpar_item
*item
);
192 Returns the argument of the option parsing item \p item, or
196 Option parsing item of which to get the argument.
199 Argument of \p item, or \c NULL if none.
202 \p item is not \c NULL.
204 \p item has the type #ARGPAR_ITEM_TYPE_OPT.
206 /// @cond hidden_macro
209 const char *argpar_item_opt_arg(const struct argpar_item
*item
);
213 Returns the complete original argument, pointing to one of the
214 entries of the original arguments (\p argv as passed to
215 argpar_iter_create()), of the non-option parsing item \p item.
218 Non-option parsing item of which to get the complete original
222 Complete original argument of \p item.
225 \p item is not \c NULL.
227 \p item has the type #ARGPAR_ITEM_TYPE_NON_OPT.
229 /// @cond hidden_macro
232 const char *argpar_item_non_opt_arg(const struct argpar_item
*item
);
236 Returns the index, within \em all the original arguments (\p argv
237 as passed to argpar_iter_create()), of the non-option parsing item
240 For example, with the following command line (all options have no
244 -f -m meow --jus mix --kilo
247 The original argument index of \c meow is 2 while the original
248 argument index of \c mix is 4.
251 Non-option parsing item of which to get the original argument index.
254 Original argument index of \p item.
257 \p item is not \c NULL.
259 \p item has the type #ARGPAR_ITEM_TYPE_NON_OPT.
262 argpar_item_non_opt_non_opt_index() -- Returns the non-option index
263 of a non-option parsing item.
265 /// @cond hidden_macro
268 unsigned int argpar_item_non_opt_orig_index(const struct argpar_item
*item
);
272 Returns the index, within the parsed non-option parsing items, of
273 the non-option parsing item \p item.
275 For example, with the following command line (all options have no
279 -f -m meow --jus mix --kilo
282 The non-option index of \c meow is 0 while the original
283 argument index of \c mix is 1.
286 Non-option parsing item of which to get the non-option index.
289 Non-option index of \p item.
292 \p item is not \c NULL.
294 \p item has the type #ARGPAR_ITEM_TYPE_NON_OPT.
297 argpar_item_non_opt_orig_index() -- Returns the original argument
298 index of a non-option parsing item.
300 /// @cond hidden_macro
303 unsigned int argpar_item_non_opt_non_opt_index(const struct argpar_item
*item
);
307 Destroys the parsing item \p item.
310 Parsing item to destroy (may be \c NULL).
312 /// @cond hidden_macro
315 void argpar_item_destroy(const struct argpar_item
*item
);
318 @def ARGPAR_ITEM_DESTROY_AND_RESET(_item)
321 Calls argpar_item_destroy() with \p _item, and then sets \p _item
325 Item to destroy and variable to reset
326 (<code>const struct argpar_item *</code> type).
328 #define ARGPAR_ITEM_DESTROY_AND_RESET(_item) \
330 argpar_item_destroy(_item); \
345 argpar_iter_create() accepts an array of instances of such a type,
346 terminated with #ARGPAR_OPT_DESCR_SENTINEL, as its \p descrs parameter.
348 The typical usage is, for example:
351 const struct argpar_opt_descr descrs[] = {
352 { 0, 'd', NULL, false },
353 { 1, '\0', "squeeze", true },
354 { 2, 'm', "meow", true },
355 ARGPAR_OPT_DESCR_SENTINEL,
359 struct argpar_opt_descr
{
360 /// Numeric ID, to uniquely identify this descriptor
363 /// Short option character, or <code>'\0'</code>
364 const char short_name
;
366 /// Long option name (without the <code>\--</code> prefix), or \c NULL
367 const char * const long_name
;
369 /// \c true if this option has an argument
375 Sentinel for an option descriptor array
377 The typical usage is, for example:
380 const struct argpar_opt_descr descrs[] = {
381 { 0, 'd', NULL, false },
382 { 1, '\0', "squeeze", true },
383 { 2, 'm', "meow", true },
384 ARGPAR_OPT_DESCR_SENTINEL,
388 #define ARGPAR_OPT_DESCR_SENTINEL { -1, '\0', NULL, false }
394 Opaque argpar iterator type
396 argpar_iter_create() returns a pointer to such a type.
402 Creates and returns an argument parsing iterator to parse the
403 original arguments \p argv of which the count is \p argc using the
404 option descriptors \p descrs.
406 This function initializes the returned structure, but doesn't actually
407 start parsing the arguments.
409 argpar considers \em all the elements of \p argv, including the first
410 one, so that you would typically pass <code>(argc - 1)</code> as \p argc
411 and <code>\&argv[1]</code> as \p argv from what <code>main()</code>
412 receives, or ignore the parsing item of the first call to
415 \p *argv and \p *descrs must \em not change for the lifetime of the
416 returned iterator (until you call argpar_iter_destroy()) and for the
417 lifetime of any parsing item (until you call argpar_item_destroy())
418 which argpar_iter_next() creates from the returned iterator.
421 Number of original arguments to parse in \p argv.
423 Original arguments to parse, of which the count is \p argc.
426 Option descriptor array, terminated with #ARGPAR_OPT_DESCR_SENTINEL.
428 May contain duplicate entries.
432 New argument parsing iterator, or \c NULL on memory error.
435 \p argc is greater than 0.
437 \p argv is not \c NULL.
439 The first \p argc elements of \p argv are not \c NULL.
441 \p descrs is not \c NULL.
444 argpar_iter_destroy() -- Destroys an argument parsing iterator.
446 /// @cond hidden_macro
449 struct argpar_iter
*argpar_iter_create(unsigned int argc
,
450 const char * const *argv
,
451 const struct argpar_opt_descr
*descrs
);
455 Destroys the argument parsing iterator \p iter.
458 Argument parsing iterator to destroy (may be \c NULL).
461 argpar_iter_create() -- Creates an argument parsing iterator.
463 /// @cond hidden_macro
466 void argpar_iter_destroy(struct argpar_iter
*iter
);
470 Return type of argpar_iter_next().
472 enum argpar_iter_next_status
{
474 ARGPAR_ITER_NEXT_STATUS_OK
,
476 /// End of iteration (no more original arguments to parse)
477 ARGPAR_ITER_NEXT_STATUS_END
,
479 /// Unknown option error
480 ARGPAR_ITER_NEXT_STATUS_ERROR_UNKNOWN_OPT
,
482 /// Missing option argument error
483 ARGPAR_ITER_NEXT_STATUS_ERROR_MISSING_OPT_ARG
,
485 /// Unexpected option argument error
486 ARGPAR_ITER_NEXT_STATUS_ERROR_UNEXPECTED_OPT_ARG
,
489 ARGPAR_ITER_NEXT_STATUS_ERROR_MEMORY
,
494 Sets \p *item to the next item of the argument parsing iterator
495 \p iter and advances \p iter.
497 If there are no more original arguments to parse, this function returns
498 #ARGPAR_ITER_NEXT_STATUS_END.
501 Argument parsing iterator from which to get the next parsing item.
504 On success, \p *item is the next parsing item of \p iter.
506 Destroy \p *item with argpar_item_destroy().
510 When this function returns
511 #ARGPAR_ITER_NEXT_STATUS_ERROR_UNKNOWN_OPT,
512 #ARGPAR_ITER_NEXT_STATUS_ERROR_MISSING_OPT_ARG, or
513 #ARGPAR_ITER_NEXT_STATUS_ERROR_UNEXPECTED_OPG_ARG, if this parameter
515 \p *error is a string which explains the parsing error in English.
517 Free \p *error with <code>free()</code>.
524 \p iter is not \c NULL.
526 \p item is not \c NULL.
528 /// @cond hidden_macro
531 enum argpar_iter_next_status
argpar_iter_next(
532 struct argpar_iter
*iter
, const struct argpar_item
**item
,
536 * Returns the number of ingested elements from `argv`, as passed to
537 * argpar_iter_create() to create `*iter`, that were required to produce
538 * the previously returned items.
543 Returns the number of ingested original arguments (in
544 \p argv as passed to argpar_iter_create() to create \p iter) that
545 the parser ingested to produce the \em previous parsing items.
548 Argument parsing iterator of which to get the number of ingested
552 Number of original arguments which \p iter ingested.
555 \p iter is not \c NULL.
557 /// @cond hidden_macro
560 unsigned int argpar_iter_ingested_orig_args(const struct argpar_iter
*iter
);
566 #endif /* ARGPAR_ARGPAR_H */