Remove the argpar_parse() API
[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 *
4d6198b5
PP
17 * Create a parsing iterator with argpar_iter_create(), then
18 * repeatedly call argpar_iter_next() to access the parsing results,
19 * until one of:
fc07e526 20 *
4d6198b5 21 * * There are no more arguments.
fc07e526 22 *
4d6198b5
PP
23 * * The argument parser encounters an error (for example, an unknown
24 * option).
fc07e526 25 *
4d6198b5 26 * * You need to stop.
fc07e526 27 *
4d6198b5
PP
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`.
fc07e526
SM
31 *
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
34 * main() receives.
35 *
4d6198b5 36 * The argpar parser supports:
fc07e526
SM
37 *
38 * * Short options without an argument, possibly tied together:
39 *
40 * -f -auf -n
41 *
42 * * Short options with argument:
43 *
44 * -b 45 -f/mein/file -xyzhello
45 *
46 * * Long options without an argument:
47 *
48 * --five-guys --burger-king --pizza-hut --subway
49 *
50 * * Long options with arguments:
51 *
52 * --security enable --time=18.56
53 *
54 * * Non-option arguments (anything else).
55 *
4d6198b5 56 * The argpar parser parses `-` and `--` as non-option arguments. A
fc07e526
SM
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.
61 *
4d6198b5
PP
62 * argpar_iter_create() accepts duplicate options in `descrs` (it
63 * produces one item for each instance).
fc07e526 64 *
d4539a90
PP
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).
fc07e526 69 *
4d6198b5
PP
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:
fc07e526
SM
73 *
74 * --hello --count=23 /path/to/file -ab --type file magie
75 *
4d6198b5 76 * argpar_iter_next() produces the following items, in this order:
fc07e526
SM
77 *
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`).
85 */
86
903a5b8a 87/* Sentinel for an option descriptor array */
1c9a6bde 88#define ARGPAR_OPT_DESCR_SENTINEL { -1, '\0', NULL, false }
903a5b8a 89
7ac57709 90/*
fb12ac67
PP
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".
7ac57709 93 *
fc07e526
SM
94 * On Windows, symbols are local unless explicitly exported; see
95 * <https://gcc.gnu.org/wiki/Visibility>.
7ac57709
SM
96 */
97#if defined(_WIN32) || defined(__CYGWIN__)
fb12ac67 98# define ARGPAR_HIDDEN
7ac57709 99#else
fb12ac67 100# define ARGPAR_HIDDEN __attribute__((visibility("hidden")))
7ac57709
SM
101#endif
102
4d6198b5 103/* Forward-declaration for the opaque argpar iterator type */
fc07e526
SM
104struct argpar_iter;
105
903a5b8a 106/* Option descriptor */
1c9a6bde 107struct argpar_opt_descr {
903a5b8a
SM
108 /* Numeric ID for this option */
109 const int id;
110
111 /* Short option character, or `\0` */
112 const char short_name;
113
dd757a65 114 /* Long option name (without the `--` prefix), or `NULL` */
903a5b8a
SM
115 const char * const long_name;
116
117 /* True if this option has an argument */
118 const bool with_arg;
119};
120
121/* Item type */
1c9a6bde 122enum argpar_item_type {
903a5b8a 123 /* Option */
1c9a6bde 124 ARGPAR_ITEM_TYPE_OPT,
903a5b8a
SM
125
126 /* Non-option */
1c9a6bde 127 ARGPAR_ITEM_TYPE_NON_OPT,
903a5b8a
SM
128};
129
4d6198b5 130/* Forward-declaration for the opaque argpar parsing item type */
d4539a90 131struct argpar_item;
903a5b8a 132
d4539a90
PP
133/*
134 * Returns the type of the parsing item `item`.
135 */
136ARGPAR_HIDDEN
137enum argpar_item_type argpar_item_type(const struct argpar_item *item);
903a5b8a 138
d4539a90
PP
139/*
140 * Returns the option descriptor of the option parsing item `item`.
141 */
142ARGPAR_HIDDEN
143const struct argpar_opt_descr *argpar_item_opt_descr(
144 const struct argpar_item *item);
903a5b8a 145
d4539a90
PP
146/*
147 * Returns the argument of the option parsing item `item`, or `NULL` if
148 * none.
149 */
150ARGPAR_HIDDEN
151const char *argpar_item_opt_arg(const struct argpar_item *item);
903a5b8a 152
d4539a90
PP
153/*
154 * Returns the complete argument, pointing to one of the entries of the
155 * original arguments (`argv`), of the non-option parsing item `item`.
156 */
157ARGPAR_HIDDEN
158const char *argpar_item_non_opt_arg(const struct argpar_item *item);
903a5b8a 159
d4539a90
PP
160/*
161 * Returns the original index, within ALL the original arguments
162 * (`argv`), of the non-option parsing item `item`.
163 */
164ARGPAR_HIDDEN
165unsigned int argpar_item_non_opt_orig_index(const struct argpar_item *item);
903a5b8a 166
d4539a90
PP
167/*
168 * Returns the index, within the non-option arguments, of the non-option
169 * parsing item `item`.
170 */
171ARGPAR_HIDDEN
172unsigned int argpar_item_non_opt_non_opt_index(const struct argpar_item *item);
903a5b8a 173
a473f6cb
PP
174/*
175 * Destroys `item`, as created by argpar_iter_next().
176 */
177ARGPAR_HIDDEN
178void argpar_item_destroy(const struct argpar_item *item);
179
fc07e526
SM
180/*
181 * Creates an argument parsing iterator.
182 *
183 * This function initializes the returned structure, but doesn't
184 * actually start parsing the arguments.
185 *
186 * `*argv` and `*descrs` must NOT change for the lifetime of the
a2979011
PP
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.
fc07e526 190 *
2af370d0
PP
191 * Call argpar_iter_next() with the returned iterator to obtain the next
192 * parsing result (item).
fc07e526
SM
193 */
194ARGPAR_HIDDEN
195struct argpar_iter *argpar_iter_create(unsigned int argc,
196 const char * const *argv,
197 const struct argpar_opt_descr *descrs);
198
199/*
200 * Destroys `iter`, as returned by argpar_iter_create().
201 */
202ARGPAR_HIDDEN
203void argpar_iter_destroy(struct argpar_iter *iter);
204
205/*
2af370d0 206 * Return type of argpar_iter_next().
fc07e526 207 */
2af370d0
PP
208enum 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,
2af370d0
PP
213 ARGPAR_ITER_NEXT_STATUS_ERROR_UNEXPECTED_OPT_ARG,
214 ARGPAR_ITER_NEXT_STATUS_ERROR_MEMORY,
fc07e526
SM
215};
216
217/*
218 * Parses and returns the next item from `iter`.
219 *
d4d05805
PP
220 * On success, this function:
221 *
222 * * Sets `*item` to a parsing item which describes the next option
223 * or non-option argument.
224 *
225 * Destroy `*item` with argpar_item_destroy().
226 *
2af370d0 227 * * Returns `ARGPAR_ITER_NEXT_STATUS_OK`.
fc07e526
SM
228 *
229 * If there are no more items to return, this function returns
2af370d0 230 * `ARGPAR_ITER_NEXT_STATUS_END`.
fc07e526 231 *
d4d05805
PP
232 * On failure, this function:
233 *
234 * * Returns one of:
235 *
2af370d0 236 * `ARGPAR_ITER_NEXT_STATUS_ERROR_UNKNOWN_OPT`:
d4d05805
PP
237 * Unknown option (not found in `descrs` as passed to
238 * argpar_iter_create() to create `iter`).
239 *
2af370d0 240 * `ARGPAR_ITER_NEXT_STATUS_ERROR_MISSING_OPT_ARG`:
d4d05805
PP
241 * Missing option argument.
242 *
2af370d0 243 * `ARGPAR_ITER_NEXT_STATUS_ERROR_UNEXPECTED_OPT_ARG`:
d4d05805
PP
244 * Unexpected option argument.
245 *
2af370d0 246 * `ARGPAR_ITER_NEXT_STATUS_ERROR_MEMORY`:
d4d05805 247 * Memory error.
fc07e526 248 *
2af370d0 249 * * Except for the `ARGPAR_ITER_NEXT_STATUS_ERROR_MEMORY` status,
d4d05805
PP
250 * sets `*error`, if not `NULL`, to a descriptive error string.
251 * Free `*error` with free().
fc07e526 252 */
2af370d0 253enum argpar_iter_next_status argpar_iter_next(
fc07e526
SM
254 struct argpar_iter *iter, const struct argpar_item **item,
255 char **error);
256
257/*
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.
261 */
262ARGPAR_HIDDEN
f3ab5ca1 263unsigned int argpar_iter_ingested_orig_args(const struct argpar_iter *iter);
fc07e526 264
fc07e526
SM
265/*
266 * Destroys `_item` (`const struct argpar_item *`) and sets it to
267 * `NULL`.
268 */
fb12ac67
PP
269#define ARGPAR_ITEM_DESTROY_AND_RESET(_item) \
270 { \
271 argpar_item_destroy(_item); \
272 _item = NULL; \
fc07e526
SM
273 }
274
fe5a18f8 275#endif /* ARGPAR_ARGPAR_H */
This page took 0.056964 seconds and 4 git commands to generate.