VirtualBox

source: vbox/trunk/include/iprt/getopt.h@ 69617

Last change on this file since 69617 was 69105, checked in by vboxsync, 7 years ago

include/iprt/: (C) year

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette