1 #ifndef BABELTRACE_ARGPAR_H
2 #define BABELTRACE_ARGPAR_H
5 * Copyright 2019 Philippe Proulx <pproulx@efficios.com>
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 #include "common/macros.h"
31 /* Sentinel for an option descriptor array */
32 #define BT_ARGPAR_OPT_DESCR_SENTINEL { -1, '\0', NULL, false }
34 /* Option descriptor */
35 struct bt_argpar_opt_descr
{
36 /* Numeric ID for this option */
39 /* Short option character, or `\0` */
40 const char short_name
;
42 /* Long option name (without `--`), or `NULL` */
43 const char * const long_name
;
45 /* True if this option has an argument */
50 enum bt_argpar_item_type
{
52 BT_ARGPAR_ITEM_TYPE_OPT
,
55 BT_ARGPAR_ITEM_TYPE_NON_OPT
,
59 struct bt_argpar_item
{
60 enum bt_argpar_item_type type
;
64 struct bt_argpar_item_opt
{
65 struct bt_argpar_item base
;
67 /* Corresponding descriptor */
68 const struct bt_argpar_opt_descr
*descr
;
70 /* Argument, or `NULL` if none */
75 struct bt_argpar_item_non_opt
{
76 struct bt_argpar_item base
;
79 * Complete argument, pointing to one of the entries of the
80 * original arguments (`argv`).
84 /* Index of this argument amongst all original arguments (`argv`) */
85 unsigned int orig_index
;
87 /* Index of this argument amongst other non-option arguments */
88 unsigned int non_opt_index
;
91 /* What is returned by bt_argpar_parse() */
92 struct bt_argpar_parse_ret
{
93 /* Array of `struct bt_argpar_item *`, or `NULL` on error */
96 /* Error string, or `NULL` if none */
99 /* Number of original arguments (`argv`) ingested */
100 unsigned int ingested_orig_args
;
104 * Parses the arguments `argv` of which the count is `argc` using the
105 * sentinel-terminated (use `BT_ARGPAR_OPT_DESCR_SENTINEL`) option
106 * descriptor array `descrs`.
108 * This function considers ALL the elements of `argv`, including the
109 * first one, so that you would typically pass `argc - 1` and
110 * `&argv[1]` from what main() receives.
112 * This argument parser supports:
114 * * Short options without an argument, possibly tied together:
118 * * Short options with argument:
120 * -b 45 -f/mein/file -xyzhello
122 * * Long options without an argument:
124 * --five-guys --burger-king --pizza-hut --subway
126 * * Long options with arguments:
128 * --security enable --time=18.56
130 * * Non-option arguments (anything else).
132 * This function does not accept `-` or `--` as arguments. The latter
133 * means "end of options" for many command-line tools, but this function
134 * is all about keeping the order of the arguments, so it does not mean
135 * much to put them at the end. This has the side effect that a
136 * non-option argument cannot have the form of an option, for example if
137 * you need to pass the exact relative path `--component`. In that case,
138 * you would need to pass `./--component`. There's no generic way to
139 * escape `-` for the moment.
141 * This function accepts duplicate options (the resulting array of items
142 * contains one entry for each instance).
144 * On success, this function returns an array of items
145 * (`struct bt_argpar_item *`). Each item is to be casted to the
146 * appropriate type (`struct bt_argpar_item_opt *` or
147 * `struct bt_argpar_item_non_opt *`) depending on its type.
149 * The returned array contains the items in the same order that the
150 * arguments were parsed, including non-option arguments. This means,
151 * for example, that for
153 * --hello --meow=23 /path/to/file -b
155 * the function returns an array of four items: two options, one
156 * non-option, and one option.
158 * In the returned structure, `ingested_orig_args` is the number of
159 * ingested arguments within `argv` to produce the resulting array of
160 * items. If `fail_on_unknown_opt` is true, then on success
161 * `ingested_orig_args` is equal to `argc`. Otherwise,
162 * `ingested_orig_args` contains the number of original arguments until
163 * an unknown _option_ occurs. For example, with
165 * --great --white contact nuance --shark nuclear
167 * if `--shark` is not described within `descrs` and
168 * `fail_on_unknown_opt` is false, then `ingested_orig_args` is 4 (two
169 * options, two non-options), whereas `argc` is 6.
171 * This makes it possible to know where a command name is, for example.
172 * With those arguments:
174 * --verbose --stuff=23 do-something --specific-opt -f -b
176 * and the descriptors for `--verbose` and `--stuff` only, the function
177 * returns the `--verbose` and `--stuff` option items, the
178 * `do-something` non-option item, and that three original arguments
179 * were ingested. This means you can start the next argument parsing
180 * stage, with option descriptors depending on the command name, at
183 * Note that `ingested_orig_args` is not always equal to the number of
188 * for example contains two ingested original arguments, but four
191 * On failure, the returned structure's `items` member is `NULL`, and
192 * the `error` string member contains details about the error.
194 * You can finalize the returned structure with
195 * bt_argpar_parse_ret_fini().
198 struct bt_argpar_parse_ret
bt_argpar_parse(unsigned int argc
,
199 const char * const *argv
,
200 const struct bt_argpar_opt_descr
*descrs
,
201 bool fail_on_unknown_opt
);
204 * Finalizes what is returned by bt_argpar_parse().
206 * It is safe to call bt_argpar_parse() multiple times with the same
210 void bt_argpar_parse_ret_fini(struct bt_argpar_parse_ret
*ret
);
212 #endif /* BABELTRACE_ARGPAR_H */