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 * Create a parsing iterator with argpar_iter_create(), then
18 * repeatedly call argpar_iter_next() to access the parsing results,
21 * * There are no more arguments.
23 * * The argument parser encounters an error (for example, an unknown
28 * The argpar parser parses the original arguments `argv` of which the
29 * count is `argc` using the sentinel-terminated (use
30 * `ARGPAR_OPT_DESCR_SENTINEL`) option descriptor array `descrs`.
32 * argpar considers ALL the elements of `argv`, including the first one,
33 * so that you would typically pass `argc - 1` and `&argv[1]` from what
36 * The argpar parser supports:
38 * * Short options without an argument, possibly tied together:
42 * * Short options with argument:
44 * -b 45 -f/mein/file -xyzhello
46 * * Long options without an argument:
48 * --five-guys --burger-king --pizza-hut --subway
50 * * Long options with arguments:
52 * --security enable --time=18.56
54 * * Non-option arguments (anything else).
56 * The argpar parser parses `-` and `--` as non-option arguments. A
57 * non-option argument cannot have the form of an option, for example if
58 * you need to pass the exact relative path `--component`. In that case,
59 * you would need to pass `./--component`. There's no generic way to
60 * escape `-` as of this version.
62 * argpar_iter_create() accepts duplicate options in `descrs` (it
63 * produces one item for each instance).
65 * A returned parsing item has the type `const struct argpar_item *`.
66 * Get the type (option or non-option) of an item with
67 * argpar_item_type(). Each item type has its set of dedicated methods
68 * (`argpar_item_opt_` and `argpar_item_non_opt_` prefixes).
70 * argpar_iter_next() produces the items in the same order that the
71 * original arguments were parsed, including non-option arguments. This
72 * means, for example, that for:
74 * --hello --count=23 /path/to/file -ab --type file magie
76 * argpar_iter_next() produces the following items, in this order:
78 * 1. Option item (`--hello`).
79 * 2. Option item (`--count` with argument `23`).
80 * 3. Non-option item (`/path/to/file`).
81 * 4. Option item (`-a`).
82 * 5. Option item (`-b`).
83 * 6. Option item (`--type` with argument `file`).
84 * 7. Non-option item (`magie`).
87 /* Sentinel for an option descriptor array */
88 #define ARGPAR_OPT_DESCR_SENTINEL { -1, '\0', NULL, false }
91 * If argpar is used in some shared library, we don't want said library
92 * to export its symbols, so mark them as "hidden".
94 * On Windows, symbols are local unless explicitly exported; see
95 * <https://gcc.gnu.org/wiki/Visibility>.
97 #if defined(_WIN32) || defined(__CYGWIN__)
98 # define ARGPAR_HIDDEN
100 # define ARGPAR_HIDDEN __attribute__((visibility("hidden")))
103 /* Forward-declaration for the opaque argpar iterator type */
106 /* Option descriptor */
107 struct argpar_opt_descr
{
108 /* Numeric ID for this option */
111 /* Short option character, or `\0` */
112 const char short_name
;
114 /* Long option name (without the `--` prefix), or `NULL` */
115 const char * const long_name
;
117 /* True if this option has an argument */
122 enum argpar_item_type
{
124 ARGPAR_ITEM_TYPE_OPT
,
127 ARGPAR_ITEM_TYPE_NON_OPT
,
130 /* Forward-declaration for the opaque argpar parsing item type */
134 * Returns the type of the parsing item `item`.
137 enum argpar_item_type
argpar_item_type(const struct argpar_item
*item
);
140 * Returns the option descriptor of the option parsing item `item`.
143 const struct argpar_opt_descr
*argpar_item_opt_descr(
144 const struct argpar_item
*item
);
147 * Returns the argument of the option parsing item `item`, or `NULL` if
151 const char *argpar_item_opt_arg(const struct argpar_item
*item
);
154 * Returns the complete argument, pointing to one of the entries of the
155 * original arguments (`argv`), of the non-option parsing item `item`.
158 const char *argpar_item_non_opt_arg(const struct argpar_item
*item
);
161 * Returns the original index, within ALL the original arguments
162 * (`argv`), of the non-option parsing item `item`.
165 unsigned int argpar_item_non_opt_orig_index(const struct argpar_item
*item
);
168 * Returns the index, within the non-option arguments, of the non-option
169 * parsing item `item`.
172 unsigned int argpar_item_non_opt_non_opt_index(const struct argpar_item
*item
);
175 * Destroys `item`, as created by argpar_iter_next().
178 void argpar_item_destroy(const struct argpar_item
*item
);
181 * Creates an argument parsing iterator.
183 * This function initializes the returned structure, but doesn't
184 * actually start parsing the arguments.
186 * `*argv` and `*descrs` must NOT change for the lifetime of the
187 * returned iterator (until you call argpar_iter_destroy()) and for the
188 * lifetime of any parsing item (until you call argpar_item_destroy())
189 * argpar_iter_next() creates for the returned iterator.
191 * Call argpar_iter_next() with the returned iterator to obtain the next
192 * parsing result (item).
195 struct argpar_iter
*argpar_iter_create(unsigned int argc
,
196 const char * const *argv
,
197 const struct argpar_opt_descr
*descrs
);
200 * Destroys `iter`, as returned by argpar_iter_create().
203 void argpar_iter_destroy(struct argpar_iter
*iter
);
206 * Return type of argpar_iter_next().
208 enum argpar_iter_next_status
{
209 ARGPAR_ITER_NEXT_STATUS_OK
,
210 ARGPAR_ITER_NEXT_STATUS_END
,
211 ARGPAR_ITER_NEXT_STATUS_ERROR_UNKNOWN_OPT
,
212 ARGPAR_ITER_NEXT_STATUS_ERROR_MISSING_OPT_ARG
,
213 ARGPAR_ITER_NEXT_STATUS_ERROR_UNEXPECTED_OPT_ARG
,
214 ARGPAR_ITER_NEXT_STATUS_ERROR_MEMORY
,
218 * Parses and returns the next item from `iter`.
220 * On success, this function:
222 * * Sets `*item` to a parsing item which describes the next option
223 * or non-option argument.
225 * Destroy `*item` with argpar_item_destroy().
227 * * Returns `ARGPAR_ITER_NEXT_STATUS_OK`.
229 * If there are no more items to return, this function returns
230 * `ARGPAR_ITER_NEXT_STATUS_END`.
232 * On failure, this function:
236 * `ARGPAR_ITER_NEXT_STATUS_ERROR_UNKNOWN_OPT`:
237 * Unknown option (not found in `descrs` as passed to
238 * argpar_iter_create() to create `iter`).
240 * `ARGPAR_ITER_NEXT_STATUS_ERROR_MISSING_OPT_ARG`:
241 * Missing option argument.
243 * `ARGPAR_ITER_NEXT_STATUS_ERROR_UNEXPECTED_OPT_ARG`:
244 * Unexpected option argument.
246 * `ARGPAR_ITER_NEXT_STATUS_ERROR_MEMORY`:
249 * * Except for the `ARGPAR_ITER_NEXT_STATUS_ERROR_MEMORY` status,
250 * sets `*error`, if not `NULL`, to a descriptive error string.
251 * Free `*error` with free().
253 enum argpar_iter_next_status
argpar_iter_next(
254 struct argpar_iter
*iter
, const struct argpar_item
**item
,
258 * Returns the number of ingested elements from `argv`, as passed to
259 * argpar_iter_create() to create `*iter`, that were required to produce
260 * the previously returned items.
263 unsigned int argpar_iter_ingested_orig_args(const struct argpar_iter
*iter
);
266 * Destroys `_item` (`const struct argpar_item *`) and sets it to
269 #define ARGPAR_ITEM_DESTROY_AND_RESET(_item) \
271 argpar_item_destroy(_item); \
275 #endif /* ARGPAR_ARGPAR_H */