Add internal command-line argument parser API
[babeltrace.git] / src / argpar / argpar.h
1 #ifndef BABELTRACE_ARGPAR_H
2 #define BABELTRACE_ARGPAR_H
3
4 /*
5 * Copyright 2019 Philippe Proulx <pproulx@efficios.com>
6 *
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:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
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
23 * SOFTWARE.
24 */
25
26 #include <glib.h>
27 #include <stdbool.h>
28
29 #include "common/macros.h"
30
31 /* Sentinel for an option descriptor array */
32 #define BT_ARGPAR_OPT_DESCR_SENTINEL { -1, '\0', NULL, false }
33
34 /* Option descriptor */
35 struct bt_argpar_opt_descr {
36 /* Numeric ID for this option */
37 const int id;
38
39 /* Short option character, or `\0` */
40 const char short_name;
41
42 /* Long option name (without `--`), or `NULL` */
43 const char * const long_name;
44
45 /* True if this option has an argument */
46 const bool with_arg;
47 };
48
49 /* Item type */
50 enum bt_argpar_item_type {
51 /* Option */
52 BT_ARGPAR_ITEM_TYPE_OPT,
53
54 /* Non-option */
55 BT_ARGPAR_ITEM_TYPE_NON_OPT,
56 };
57
58 /* Base item */
59 struct bt_argpar_item {
60 enum bt_argpar_item_type type;
61 };
62
63 /* Option item */
64 struct bt_argpar_item_opt {
65 struct bt_argpar_item base;
66
67 /* Corresponding descriptor */
68 const struct bt_argpar_opt_descr *descr;
69
70 /* Argument, or `NULL` if none */
71 const char *arg;
72 };
73
74 /* Non-option item */
75 struct bt_argpar_item_non_opt {
76 struct bt_argpar_item base;
77
78 /*
79 * Complete argument, pointing to one of the entries of the
80 * original arguments (`argv`).
81 */
82 const char *arg;
83
84 /* Index of this argument amongst all original arguments (`argv`) */
85 unsigned int orig_index;
86
87 /* Index of this argument amongst other non-option arguments */
88 unsigned int non_opt_index;
89 };
90
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 */
94 GPtrArray *items;
95
96 /* Error string, or `NULL` if none */
97 GString *error;
98
99 /* Number of original arguments (`argv`) ingested */
100 unsigned int ingested_orig_args;
101 };
102
103 /*
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`.
107 *
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.
111 *
112 * This argument parser supports:
113 *
114 * * Short options without an argument, possibly tied together:
115 *
116 * -f -auf -n
117 *
118 * * Short options with argument:
119 *
120 * -b 45 -f/mein/file -xyzhello
121 *
122 * * Long options without an argument:
123 *
124 * --five-guys --burger-king --pizza-hut --subway
125 *
126 * * Long options with arguments:
127 *
128 * --security enable --time=18.56
129 *
130 * * Non-option arguments (anything else).
131 *
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.
140 *
141 * This function accepts duplicate options (the resulting array of items
142 * contains one entry for each instance).
143 *
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.
148 *
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
152 *
153 * --hello --meow=23 /path/to/file -b
154 *
155 * the function returns an array of four items: two options, one
156 * non-option, and one option.
157 *
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
164 *
165 * --great --white contact nuance --shark nuclear
166 *
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.
170 *
171 * This makes it possible to know where a command name is, for example.
172 * With those arguments:
173 *
174 * --verbose --stuff=23 do-something --specific-opt -f -b
175 *
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
181 * `&argv[3]`.
182 *
183 * Note that `ingested_orig_args` is not always equal to the number of
184 * returned items, as
185 *
186 * --hello -fdw
187 *
188 * for example contains two ingested original arguments, but four
189 * resulting items.
190 *
191 * On failure, the returned structure's `items` member is `NULL`, and
192 * the `error` string member contains details about the error.
193 *
194 * You can finalize the returned structure with
195 * bt_argpar_parse_ret_fini().
196 */
197 BT_HIDDEN
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);
202
203 /*
204 * Finalizes what is returned by bt_argpar_parse().
205 *
206 * It is safe to call bt_argpar_parse() multiple times with the same
207 * structure.
208 */
209 BT_HIDDEN
210 void bt_argpar_parse_ret_fini(struct bt_argpar_parse_ret *ret);
211
212 #endif /* BABELTRACE_ARGPAR_H */
This page took 0.035693 seconds and 4 git commands to generate.