Move to kernel style SPDX license identifiers
[babeltrace.git] / src / argpar / argpar.h
CommitLineData
fac21c87 1/*
0235b0db 2 * SPDX-License-Identifier: MIT
fac21c87 3 *
0235b0db 4 * Copyright 2019 Philippe Proulx <pproulx@efficios.com>
fac21c87
PP
5 */
6
0235b0db
MJ
7#ifndef BABELTRACE_ARGPAR_H
8#define BABELTRACE_ARGPAR_H
9
fac21c87
PP
10#include <stdbool.h>
11
fac21c87 12/* Sentinel for an option descriptor array */
65966041
SM
13#define ARGPAR_OPT_DESCR_SENTINEL { -1, '\0', NULL, false }
14
15/*
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".
18 *
19 * On Windows, symbols are local unless explicitly exported,
20 * see https://gcc.gnu.org/wiki/Visibility
21 */
22#if defined(_WIN32) || defined(__CYGWIN__)
23#define ARGPAR_HIDDEN
24#else
25#define ARGPAR_HIDDEN __attribute__((visibility("hidden")))
26#endif
fac21c87
PP
27
28/* Option descriptor */
65966041 29struct argpar_opt_descr {
fac21c87
PP
30 /* Numeric ID for this option */
31 const int id;
32
33 /* Short option character, or `\0` */
34 const char short_name;
35
36 /* Long option name (without `--`), or `NULL` */
37 const char * const long_name;
38
39 /* True if this option has an argument */
40 const bool with_arg;
41};
42
43/* Item type */
65966041 44enum argpar_item_type {
fac21c87 45 /* Option */
65966041 46 ARGPAR_ITEM_TYPE_OPT,
fac21c87
PP
47
48 /* Non-option */
65966041 49 ARGPAR_ITEM_TYPE_NON_OPT,
fac21c87
PP
50};
51
52/* Base item */
65966041
SM
53struct argpar_item {
54 enum argpar_item_type type;
fac21c87
PP
55};
56
57/* Option item */
65966041
SM
58struct argpar_item_opt {
59 struct argpar_item base;
fac21c87
PP
60
61 /* Corresponding descriptor */
65966041 62 const struct argpar_opt_descr *descr;
fac21c87
PP
63
64 /* Argument, or `NULL` if none */
65 const char *arg;
66};
67
68/* Non-option item */
65966041
SM
69struct argpar_item_non_opt {
70 struct argpar_item base;
fac21c87
PP
71
72 /*
73 * Complete argument, pointing to one of the entries of the
74 * original arguments (`argv`).
75 */
76 const char *arg;
77
78 /* Index of this argument amongst all original arguments (`argv`) */
79 unsigned int orig_index;
80
81 /* Index of this argument amongst other non-option arguments */
82 unsigned int non_opt_index;
83};
84
65966041
SM
85struct argpar_item_array {
86 /* Array of `struct argpar_item *`, or `NULL` on error */
87 struct argpar_item **items;
88
89 /* Number of used slots in `items`. */
90 unsigned int n_items;
91
92 /* Number of allocated slots in `items`. */
93 unsigned int n_alloc;
94};
95
96/* What is returned by argpar_parse() */
97struct argpar_parse_ret {
98 /* Array of `struct argpar_item *`, or `NULL` on error */
99 struct argpar_item_array *items;
fac21c87
PP
100
101 /* Error string, or `NULL` if none */
65966041 102 char *error;
fac21c87
PP
103
104 /* Number of original arguments (`argv`) ingested */
105 unsigned int ingested_orig_args;
106};
107
108/*
109 * Parses the arguments `argv` of which the count is `argc` using the
65966041 110 * sentinel-terminated (use `ARGPAR_OPT_DESCR_SENTINEL`) option
fac21c87
PP
111 * descriptor array `descrs`.
112 *
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.
116 *
117 * This argument parser supports:
118 *
119 * * Short options without an argument, possibly tied together:
120 *
121 * -f -auf -n
122 *
123 * * Short options with argument:
124 *
125 * -b 45 -f/mein/file -xyzhello
126 *
127 * * Long options without an argument:
128 *
129 * --five-guys --burger-king --pizza-hut --subway
130 *
131 * * Long options with arguments:
132 *
133 * --security enable --time=18.56
134 *
135 * * Non-option arguments (anything else).
136 *
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.
145 *
146 * This function accepts duplicate options (the resulting array of items
147 * contains one entry for each instance).
148 *
149 * On success, this function returns an array of items
65966041
SM
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.
fac21c87
PP
153 *
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
157 *
158 * --hello --meow=23 /path/to/file -b
159 *
160 * the function returns an array of four items: two options, one
161 * non-option, and one option.
162 *
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
169 *
170 * --great --white contact nuance --shark nuclear
171 *
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.
175 *
176 * This makes it possible to know where a command name is, for example.
177 * With those arguments:
178 *
179 * --verbose --stuff=23 do-something --specific-opt -f -b
180 *
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
186 * `&argv[3]`.
187 *
188 * Note that `ingested_orig_args` is not always equal to the number of
189 * returned items, as
190 *
191 * --hello -fdw
192 *
193 * for example contains two ingested original arguments, but four
194 * resulting items.
195 *
196 * On failure, the returned structure's `items` member is `NULL`, and
197 * the `error` string member contains details about the error.
198 *
199 * You can finalize the returned structure with
65966041 200 * argpar_parse_ret_fini().
fac21c87 201 */
65966041
SM
202ARGPAR_HIDDEN
203struct argpar_parse_ret argpar_parse(unsigned int argc,
fac21c87 204 const char * const *argv,
65966041 205 const struct argpar_opt_descr *descrs,
fac21c87
PP
206 bool fail_on_unknown_opt);
207
208/*
65966041 209 * Finalizes what is returned by argpar_parse().
fac21c87 210 *
65966041 211 * It is safe to call argpar_parse() multiple times with the same
fac21c87
PP
212 * structure.
213 */
65966041
SM
214ARGPAR_HIDDEN
215void argpar_parse_ret_fini(struct argpar_parse_ret *ret);
fac21c87
PP
216
217#endif /* BABELTRACE_ARGPAR_H */
This page took 0.048593 seconds and 4 git commands to generate.