1 | /** @file
|
---|
2 | * IPRT - Command Line Parsing.
|
---|
3 | */
|
---|
4 |
|
---|
5 | /*
|
---|
6 | * Copyright (C) 2007-2023 Oracle and/or its affiliates.
|
---|
7 | *
|
---|
8 | * This file is part of VirtualBox base platform packages, as
|
---|
9 | * available from https://www.virtualbox.org.
|
---|
10 | *
|
---|
11 | * This program is free software; you can redistribute it and/or
|
---|
12 | * modify it under the terms of the GNU General Public License
|
---|
13 | * as published by the Free Software Foundation, in version 3 of the
|
---|
14 | * License.
|
---|
15 | *
|
---|
16 | * This program is distributed in the hope that it will be useful, but
|
---|
17 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
19 | * General Public License for more details.
|
---|
20 | *
|
---|
21 | * You should have received a copy of the GNU General Public License
|
---|
22 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
23 | *
|
---|
24 | * The contents of this file may alternatively be used under the terms
|
---|
25 | * of the Common Development and Distribution License Version 1.0
|
---|
26 | * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
27 | * in the VirtualBox distribution, in which case the provisions of the
|
---|
28 | * CDDL are applicable instead of those of the GPL.
|
---|
29 | *
|
---|
30 | * You may elect to license modified versions of this file under the
|
---|
31 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
32 | *
|
---|
33 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
34 | */
|
---|
35 |
|
---|
36 | #ifndef IPRT_INCLUDED_getopt_h
|
---|
37 | #define IPRT_INCLUDED_getopt_h
|
---|
38 | #ifndef RT_WITHOUT_PRAGMA_ONCE
|
---|
39 | # pragma once
|
---|
40 | #endif
|
---|
41 |
|
---|
42 |
|
---|
43 | #include <iprt/cdefs.h>
|
---|
44 | #include <iprt/types.h>
|
---|
45 | #include <iprt/errcore.h> /* for VINF_GETOPT_NOT_OPTION */
|
---|
46 |
|
---|
47 | RT_C_DECLS_BEGIN
|
---|
48 |
|
---|
49 | /** @defgroup grp_rt_getopt RTGetOpt - Command Line Parsing
|
---|
50 | * @ingroup grp_rt
|
---|
51 | * @{
|
---|
52 | */
|
---|
53 |
|
---|
54 | /** @name Values for RTGETOPTDEF::fFlags and the fFlags parameter of
|
---|
55 | * RTGetOptFetchValue.
|
---|
56 | *
|
---|
57 | * @remarks When neither of the RTGETOPT_FLAG_HEX, RTGETOPT_FLAG_OCT and RTGETOPT_FLAG_DEC
|
---|
58 | * flags are specified with a integer value format, RTGetOpt will default to
|
---|
59 | * decimal but recognize the 0x prefix when present. RTGetOpt will not look for
|
---|
60 | * for the octal prefix (0).
|
---|
61 | * @{ */
|
---|
62 | /** Requires no extra argument.
|
---|
63 | * (Can be assumed to be 0 for ever.) */
|
---|
64 | #define RTGETOPT_REQ_NOTHING 0
|
---|
65 | /** A value is required or error will be returned. */
|
---|
66 | #define RTGETOPT_REQ_STRING 1
|
---|
67 | /** The value must be a valid signed 8-bit integer or an error will be returned. */
|
---|
68 | #define RTGETOPT_REQ_INT8 2
|
---|
69 | /** The value must be a valid unsigned 8-bit integer or an error will be returned. */
|
---|
70 | #define RTGETOPT_REQ_UINT8 3
|
---|
71 | /** The value must be a valid signed 16-bit integer or an error will be returned. */
|
---|
72 | #define RTGETOPT_REQ_INT16 4
|
---|
73 | /** The value must be a valid unsigned 16-bit integer or an error will be returned. */
|
---|
74 | #define RTGETOPT_REQ_UINT16 5
|
---|
75 | /** The value must be a valid signed 32-bit integer or an error will be returned. */
|
---|
76 | #define RTGETOPT_REQ_INT32 6
|
---|
77 | /** The value must be a valid unsigned 32-bit integer or an error will be returned. */
|
---|
78 | #define RTGETOPT_REQ_UINT32 7
|
---|
79 | /** The value must be a valid signed 64-bit integer or an error will be returned. */
|
---|
80 | #define RTGETOPT_REQ_INT64 8
|
---|
81 | /** The value must be a valid unsigned 64-bit integer or an error will be returned. */
|
---|
82 | #define RTGETOPT_REQ_UINT64 9
|
---|
83 | /** The value must be a valid IPv4 address.
|
---|
84 | * (Not a name, but 4 values in the 0..255 range with dots separating them). */
|
---|
85 | #define RTGETOPT_REQ_IPV4ADDR 10
|
---|
86 | /** The value must be a valid IPv4 CIDR.
|
---|
87 | * As with RTGETOPT_REQ_IPV4ADDR, no name.
|
---|
88 | */
|
---|
89 | #define RTGETOPT_REQ_IPV4CIDR 11
|
---|
90 | #if 0
|
---|
91 | /* take placers */
|
---|
92 | /** The value must be a valid IPv6 addr
|
---|
93 | * @todo: Add types and parsing routines in (iprt/net.h)
|
---|
94 | */
|
---|
95 | #define RTGETOPT_REQ_IPV6ADDR 12
|
---|
96 | /** The value must be a valid IPv6 CIDR
|
---|
97 | * @todo: Add types and parsing routines in (iprt/net.h)
|
---|
98 | */
|
---|
99 | #define RTGETOPT_REQ_IPV6CIDR 13
|
---|
100 | #endif
|
---|
101 | /** The value must be a valid ethernet MAC address. */
|
---|
102 | #define RTGETOPT_REQ_MACADDR 14
|
---|
103 | /** The value must be a valid UUID. */
|
---|
104 | #define RTGETOPT_REQ_UUID 15
|
---|
105 | /** The value must be a string with value as "on" or "off". */
|
---|
106 | #define RTGETOPT_REQ_BOOL_ONOFF 16
|
---|
107 | /** Boolean option accepting a wide range of typical ways of
|
---|
108 | * expression true and false. */
|
---|
109 | #define RTGETOPT_REQ_BOOL 17
|
---|
110 | /** The value must two unsigned 32-bit integer values separated by a colon,
|
---|
111 | * slash, pipe or space(s). */
|
---|
112 | #define RTGETOPT_REQ_UINT32_PAIR 18
|
---|
113 | /** The value must two unsigned 64-bit integer values separated by a colon,
|
---|
114 | * slash, pipe or space(s). */
|
---|
115 | #define RTGETOPT_REQ_UINT64_PAIR 19
|
---|
116 | /** The value must at least unsigned 32-bit integer value, optionally
|
---|
117 | * followed by a second separated by a colon, slash, pipe or space(s). */
|
---|
118 | #define RTGETOPT_REQ_UINT32_OPTIONAL_PAIR 20
|
---|
119 | /** The value must at least unsigned 64-bit integer value, optionally
|
---|
120 | * followed by a second separated by a colon, slash, pipe or space(s). */
|
---|
121 | #define RTGETOPT_REQ_UINT64_OPTIONAL_PAIR 21
|
---|
122 | /** The mask of the valid required types. */
|
---|
123 | #define RTGETOPT_REQ_MASK 31
|
---|
124 | /** Treat the value as hexadecimal - only applicable with the RTGETOPT_REQ_*INT*. */
|
---|
125 | #define RTGETOPT_FLAG_HEX RT_BIT(16)
|
---|
126 | /** Treat the value as octal - only applicable with the RTGETOPT_REQ_*INT*. */
|
---|
127 | #define RTGETOPT_FLAG_OCT RT_BIT(17)
|
---|
128 | /** Treat the value as decimal - only applicable with the RTGETOPT_REQ_*INT*. */
|
---|
129 | #define RTGETOPT_FLAG_DEC RT_BIT(18)
|
---|
130 | /** The index value is attached to the argument - only valid for long arguments. */
|
---|
131 | #define RTGETOPT_FLAG_INDEX RT_BIT(19)
|
---|
132 | /** Used with RTGETOPT_FLAG_INDEX, setting index to zero if none given.
|
---|
133 | * (The default is to fail with VERR_GETOPT_INDEX_MISSING.) */
|
---|
134 | #define RTGETOPT_FLAG_INDEX_DEF_0 RT_BIT(20)
|
---|
135 | /** Used with RTGETOPT_FLAG_INDEX, setting index to one if none given.
|
---|
136 | * (The default is to fail with VERR_GETOPT_INDEX_MISSING.) */
|
---|
137 | #define RTGETOPT_FLAG_INDEX_DEF_1 RT_BIT(21)
|
---|
138 | /** For simplicity. */
|
---|
139 | #define RTGETOPT_FLAG_INDEX_DEF_MASK (RT_BIT(20) | RT_BIT(21))
|
---|
140 | /** For simple conversion. */
|
---|
141 | #define RTGETOPT_FLAG_INDEX_DEF_SHIFT 20
|
---|
142 | /** For use with RTGETOPT_FLAG_INDEX_DEF_0 or RTGETOPT_FLAG_INDEX_DEF_1 to
|
---|
143 | * imply a dash before the index when a digit is specified.
|
---|
144 | * This is for transitioning from options without index to optionally allow
|
---|
145 | * index options, i.e. "--long" defaults to either index 1 or 1 using the above
|
---|
146 | * flags, while "--long-1" explicitly gives the index ("--long-" is not valid).
|
---|
147 | * This flag matches an "-" separating the "--long" string
|
---|
148 | * (RTGETOPTDEFS::pszLong) from the index value. */
|
---|
149 | #define RTGETOPT_FLAG_INDEX_DEF_DASH RT_BIT(22)
|
---|
150 | /** Treat the long option as case insensitive. */
|
---|
151 | #define RTGETOPT_FLAG_ICASE RT_BIT(23)
|
---|
152 | /** Mask of valid bits - for validation. */
|
---|
153 | #define RTGETOPT_VALID_MASK ( RTGETOPT_REQ_MASK \
|
---|
154 | | RTGETOPT_FLAG_HEX \
|
---|
155 | | RTGETOPT_FLAG_OCT \
|
---|
156 | | RTGETOPT_FLAG_DEC \
|
---|
157 | | RTGETOPT_FLAG_INDEX \
|
---|
158 | | RTGETOPT_FLAG_INDEX_DEF_0 \
|
---|
159 | | RTGETOPT_FLAG_INDEX_DEF_1 \
|
---|
160 | | RTGETOPT_FLAG_INDEX_DEF_DASH \
|
---|
161 | | RTGETOPT_FLAG_ICASE )
|
---|
162 | /** @} */
|
---|
163 |
|
---|
164 | /**
|
---|
165 | * An option definition.
|
---|
166 | */
|
---|
167 | typedef struct RTGETOPTDEF
|
---|
168 | {
|
---|
169 | /** The long option.
|
---|
170 | * This is optional */
|
---|
171 | const char *pszLong;
|
---|
172 | /** The short option character.
|
---|
173 | * This doesn't have to be a character, it may also be a \#define or enum value if
|
---|
174 | * there isn't any short version of this option. Must be greater than 0. */
|
---|
175 | int iShort;
|
---|
176 | /** The flags (RTGETOPT_*). */
|
---|
177 | unsigned fFlags;
|
---|
178 | } RTGETOPTDEF;
|
---|
179 | /** Pointer to an option definition. */
|
---|
180 | typedef RTGETOPTDEF *PRTGETOPTDEF;
|
---|
181 | /** Pointer to an const option definition. */
|
---|
182 | typedef const RTGETOPTDEF *PCRTGETOPTDEF;
|
---|
183 |
|
---|
184 | /**
|
---|
185 | * Option argument union.
|
---|
186 | *
|
---|
187 | * What ends up here depends on argument format in the option definition.
|
---|
188 | */
|
---|
189 | typedef union RTGETOPTUNION
|
---|
190 | {
|
---|
191 | /** Pointer to the definition on failure or when the option doesn't take an argument.
|
---|
192 | * This can be NULL for some errors. */
|
---|
193 | PCRTGETOPTDEF pDef;
|
---|
194 | /** A RTGETOPT_REQ_STRING option argument. */
|
---|
195 | const char *psz;
|
---|
196 |
|
---|
197 | /** A RTGETOPT_REQ_INT8 option argument. */
|
---|
198 | int8_t i8;
|
---|
199 | /** A RTGETOPT_REQ_UINT8 option argument . */
|
---|
200 | uint8_t u8;
|
---|
201 | /** A RTGETOPT_REQ_INT16 option argument. */
|
---|
202 | int16_t i16;
|
---|
203 | /** A RTGETOPT_REQ_UINT16 option argument . */
|
---|
204 | uint16_t u16;
|
---|
205 | /** A RTGETOPT_REQ_INT16 option argument. */
|
---|
206 | int32_t i32;
|
---|
207 | /** A RTGETOPT_REQ_UINT32 option argument . */
|
---|
208 | uint32_t u32;
|
---|
209 | /** A RTGETOPT_REQ_INT64 option argument. */
|
---|
210 | int64_t i64;
|
---|
211 | /** A RTGETOPT_REQ_UINT64 option argument. */
|
---|
212 | uint64_t u64;
|
---|
213 | #ifdef IPRT_INCLUDED_net_h
|
---|
214 | /** A RTGETOPT_REQ_IPV4ADDR option argument. */
|
---|
215 | RTNETADDRIPV4 IPv4Addr;
|
---|
216 | /** A RTGETOPT_REQ_IPV4CIDR option argument. */
|
---|
217 | struct
|
---|
218 | {
|
---|
219 | RTNETADDRIPV4 IPv4Network;
|
---|
220 | RTNETADDRIPV4 IPv4Netmask;
|
---|
221 | } CidrIPv4;
|
---|
222 | #endif
|
---|
223 | /** A RTGETOPT_REQ_MACADDR option argument. */
|
---|
224 | RTMAC MacAddr;
|
---|
225 | /** A RTGETOPT_REQ_UUID option argument. */
|
---|
226 | RTUUID Uuid;
|
---|
227 | /** A boolean flag. */
|
---|
228 | bool f;
|
---|
229 | /** A RTGETOPT_REQ_UINT32_PAIR or RTGETOPT_REQ_UINT32_OPTIONAL_PAIR option
|
---|
230 | * argument. */
|
---|
231 | struct
|
---|
232 | {
|
---|
233 | uint32_t uFirst;
|
---|
234 | uint32_t uSecond; /**< Set to UINT32_MAX if optional and not present. */
|
---|
235 | } PairU32;
|
---|
236 | /** A RTGETOPT_REQ_UINT64_COLON_PAIR option argument. */
|
---|
237 | struct
|
---|
238 | {
|
---|
239 | uint64_t uFirst;
|
---|
240 | uint64_t uSecond; /**< Set to UINT64_MAX if optional and not present. */
|
---|
241 | } PairU64;
|
---|
242 | } RTGETOPTUNION;
|
---|
243 | /** Pointer to an option argument union. */
|
---|
244 | typedef RTGETOPTUNION *PRTGETOPTUNION;
|
---|
245 | /** Pointer to a const option argument union. */
|
---|
246 | typedef RTGETOPTUNION const *PCRTGETOPTUNION;
|
---|
247 |
|
---|
248 |
|
---|
249 | /**
|
---|
250 | * RTGetOpt state.
|
---|
251 | */
|
---|
252 | typedef struct RTGETOPTSTATE
|
---|
253 | {
|
---|
254 | /** The next argument. */
|
---|
255 | int iNext;
|
---|
256 | /** Argument array. */
|
---|
257 | char **argv;
|
---|
258 | /** Number of items in argv. */
|
---|
259 | int argc;
|
---|
260 | /** Option definition array. */
|
---|
261 | PCRTGETOPTDEF paOptions;
|
---|
262 | /** Number of items in paOptions. */
|
---|
263 | size_t cOptions;
|
---|
264 | /** The next short option.
|
---|
265 | * (For parsing ls -latrT4 kind of option lists.) */
|
---|
266 | const char *pszNextShort;
|
---|
267 | /** The option definition which matched. NULL otherwise. */
|
---|
268 | PCRTGETOPTDEF pDef;
|
---|
269 | /** The index of an index option, otherwise UINT32_MAX. */
|
---|
270 | uint32_t uIndex;
|
---|
271 | /** The flags passed to RTGetOptInit. */
|
---|
272 | uint32_t fFlags;
|
---|
273 | /** Number of non-options that we're skipping during a sorted get. The value
|
---|
274 | * INT32_MAX is used to indicate that there are no more options. This is used
|
---|
275 | * to implement '--'. */
|
---|
276 | int32_t cNonOptions;
|
---|
277 |
|
---|
278 | /* More members may be added later for dealing with new features. */
|
---|
279 | } RTGETOPTSTATE;
|
---|
280 | /** Pointer to RTGetOpt state. */
|
---|
281 | typedef RTGETOPTSTATE *PRTGETOPTSTATE;
|
---|
282 |
|
---|
283 |
|
---|
284 | /**
|
---|
285 | * Initialize the RTGetOpt state.
|
---|
286 | *
|
---|
287 | * The passed in argument vector may be sorted if fFlags indicates that this is
|
---|
288 | * desired (to be implemented).
|
---|
289 | *
|
---|
290 | * @returns VINF_SUCCESS, VERR_INVALID_PARAMETER or VERR_INVALID_POINTER.
|
---|
291 | * @param pState The state.
|
---|
292 | *
|
---|
293 | * @param argc Argument count, to be copied from what comes in with
|
---|
294 | * main().
|
---|
295 | * @param argv Argument array, to be copied from what comes in with
|
---|
296 | * main(). This may end up being modified by the
|
---|
297 | * option/argument sorting.
|
---|
298 | * @param paOptions Array of RTGETOPTDEF structures, which must specify what
|
---|
299 | * options are understood by the program.
|
---|
300 | * @param cOptions Number of array items passed in with paOptions.
|
---|
301 | * @param iFirst The argument to start with (in argv).
|
---|
302 | * @param fFlags The flags, see RTGETOPTINIT_FLAGS_XXX.
|
---|
303 | */
|
---|
304 | RTDECL(int) RTGetOptInit(PRTGETOPTSTATE pState, int argc, char **argv,
|
---|
305 | PCRTGETOPTDEF paOptions, size_t cOptions,
|
---|
306 | int iFirst, uint32_t fFlags);
|
---|
307 |
|
---|
308 | /** @name RTGetOptInit flags.
|
---|
309 | * @{ */
|
---|
310 | /** Sort the arguments so that options comes first, then non-options. */
|
---|
311 | #define RTGETOPTINIT_FLAGS_OPTS_FIRST RT_BIT_32(0)
|
---|
312 | /** Prevent add the standard version and help options:
|
---|
313 | * - "--help", "-h" and "-?" returns 'h'.
|
---|
314 | * - "--version" and "-V" return 'V'.
|
---|
315 | */
|
---|
316 | #define RTGETOPTINIT_FLAGS_NO_STD_OPTS RT_BIT_32(1)
|
---|
317 | /** @} */
|
---|
318 |
|
---|
319 | /**
|
---|
320 | * Command line argument parser, handling both long and short options and checking
|
---|
321 | * argument formats, if desired.
|
---|
322 | *
|
---|
323 | * This is to be called in a loop until it returns 0 (meaning that all options
|
---|
324 | * were parsed) or a negative value (meaning that an error occurred). How non-option
|
---|
325 | * arguments are dealt with depends on the flags passed to RTGetOptInit. The default
|
---|
326 | * (fFlags = 0) is to return VINF_GETOPT_NOT_OPTION with pValueUnion->psz pointing to
|
---|
327 | * the argument string.
|
---|
328 | *
|
---|
329 | * For example, for a program which takes the following options:
|
---|
330 | *
|
---|
331 | * --optwithstring (or -s) and a string argument;
|
---|
332 | * --optwithint (or -i) and a 32-bit signed integer argument;
|
---|
333 | * --verbose (or -v) with no arguments,
|
---|
334 | *
|
---|
335 | * code would look something like this:
|
---|
336 | *
|
---|
337 | * @code
|
---|
338 | int main(int argc, char **argv)
|
---|
339 | {
|
---|
340 | int rc = RTR3Init();
|
---|
341 | if (RT_FAILURE(rc))
|
---|
342 | return RTMsgInitFailure(rc);
|
---|
343 |
|
---|
344 | static const RTGETOPTDEF s_aOptions[] =
|
---|
345 | {
|
---|
346 | { "--optwithstring", 's', RTGETOPT_REQ_STRING },
|
---|
347 | { "--optwithint", 'i', RTGETOPT_REQ_INT32 },
|
---|
348 | { "--verbose", 'v', 0 },
|
---|
349 | };
|
---|
350 |
|
---|
351 | int ch;
|
---|
352 | RTGETOPTUNION ValueUnion;
|
---|
353 | RTGETOPTSTATE GetState;
|
---|
354 | RTGetOptInit(&GetState, argc, argv, s_aOptions, RT_ELEMENTS(s_aOptions), 1, 0);
|
---|
355 | while ((ch = RTGetOpt(&GetState, &ValueUnion)))
|
---|
356 | {
|
---|
357 | // for options that require an argument, ValueUnion has received the value
|
---|
358 | switch (ch)
|
---|
359 | {
|
---|
360 | case 's': // --optwithstring or -s
|
---|
361 | // string argument, copy ValueUnion.psz
|
---|
362 | break;
|
---|
363 |
|
---|
364 | case 'i': // --optwithint or -i
|
---|
365 | // integer argument, copy ValueUnion.i32
|
---|
366 | break;
|
---|
367 |
|
---|
368 | case 'v': // --verbose or -v
|
---|
369 | g_fOptVerbose = true;
|
---|
370 | break;
|
---|
371 |
|
---|
372 | case VINF_GETOPT_NOT_OPTION:
|
---|
373 | // handle non-option argument in ValueUnion.psz.
|
---|
374 | break;
|
---|
375 |
|
---|
376 | default:
|
---|
377 | return RTGetOptPrintError(ch, &ValueUnion);
|
---|
378 | }
|
---|
379 | }
|
---|
380 |
|
---|
381 | return RTEXITCODE_SUCCESS;
|
---|
382 | }
|
---|
383 | @endcode
|
---|
384 | *
|
---|
385 | * @returns 0 when done parsing.
|
---|
386 | * @returns the iShort value of the option. pState->pDef points to the option
|
---|
387 | * definition which matched.
|
---|
388 | * @returns IPRT error status on parse error.
|
---|
389 | * @returns VINF_GETOPT_NOT_OPTION when encountering a non-option argument and
|
---|
390 | * RTGETOPTINIT_FLAGS_OPTS_FIRST was not specified. pValueUnion->psz
|
---|
391 | * points to the argument string.
|
---|
392 | * @returns VERR_GETOPT_UNKNOWN_OPTION when encountering an unknown option.
|
---|
393 | * pValueUnion->psz points to the option string.
|
---|
394 | * @returns VERR_GETOPT_REQUIRED_ARGUMENT_MISSING and pValueUnion->pDef if
|
---|
395 | * a required argument (aka value) was missing for an option.
|
---|
396 | * @returns VERR_GETOPT_INVALID_ARGUMENT_FORMAT and pValueUnion->pDef if
|
---|
397 | * argument (aka value) conversion failed.
|
---|
398 | *
|
---|
399 | * @param pState The state previously initialized with RTGetOptInit.
|
---|
400 | * @param pValueUnion Union with value; in the event of an error, psz member
|
---|
401 | * points to erroneous parameter; otherwise, for options
|
---|
402 | * that require an argument, this contains the value of
|
---|
403 | * that argument, depending on the type that is required.
|
---|
404 | */
|
---|
405 | RTDECL(int) RTGetOpt(PRTGETOPTSTATE pState, PRTGETOPTUNION pValueUnion);
|
---|
406 |
|
---|
407 | /**
|
---|
408 | * Fetch a value.
|
---|
409 | *
|
---|
410 | * Used to retrive a value argument in a manner similar to what RTGetOpt does
|
---|
411 | * (@a fFlags -> @a pValueUnion). This can be used when handling
|
---|
412 | * VINF_GETOPT_NOT_OPTION, but is equally useful for decoding options that
|
---|
413 | * takes more than one value.
|
---|
414 | *
|
---|
415 | * @returns VINF_SUCCESS on success.
|
---|
416 | * @returns IPRT error status on parse error.
|
---|
417 | * @returns VERR_INVALID_PARAMETER if the flags are wrong.
|
---|
418 | * @returns VERR_GETOPT_UNKNOWN_OPTION when pState->pDef is null.
|
---|
419 | * @returns VERR_GETOPT_REQUIRED_ARGUMENT_MISSING if there are no more
|
---|
420 | * available arguments. pValueUnion->pDef is NULL.
|
---|
421 | * @returns VERR_GETOPT_INVALID_ARGUMENT_FORMAT and pValueUnion->pDef is
|
---|
422 | * unchanged if value conversion failed.
|
---|
423 | *
|
---|
424 | * @param pState The state previously initialized with RTGetOptInit.
|
---|
425 | * @param pValueUnion Union with value; in the event of an error, psz member
|
---|
426 | * points to erroneous parameter; otherwise, for options
|
---|
427 | * that require an argument, this contains the value of
|
---|
428 | * that argument, depending on the type that is required.
|
---|
429 | * @param fFlags What to get, that is RTGETOPT_REQ_XXX.
|
---|
430 | */
|
---|
431 | RTDECL(int) RTGetOptFetchValue(PRTGETOPTSTATE pState, PRTGETOPTUNION pValueUnion, uint32_t fFlags);
|
---|
432 |
|
---|
433 | /**
|
---|
434 | * Gets the pointer to the argv entry of the current non-option argument.
|
---|
435 | *
|
---|
436 | * This function ASSUMES the previous RTGetOpt() call returned
|
---|
437 | * VINF_GETOPT_NOT_OPTION and require RTGETOPTINIT_FLAGS_OPTS_FIRST to be
|
---|
438 | * specified to RTGetOptInit().
|
---|
439 | *
|
---|
440 | * @returns Pointer to the argv entry of the current non-option. NULL if
|
---|
441 | * (detectable) precondition isn't fullfilled (asserted)
|
---|
442 | * @param pState The state previously initialized with RTGetOptInit.
|
---|
443 | */
|
---|
444 | RTDECL(char **) RTGetOptNonOptionArrayPtr(PRTGETOPTSTATE pState);
|
---|
445 |
|
---|
446 | /**
|
---|
447 | * Print error messages for a RTGetOpt default case.
|
---|
448 | *
|
---|
449 | * Uses RTMsgError.
|
---|
450 | *
|
---|
451 | * @returns Suitable exit code.
|
---|
452 | *
|
---|
453 | * @param ch The RTGetOpt return value.
|
---|
454 | * @param pValueUnion The value union returned by RTGetOpt.
|
---|
455 | */
|
---|
456 | RTDECL(RTEXITCODE) RTGetOptPrintError(int ch, PCRTGETOPTUNION pValueUnion);
|
---|
457 |
|
---|
458 | /**
|
---|
459 | * Formats error messages for a RTGetOpt default case.
|
---|
460 | *
|
---|
461 | * @returns On success, positive count of formatted character excluding the
|
---|
462 | * terminator. On buffer overflow, negative number giving the required
|
---|
463 | * buffer size (including terminator char). (RTStrPrintf2 style.)
|
---|
464 | *
|
---|
465 | * @param pszBuf The buffer to format into.
|
---|
466 | * @param cbBuf The size of the buffer @a pszBuf points to.
|
---|
467 | * @param ch The RTGetOpt return value.
|
---|
468 | * @param pValueUnion The value union returned by RTGetOpt.
|
---|
469 | */
|
---|
470 | RTDECL(ssize_t) RTGetOptFormatError(char *pszBuf, size_t cbBuf, int ch, PCRTGETOPTUNION pValueUnion);
|
---|
471 |
|
---|
472 | /**
|
---|
473 | * Parses the @a pszCmdLine string into an argv array.
|
---|
474 | *
|
---|
475 | * This is useful for converting a response file or similar to an argument
|
---|
476 | * vector that can be used with RTGetOptInit().
|
---|
477 | *
|
---|
478 | * This function aims at following the bourne shell string quoting rules.
|
---|
479 | *
|
---|
480 | * @returns IPRT status code.
|
---|
481 | *
|
---|
482 | * @param ppapszArgv Where to return the argument vector. This must be
|
---|
483 | * freed by calling RTGetOptArgvFreeEx or
|
---|
484 | * RTGetOptArgvFree.
|
---|
485 | * @param pcArgs Where to return the argument count.
|
---|
486 | * @param pszCmdLine The string to parse.
|
---|
487 | * @param fFlags A combination of the RTGETOPTARGV_CNV_XXX flags,
|
---|
488 | * except RTGETOPTARGV_CNV_UNQUOTED is not supported.
|
---|
489 | * @param pszSeparators String containing the argument separators. If NULL,
|
---|
490 | * then space, tab, line feed (\\n) and return (\\r)
|
---|
491 | * are used.
|
---|
492 | */
|
---|
493 | RTDECL(int) RTGetOptArgvFromString(char ***ppapszArgv, int *pcArgs, const char *pszCmdLine, uint32_t fFlags,
|
---|
494 | const char *pszSeparators);
|
---|
495 |
|
---|
496 | /**
|
---|
497 | * Frees and argument vector returned by RTGetOptStringToArgv.
|
---|
498 | *
|
---|
499 | * @param papszArgv Argument vector. NULL is fine.
|
---|
500 | */
|
---|
501 | RTDECL(void) RTGetOptArgvFree(char **papszArgv);
|
---|
502 |
|
---|
503 | /**
|
---|
504 | * Frees and argument vector returned by RTGetOptStringToArgv, taking
|
---|
505 | * RTGETOPTARGV_CNV_MODIFY_INPUT into account.
|
---|
506 | *
|
---|
507 | * @param papszArgv Argument vector. NULL is fine.
|
---|
508 | * @param fFlags The flags passed to RTGetOptStringToArgv.
|
---|
509 | */
|
---|
510 | RTDECL(void) RTGetOptArgvFreeEx(char **papszArgv, uint32_t fFlags);
|
---|
511 |
|
---|
512 | /**
|
---|
513 | * Turns an argv array into a command line string.
|
---|
514 | *
|
---|
515 | * This is useful for calling CreateProcess on Windows, but can also be used for
|
---|
516 | * displaying an argv array.
|
---|
517 | *
|
---|
518 | * This function aims at following the bourn shell string quoting rules.
|
---|
519 | *
|
---|
520 | * @returns IPRT status code.
|
---|
521 | *
|
---|
522 | * @param ppszCmdLine Where to return the command line string. This must
|
---|
523 | * be freed by calling RTStrFree.
|
---|
524 | * @param papszArgv The argument vector to convert.
|
---|
525 | * @param fFlags A combination of the RTGETOPTARGV_CNV_XXX flags.
|
---|
526 | */
|
---|
527 | RTDECL(int) RTGetOptArgvToString(char **ppszCmdLine, const char * const *papszArgv, uint32_t fFlags);
|
---|
528 |
|
---|
529 | /** @name RTGetOptArgvToString, RTGetOptArgvToUtf16String and
|
---|
530 | * RTGetOptArgvFromString flags
|
---|
531 | * @{ */
|
---|
532 | /** Quote strings according to the Microsoft CRT rules. */
|
---|
533 | #define RTGETOPTARGV_CNV_QUOTE_MS_CRT UINT32_C(0x00000000)
|
---|
534 | /** Quote strings according to the Unix Bourne Shell. */
|
---|
535 | #define RTGETOPTARGV_CNV_QUOTE_BOURNE_SH UINT32_C(0x00000001)
|
---|
536 | /** Don't quote any strings at all. */
|
---|
537 | #define RTGETOPTARGV_CNV_UNQUOTED UINT32_C(0x00000002)
|
---|
538 | /** Mask for the quoting style. */
|
---|
539 | #define RTGETOPTARGV_CNV_QUOTE_MASK UINT32_C(0x00000003)
|
---|
540 | /** Allow RTGetOptArgvFromString to modifying the command line input string.
|
---|
541 | * @note Must use RTGetOptArgvFreeEx to free. */
|
---|
542 | #define RTGETOPTARGV_CNV_MODIFY_INPUT UINT32_C(0x00000004)
|
---|
543 | /** Valid bits. */
|
---|
544 | #define RTGETOPTARGV_CNV_VALID_MASK UINT32_C(0x00000007)
|
---|
545 | /** @} */
|
---|
546 |
|
---|
547 | /**
|
---|
548 | * Convenience wrapper around RTGetOpArgvToString and RTStrToUtf16.
|
---|
549 | *
|
---|
550 | * @returns IPRT status code.
|
---|
551 | *
|
---|
552 | * @param ppwszCmdLine Where to return the command line string. This must
|
---|
553 | * be freed by calling RTUtf16Free.
|
---|
554 | * @param papszArgv The argument vector to convert.
|
---|
555 | * @param fFlags A combination of the RTGETOPTARGV_CNV_XXX flags.
|
---|
556 | */
|
---|
557 | RTDECL(int) RTGetOptArgvToUtf16String(PRTUTF16 *ppwszCmdLine, const char * const *papszArgv, uint32_t fFlags);
|
---|
558 |
|
---|
559 | /** @} */
|
---|
560 |
|
---|
561 | RT_C_DECLS_END
|
---|
562 |
|
---|
563 | #endif /* !IPRT_INCLUDED_getopt_h */
|
---|
564 |
|
---|