2 * SPDX-License-Identifier: MIT
4 * Copyright 2019 Philippe Proulx <pproulx@efficios.com>
7 #ifndef BABELTRACE_ARGPAR_H
8 #define BABELTRACE_ARGPAR_H
12 /* Sentinel for an option descriptor array */
13 #define ARGPAR_OPT_DESCR_SENTINEL { -1, '\0', NULL, false }
16 * ARGPAR_HIDDEN: if argpar is used in some shared library, we don't want them
17 * to be exported by that library, so mark them as "hidden".
19 * On Windows, symbols are local unless explicitly exported,
20 * see https://gcc.gnu.org/wiki/Visibility
22 #if defined(_WIN32) || defined(__CYGWIN__)
25 #define ARGPAR_HIDDEN __attribute__((visibility("hidden")))
28 /* Option descriptor */
29 struct argpar_opt_descr
{
30 /* Numeric ID for this option */
33 /* Short option character, or `\0` */
34 const char short_name
;
36 /* Long option name (without `--`), or `NULL` */
37 const char * const long_name
;
39 /* True if this option has an argument */
44 enum argpar_item_type
{
49 ARGPAR_ITEM_TYPE_NON_OPT
,
54 enum argpar_item_type type
;
58 struct argpar_item_opt
{
59 struct argpar_item base
;
61 /* Corresponding descriptor */
62 const struct argpar_opt_descr
*descr
;
64 /* Argument, or `NULL` if none */
69 struct argpar_item_non_opt
{
70 struct argpar_item base
;
73 * Complete argument, pointing to one of the entries of the
74 * original arguments (`argv`).
78 /* Index of this argument amongst all original arguments (`argv`) */
79 unsigned int orig_index
;
81 /* Index of this argument amongst other non-option arguments */
82 unsigned int non_opt_index
;
85 struct argpar_item_array
{
86 /* Array of `struct argpar_item *`, or `NULL` on error */
87 struct argpar_item
**items
;
89 /* Number of used slots in `items`. */
92 /* Number of allocated slots in `items`. */
96 /* What is returned by argpar_parse() */
97 struct argpar_parse_ret
{
98 /* Array of `struct argpar_item *`, or `NULL` on error */
99 struct argpar_item_array
*items
;
101 /* Error string, or `NULL` if none */
104 /* Number of original arguments (`argv`) ingested */
105 unsigned int ingested_orig_args
;
109 * Parses the arguments `argv` of which the count is `argc` using the
110 * sentinel-terminated (use `ARGPAR_OPT_DESCR_SENTINEL`) option
111 * descriptor array `descrs`.
113 * This function considers ALL the elements of `argv`, including the
114 * first one, so that you would typically pass `argc - 1` and
115 * `&argv[1]` from what main() receives.
117 * This argument parser supports:
119 * * Short options without an argument, possibly tied together:
123 * * Short options with argument:
125 * -b 45 -f/mein/file -xyzhello
127 * * Long options without an argument:
129 * --five-guys --burger-king --pizza-hut --subway
131 * * Long options with arguments:
133 * --security enable --time=18.56
135 * * Non-option arguments (anything else).
137 * This function does not accept `-` or `--` as arguments. The latter
138 * means "end of options" for many command-line tools, but this function
139 * is all about keeping the order of the arguments, so it does not mean
140 * much to put them at the end. This has the side effect that a
141 * non-option argument cannot have the form of an option, for example if
142 * you need to pass the exact relative path `--component`. In that case,
143 * you would need to pass `./--component`. There's no generic way to
144 * escape `-` for the moment.
146 * This function accepts duplicate options (the resulting array of items
147 * contains one entry for each instance).
149 * On success, this function returns an array of items
150 * (`struct argpar_item *`). Each item is to be casted to the
151 * appropriate type (`struct argpar_item_opt *` or
152 * `struct argpar_item_non_opt *`) depending on its type.
154 * The returned array contains the items in the same order that the
155 * arguments were parsed, including non-option arguments. This means,
156 * for example, that for
158 * --hello --meow=23 /path/to/file -b
160 * the function returns an array of four items: two options, one
161 * non-option, and one option.
163 * In the returned structure, `ingested_orig_args` is the number of
164 * ingested arguments within `argv` to produce the resulting array of
165 * items. If `fail_on_unknown_opt` is true, then on success
166 * `ingested_orig_args` is equal to `argc`. Otherwise,
167 * `ingested_orig_args` contains the number of original arguments until
168 * an unknown _option_ occurs. For example, with
170 * --great --white contact nuance --shark nuclear
172 * if `--shark` is not described within `descrs` and
173 * `fail_on_unknown_opt` is false, then `ingested_orig_args` is 4 (two
174 * options, two non-options), whereas `argc` is 6.
176 * This makes it possible to know where a command name is, for example.
177 * With those arguments:
179 * --verbose --stuff=23 do-something --specific-opt -f -b
181 * and the descriptors for `--verbose` and `--stuff` only, the function
182 * returns the `--verbose` and `--stuff` option items, the
183 * `do-something` non-option item, and that three original arguments
184 * were ingested. This means you can start the next argument parsing
185 * stage, with option descriptors depending on the command name, at
188 * Note that `ingested_orig_args` is not always equal to the number of
193 * for example contains two ingested original arguments, but four
196 * On failure, the returned structure's `items` member is `NULL`, and
197 * the `error` string member contains details about the error.
199 * You can finalize the returned structure with
200 * argpar_parse_ret_fini().
203 struct argpar_parse_ret
argpar_parse(unsigned int argc
,
204 const char * const *argv
,
205 const struct argpar_opt_descr
*descrs
,
206 bool fail_on_unknown_opt
);
209 * Finalizes what is returned by argpar_parse().
211 * It is safe to call argpar_parse() multiple times with the same
215 void argpar_parse_ret_fini(struct argpar_parse_ret
*ret
);
217 #endif /* BABELTRACE_ARGPAR_H */
This page took 0.034255 seconds and 4 git commands to generate.