1 | /** @file
|
---|
2 | * innotek Portable Runtime - Command Line Parsing.
|
---|
3 | */
|
---|
4 |
|
---|
5 | /*
|
---|
6 | * Copyright (C) 2007 innotek GmbH
|
---|
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 |
|
---|
33 | __BEGIN_DECLS
|
---|
34 |
|
---|
35 | /** @defgroup grp_rt_getopt RTGetOpt - Command Line Parsing
|
---|
36 | * @ingroup grp_rt
|
---|
37 | * @{
|
---|
38 | */
|
---|
39 |
|
---|
40 | /** @name RTOPTIONDEF::f
|
---|
41 | * @{ */
|
---|
42 |
|
---|
43 | /** Requires no extra argument.
|
---|
44 | * (Can be assumed to be 0 for ever.) */
|
---|
45 | #define RTGETOPT_REQ_NOTHING 0
|
---|
46 | /** A value is required or error will be returned. */
|
---|
47 | #define RTGETOPT_REQ_STRING 1
|
---|
48 | /** The value must be a valid signed 32-bit integer or an error will be returned. */
|
---|
49 | #define RTGETOPT_REQ_INT32 2
|
---|
50 | /** The value must be a valid signed 32-bit integer or an error will be returned. */
|
---|
51 | #define RTGETOPT_REQ_UINT32 3
|
---|
52 | /** The mask of the valid required types. */
|
---|
53 | #define RTGETOPT_REQ_MASK 3
|
---|
54 | /** @} */
|
---|
55 |
|
---|
56 | /**
|
---|
57 | * An option definition.
|
---|
58 | */
|
---|
59 | typedef struct RTOPTIONDEF
|
---|
60 | {
|
---|
61 | /** The long option.
|
---|
62 | * This is optional */
|
---|
63 | const char *pszLong;
|
---|
64 | /** The short option character.
|
---|
65 | * This doesn't have to be a character, it may also be a \#define or enum value if
|
---|
66 | * there isn't any short version of this option. */
|
---|
67 | int iShort;
|
---|
68 | /** The flags (RTGETOPT_*). */
|
---|
69 | unsigned fFlags;
|
---|
70 | } RTOPTIONDEF;
|
---|
71 | /** Pointer to an option definition. */
|
---|
72 | typedef RTOPTIONDEF *PRTOPTIONDEF;
|
---|
73 | /** Pointer to an const option definition. */
|
---|
74 | typedef const RTOPTIONDEF *PCRTOPTIONDEF;
|
---|
75 |
|
---|
76 | /**
|
---|
77 | * Option argument union.
|
---|
78 | *
|
---|
79 | * What ends up here depends on argument format in the option definition.
|
---|
80 | */
|
---|
81 | typedef union RTOPTIONUNION
|
---|
82 | {
|
---|
83 | /** Pointer to the definition on failure or when the option doesn't take an argument.
|
---|
84 | * This can be NULL for some errors. */
|
---|
85 | PCRTOPTIONDEF pDef;
|
---|
86 | /** A RTGETOPT_ARG_FORMAT_STRING option argument. */
|
---|
87 | const char *psz;
|
---|
88 | /** A RTGETOPT_ARG_FORMAT_INT32 option argument. */
|
---|
89 | int32_t i32;
|
---|
90 | /** A RTGETOPT_ARG_FORMAT_UINT32 option argument . */
|
---|
91 | uint32_t u32;
|
---|
92 | } RTOPTIONUNION;
|
---|
93 | /** Pointer to an option argument union. */
|
---|
94 | typedef RTOPTIONUNION *PRTOPTIONUNION;
|
---|
95 |
|
---|
96 |
|
---|
97 | /**
|
---|
98 | * Command line argument parser, handling both long and short options and checking
|
---|
99 | * argument formats, if desired.
|
---|
100 | *
|
---|
101 | * This is to be called in a loop until it returns 0 (meaning that all options
|
---|
102 | * were parsed) or a negative value (meaning that an error occured). The passed in
|
---|
103 | * argument vector is sorted into options and non-option arguments, such that when
|
---|
104 | * returning 0 the *piThis is the index of the first non-option argument.
|
---|
105 | *
|
---|
106 | * For example, for a program which takes the following options:
|
---|
107 | *
|
---|
108 | * --optwithstring (or -s) and a string argument;
|
---|
109 | * --optwithint (or -i) and a 32-bit signed integer argument;
|
---|
110 | * --verbose (or -v) with no arguments,
|
---|
111 | *
|
---|
112 | * code would look something like this:
|
---|
113 | *
|
---|
114 | * @code
|
---|
115 | * int main(int argc, char *argv[])
|
---|
116 | * {
|
---|
117 | * static const RTOPTIONDEF g_aOptions[] =
|
---|
118 | * {
|
---|
119 | * { "--optwithstring", 's', RTGETOPT_REQ_STRING },
|
---|
120 | * { "--optwithint", 'i', RTGETOPT_REQ_INT32 },
|
---|
121 | * { "--verbose", 'v', 0 },
|
---|
122 | * };
|
---|
123 | *
|
---|
124 | * int ch;
|
---|
125 | * int i = 1;
|
---|
126 | * RTOPTIONUNION ValueUnion;
|
---|
127 | * while ((ch = RTGetOpt(argc, argv, g_aOptions, RT_ELEMENTS(g_aOptions), &i, &ValueUnion)))
|
---|
128 | * {
|
---|
129 | * if (ch < 0) { .... error }
|
---|
130 | *
|
---|
131 | * // for options that require an argument, ValueUnion has received the value
|
---|
132 | * switch (ch)
|
---|
133 | * {
|
---|
134 | * case 's': // --optwithstring or -s
|
---|
135 | * // string argument, copy ValueUnion.psz
|
---|
136 | * break;
|
---|
137 | *
|
---|
138 | * case 'i': // --optwithint or -i
|
---|
139 | * // integer argument, copy ValueUnion.i32
|
---|
140 | * break;
|
---|
141 | *
|
---|
142 | * case 'v': // --verbose or -v
|
---|
143 | * g_fOptVerbose = true;
|
---|
144 | * break;
|
---|
145 | * }
|
---|
146 | * }
|
---|
147 | *
|
---|
148 | * while (i < argc)
|
---|
149 | * {
|
---|
150 | * //do stuff to argv[i].
|
---|
151 | * }
|
---|
152 | *
|
---|
153 | * return 0;
|
---|
154 | * }
|
---|
155 | * @endcode
|
---|
156 | *
|
---|
157 | * @param argc argument count, to be copied from what comes in with main().
|
---|
158 | * @param argv argument array, to be copied from what comes in with main().
|
---|
159 | * This may end up being modified by the option/argument sorting.
|
---|
160 | * @param paOptions array of RTOPTIONDEF structures, which must specify what options are understood by the program.
|
---|
161 | * @param cOptions number of array items passed in with paOptions.
|
---|
162 | * @param piThis address of stack counter used internally by RTGetOpt; value must be initialized to 1 before the first call!
|
---|
163 | * @param pValueUnion union with value; in the event of an error, psz member points to erroneous parameter; otherwise, for options
|
---|
164 | * that require an argument, this contains the value of that argument, depending on the type that is required.
|
---|
165 | */
|
---|
166 | RTDECL(int) RTGetOpt(int argc, char *argv[], PCRTOPTIONDEF paOptions, size_t cOptions, int *piThis, PRTOPTIONUNION pValueUnion);
|
---|
167 |
|
---|
168 | /** @} */
|
---|
169 |
|
---|
170 | __END_DECLS
|
---|
171 |
|
---|
172 | #endif
|
---|
173 |
|
---|