1 | /** @file
|
---|
2 | * IPRT - Command Line Parsing.
|
---|
3 | */
|
---|
4 |
|
---|
5 | /*
|
---|
6 | * Copyright (C) 2007 Sun Microsystems, Inc.
|
---|
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 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
26 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
27 | * additional information or have any questions.
|
---|
28 | */
|
---|
29 |
|
---|
30 | #ifndef ___iprt_getopt_h
|
---|
31 | #define ___iprt_getopt_h
|
---|
32 |
|
---|
33 |
|
---|
34 | #include <iprt/cdefs.h>
|
---|
35 | #include <iprt/types.h>
|
---|
36 |
|
---|
37 | __BEGIN_DECLS
|
---|
38 |
|
---|
39 | /** @defgroup grp_rt_getopt RTGetOpt - Command Line Parsing
|
---|
40 | * @ingroup grp_rt
|
---|
41 | * @{
|
---|
42 | */
|
---|
43 |
|
---|
44 | /** @name RTGETOPTDEF::fFlags
|
---|
45 | *
|
---|
46 | * @remarks When neither of the RTGETOPT_FLAG_HEX, RTGETOPT_FLAG_OCT and RTGETOPT_FLAG_DEC
|
---|
47 | * flags are specified with a integer value format, RTGetOpt will default to
|
---|
48 | * decimal but recognize the 0x prefix when present. RTGetOpt will not look for
|
---|
49 | * for the octal prefix (0).
|
---|
50 | * @{ */
|
---|
51 | /** Requires no extra argument.
|
---|
52 | * (Can be assumed to be 0 for ever.) */
|
---|
53 | #define RTGETOPT_REQ_NOTHING 0
|
---|
54 | /** A value is required or error will be returned. */
|
---|
55 | #define RTGETOPT_REQ_STRING 1
|
---|
56 | /** The value must be a valid signed 8-bit integer or an error will be returned. */
|
---|
57 | #define RTGETOPT_REQ_INT8 2
|
---|
58 | /** The value must be a valid unsigned 8-bit integer or an error will be returned. */
|
---|
59 | #define RTGETOPT_REQ_UINT8 3
|
---|
60 | /** The value must be a valid signed 16-bit integer or an error will be returned. */
|
---|
61 | #define RTGETOPT_REQ_INT16 4
|
---|
62 | /** The value must be a valid unsigned 16-bit integer or an error will be returned. */
|
---|
63 | #define RTGETOPT_REQ_UINT16 5
|
---|
64 | /** The value must be a valid signed 32-bit integer or an error will be returned. */
|
---|
65 | #define RTGETOPT_REQ_INT32 6
|
---|
66 | /** The value must be a valid unsigned 32-bit integer or an error will be returned. */
|
---|
67 | #define RTGETOPT_REQ_UINT32 7
|
---|
68 | /** The value must be a valid signed 64-bit integer or an error will be returned. */
|
---|
69 | #define RTGETOPT_REQ_INT64 8
|
---|
70 | /** The value must be a valid unsigned 64-bit integer or an error will be returned. */
|
---|
71 | #define RTGETOPT_REQ_UINT64 9
|
---|
72 | /** The mask of the valid required types. */
|
---|
73 | #define RTGETOPT_REQ_MASK 15
|
---|
74 | /** Treat the value as hexadecimal - only applicable with the RTGETOPT_REQ_*INT*. */
|
---|
75 | #define RTGETOPT_FLAG_HEX RT_BIT(16)
|
---|
76 | /** Treat the value as octal - only applicable with the RTGETOPT_REQ_*INT*. */
|
---|
77 | #define RTGETOPT_FLAG_OCT RT_BIT(17)
|
---|
78 | /** Treat the value as decimal - only applicable with the RTGETOPT_REQ_*INT*. */
|
---|
79 | #define RTGETOPT_FLAG_DEC RT_BIT(18)
|
---|
80 | /** Mask of valid bits - for validation. */
|
---|
81 | #define RTGETOPT_VALID_MASK ( RTGETOPT_REQ_MASK | RTGETOPT_FLAG_HEX | RTGETOPT_FLAG_OCT | RTGETOPT_FLAG_DEC )
|
---|
82 | /** @} */
|
---|
83 |
|
---|
84 | /**
|
---|
85 | * An option definition.
|
---|
86 | */
|
---|
87 | typedef struct RTGETOPTDEF
|
---|
88 | {
|
---|
89 | /** The long option.
|
---|
90 | * This is optional */
|
---|
91 | const char *pszLong;
|
---|
92 | /** The short option character.
|
---|
93 | * This doesn't have to be a character, it may also be a \#define or enum value if
|
---|
94 | * there isn't any short version of this option. */
|
---|
95 | int iShort;
|
---|
96 | /** The flags (RTGETOPT_*). */
|
---|
97 | unsigned fFlags;
|
---|
98 | } RTGETOPTDEF;
|
---|
99 | /** Pointer to an option definition. */
|
---|
100 | typedef RTGETOPTDEF *PRTGETOPTDEF;
|
---|
101 | /** Pointer to an const option definition. */
|
---|
102 | typedef const RTGETOPTDEF *PCRTGETOPTDEF;
|
---|
103 |
|
---|
104 | /**
|
---|
105 | * Option argument union.
|
---|
106 | *
|
---|
107 | * What ends up here depends on argument format in the option definition.
|
---|
108 | *
|
---|
109 | * @remarks Integers will bet put in the \a i and \a u members and sign/zero extended
|
---|
110 | * according to the signedness indicated by the \a fFlags. So, you can choose
|
---|
111 | * use which ever of the integer members for accessing the value regardless
|
---|
112 | * of restrictions indicated in the \a fFlags.
|
---|
113 | */
|
---|
114 | typedef union RTGETOPTUNION
|
---|
115 | {
|
---|
116 | /** Pointer to the definition on failure or when the option doesn't take an argument.
|
---|
117 | * This can be NULL for some errors. */
|
---|
118 | PCRTGETOPTDEF pDef;
|
---|
119 | /** A RTGETOPT_ARG_FORMAT_STRING option argument. */
|
---|
120 | const char *psz;
|
---|
121 |
|
---|
122 | #if !defined(RT_ARCH_AMD64) && !defined(RT_ARCH_X86)
|
---|
123 | # error "PORTME: big-endian systems will need to fix the layout here to get the next two fields working right"
|
---|
124 | #endif
|
---|
125 |
|
---|
126 | /** A RTGETOPT_ARG_FORMAT_INT8 option argument. */
|
---|
127 | int8_t i8;
|
---|
128 | /** A RTGETOPT_ARG_FORMAT_UINT8 option argument . */
|
---|
129 | uint8_t u8;
|
---|
130 | /** A RTGETOPT_ARG_FORMAT_INT16 option argument. */
|
---|
131 | int16_t i16;
|
---|
132 | /** A RTGETOPT_ARG_FORMAT_UINT16 option argument . */
|
---|
133 | uint16_t u16;
|
---|
134 | /** A RTGETOPT_ARG_FORMAT_INT16 option argument. */
|
---|
135 | int32_t i32;
|
---|
136 | /** A RTGETOPT_ARG_FORMAT_UINT32 option argument . */
|
---|
137 | uint32_t u32;
|
---|
138 | /** A RTGETOPT_ARG_FORMAT_INT64 option argument. */
|
---|
139 | int64_t i64;
|
---|
140 | /** A RTGETOPT_ARG_FORMAT_UINT64 option argument. */
|
---|
141 | uint64_t u64;
|
---|
142 | /** A signed integer value. */
|
---|
143 | int64_t i;
|
---|
144 | /** An unsigned integer value. */
|
---|
145 | uint64_t u;
|
---|
146 | } RTGETOPTUNION;
|
---|
147 | /** Pointer to an option argument union. */
|
---|
148 | typedef RTGETOPTUNION *PRTGETOPTUNION;
|
---|
149 | /** Pointer to a const option argument union. */
|
---|
150 | typedef RTGETOPTUNION const *PCRTGETOPTUNION;
|
---|
151 |
|
---|
152 |
|
---|
153 | /**
|
---|
154 | * RTGetOpt state.
|
---|
155 | */
|
---|
156 | typedef struct RTGETOPTSTATE
|
---|
157 | {
|
---|
158 | /** The next argument. */
|
---|
159 | int iNext;
|
---|
160 | /** Argument array. */
|
---|
161 | char **argv;
|
---|
162 | /** Number of items in argv. */
|
---|
163 | int argc;
|
---|
164 | /** Option definition array. */
|
---|
165 | PCRTGETOPTDEF paOptions;
|
---|
166 | /** Number of items in paOptions. */
|
---|
167 | size_t cOptions;
|
---|
168 | /** The next short option.
|
---|
169 | * (For parsing ls -latrT4 kind of option lists.) */
|
---|
170 | const char *pszNextShort;
|
---|
171 | /* More members will be added later for dealing with initial
|
---|
172 | call, optional sorting, '--' and so on. */
|
---|
173 | } RTGETOPTSTATE;
|
---|
174 | /** Pointer to RTGetOpt state. */
|
---|
175 | typedef RTGETOPTSTATE *PRTGETOPTSTATE;
|
---|
176 |
|
---|
177 |
|
---|
178 | /**
|
---|
179 | * Initialize the RTGetOpt state.
|
---|
180 | *
|
---|
181 | * The passed in argument vector may be sorted if fFlags indicates that this is
|
---|
182 | * desired (to be implemented).
|
---|
183 | *
|
---|
184 | * @returns VINF_SUCCESS, VERR_INVALID_PARAMETER or VERR_INVALID_POINTER.
|
---|
185 | * @param pState The state.
|
---|
186 | *
|
---|
187 | * @param argc Argument count, to be copied from what comes in with
|
---|
188 | * main().
|
---|
189 | * @param argv Argument array, to be copied from what comes in with
|
---|
190 | * main(). This may end up being modified by the
|
---|
191 | * option/argument sorting.
|
---|
192 | * @param paOptions Array of RTGETOPTDEF structures, which must specify what
|
---|
193 | * options are understood by the program.
|
---|
194 | * @param cOptions Number of array items passed in with paOptions.
|
---|
195 | * @param iFirst The argument to start with (in argv).
|
---|
196 | * @param fFlags The flags. MBZ for now.
|
---|
197 | */
|
---|
198 | RTDECL(int) RTGetOptInit(PRTGETOPTSTATE pState, int argc, char **argv,
|
---|
199 | PCRTGETOPTDEF paOptions, size_t cOptions,
|
---|
200 | int iFirst, uint32_t fFlags);
|
---|
201 |
|
---|
202 | /**
|
---|
203 | * Command line argument parser, handling both long and short options and checking
|
---|
204 | * argument formats, if desired.
|
---|
205 | *
|
---|
206 | * This is to be called in a loop until it returns 0 (meaning that all options
|
---|
207 | * were parsed) or a negative value (meaning that an error occured). How non-option
|
---|
208 | * arguments are dealt with depends on the flags passed to RTGetOptInit. The default
|
---|
209 | * (fFlags = 0) is to return VINF_GETOPT_NOT_OPTION with pValueUnion->psz pointing to
|
---|
210 | * the argument string.
|
---|
211 | *
|
---|
212 | * For example, for a program which takes the following options:
|
---|
213 | *
|
---|
214 | * --optwithstring (or -s) and a string argument;
|
---|
215 | * --optwithint (or -i) and a 32-bit signed integer argument;
|
---|
216 | * --verbose (or -v) with no arguments,
|
---|
217 | *
|
---|
218 | * code would look something like this:
|
---|
219 | *
|
---|
220 | * @code
|
---|
221 | * int main(int argc, char *argv[])
|
---|
222 | * {
|
---|
223 | * static const RTGETOPTDEF s_aOptions[] =
|
---|
224 | * {
|
---|
225 | * { "--optwithstring", 's', RTGETOPT_REQ_STRING },
|
---|
226 | * { "--optwithint", 'i', RTGETOPT_REQ_INT32 },
|
---|
227 | * { "--verbose", 'v', 0 },
|
---|
228 | * };
|
---|
229 | *
|
---|
230 | * int ch;
|
---|
231 | * int i = 1;
|
---|
232 | * RTGETOPTUNION ValueUnion;
|
---|
233 | * RTGETOPTSTATE GetState;
|
---|
234 | * RTGetOptInit(&GetState, argc, argv, s_aOptions, RT_ELEMENTS(s_aOptions), 1, 0);
|
---|
235 | * while ((ch = RTGetOpt(&GetState, &ValueUnion)))
|
---|
236 | * {
|
---|
237 | * // for options that require an argument, ValueUnion has received the value
|
---|
238 | * switch (ch)
|
---|
239 | * {
|
---|
240 | * case 's': // --optwithstring or -s
|
---|
241 | * // string argument, copy ValueUnion.psz
|
---|
242 | * break;
|
---|
243 | *
|
---|
244 | * case 'i': // --optwithint or -i
|
---|
245 | * // integer argument, copy ValueUnion.i32
|
---|
246 | * break;
|
---|
247 | *
|
---|
248 | * case 'v': // --verbose or -v
|
---|
249 | * g_fOptVerbose = true;
|
---|
250 | * break;
|
---|
251 | *
|
---|
252 | * case VINF_GETOPT_NOT_OPTION:
|
---|
253 | * // handle non-option argument in ValueUnion.psz.
|
---|
254 | * break;
|
---|
255 | *
|
---|
256 | * default:
|
---|
257 | * if (ch > 0)
|
---|
258 | * Error("missing case: %c\n", ch);
|
---|
259 | * else if (ValueUnion.pDef)
|
---|
260 | * Error("%s: %Rrs", ValueUnion.pDef->pszLong, ch);
|
---|
261 | * else
|
---|
262 | * Error("%Rrs", ch);
|
---|
263 | * return 1;
|
---|
264 | * }
|
---|
265 | * }
|
---|
266 | *
|
---|
267 | * return 0;
|
---|
268 | * }
|
---|
269 | * @endcode
|
---|
270 | *
|
---|
271 | * @returns 0 when done parsing.
|
---|
272 | * @returns IPRT error status on parse error.
|
---|
273 | * @returns VINF_GETOPT_NOT_OPTION when encountering a non-option argument and
|
---|
274 | * RTGETOPT_FLAG_SORT was not specified. pValueUnion->psz points to the
|
---|
275 | * argument string.
|
---|
276 | * @returns VERR_GETOPT_UNKNOWN_OPTION when encountering an unknown option.
|
---|
277 | * pValueUnion->psz points to the option string.
|
---|
278 | * @returns VERR_GETOPT_REQUIRED_ARGUMENT_MISSING and pValueUnion->pDef if
|
---|
279 | * a required argument (aka value) was missing for an option.
|
---|
280 | * @returns VERR_GETOPT_INVALID_ARGUMENT_FORMAT and pValueUnion->pDef if
|
---|
281 | * argument (aka value) conversion failed.
|
---|
282 | *
|
---|
283 | * @param pState The state previously initialized with RTGetOptInit.
|
---|
284 | * @param pValueUnion Union with value; in the event of an error, psz member
|
---|
285 | * points to erroneous parameter; otherwise, for options
|
---|
286 | * that require an argument, this contains the value of
|
---|
287 | * that argument, depending on the type that is required.
|
---|
288 | */
|
---|
289 | RTDECL(int) RTGetOpt(PRTGETOPTSTATE pState, PRTGETOPTUNION pValueUnion);
|
---|
290 |
|
---|
291 | /** @} */
|
---|
292 |
|
---|
293 | __END_DECLS
|
---|
294 |
|
---|
295 | #endif
|
---|
296 |
|
---|