1 | /* $Id: getopt.cpp 5999 2007-12-07 15:05:06Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * innotek Portable Runtime - Command Line Parsing
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2007 innotek GmbH
|
---|
8 | *
|
---|
9 | * innotek GmbH confidential
|
---|
10 | * All rights reserved
|
---|
11 | */
|
---|
12 |
|
---|
13 | /*******************************************************************************
|
---|
14 | * Header Files *
|
---|
15 | *******************************************************************************/
|
---|
16 | #include <iprt/getopt.h>
|
---|
17 | #include <iprt/err.h>
|
---|
18 | #include <iprt/string.h>
|
---|
19 | #include <iprt/assert.h>
|
---|
20 |
|
---|
21 |
|
---|
22 |
|
---|
23 | RTDECL(int) RTGetOpt(int argc, char *argv[], PCRTOPTIONDEF paOptions, size_t cOptions, int *piThis, PRTOPTIONUNION pValueUnion)
|
---|
24 | {
|
---|
25 | pValueUnion->pDef = NULL;
|
---|
26 |
|
---|
27 | if ( !piThis
|
---|
28 | || *piThis >= argc
|
---|
29 | )
|
---|
30 | return 0;
|
---|
31 |
|
---|
32 | int iThis = (*piThis)++;
|
---|
33 | const char *pszArgThis = argv[iThis];
|
---|
34 |
|
---|
35 | if (*pszArgThis == '-')
|
---|
36 | {
|
---|
37 | for (size_t i = 0; i < cOptions; i++)
|
---|
38 | {
|
---|
39 | bool fShort = false;
|
---|
40 | if ( ( paOptions[i].pszLong
|
---|
41 | && !strcmp(pszArgThis, paOptions[i].pszLong))
|
---|
42 | || ( (fShort = (pszArgThis[1] == paOptions[i].iShort))
|
---|
43 | && pszArgThis[2] == '\0'
|
---|
44 | )
|
---|
45 | )
|
---|
46 | {
|
---|
47 | Assert(!(paOptions[i].fFlags & ~RTGETOPT_REQ_MASK));
|
---|
48 | pValueUnion->pDef = &paOptions[i];
|
---|
49 |
|
---|
50 | if ((paOptions[i].fFlags & RTGETOPT_REQ_MASK) != RTGETOPT_REQ_NOTHING)
|
---|
51 | {
|
---|
52 | if (iThis >= argc - 1)
|
---|
53 | return VERR_GETOPT_REQUIRED_ARGUMENT_MISSING;
|
---|
54 |
|
---|
55 | int iNext = (*piThis)++;
|
---|
56 | switch (paOptions[i].fFlags & RTGETOPT_REQ_MASK)
|
---|
57 | {
|
---|
58 | case RTGETOPT_REQ_STRING:
|
---|
59 | pValueUnion->psz = argv[iNext];
|
---|
60 | break;
|
---|
61 |
|
---|
62 | case RTGETOPT_REQ_INT32:
|
---|
63 | {
|
---|
64 | int32_t i32;
|
---|
65 | if (RTStrToInt32Full(argv[iNext], 10, &i32))
|
---|
66 | return VERR_GETOPT_INVALID_ARGUMENT_FORMAT;
|
---|
67 |
|
---|
68 | pValueUnion->i32 = i32;
|
---|
69 | break;
|
---|
70 | }
|
---|
71 |
|
---|
72 | case RTGETOPT_REQ_UINT32:
|
---|
73 | {
|
---|
74 | uint32_t u32;
|
---|
75 | if (RTStrToUInt32Full(argv[iNext], 10, &u32))
|
---|
76 | return VERR_GETOPT_INVALID_ARGUMENT_FORMAT;
|
---|
77 |
|
---|
78 | pValueUnion->u32 = u32;
|
---|
79 | break;
|
---|
80 | }
|
---|
81 |
|
---|
82 | default:
|
---|
83 | AssertMsgFailed(("i=%d f=%#x\n", i, paOptions[i].fFlags));
|
---|
84 | return VERR_INTERNAL_ERROR;
|
---|
85 | }
|
---|
86 | }
|
---|
87 |
|
---|
88 | return paOptions[i].iShort;
|
---|
89 | }
|
---|
90 | }
|
---|
91 | }
|
---|
92 |
|
---|
93 | /** @todo Sort options and arguments (i.e. stuff that doesn't start with '-'), stop when
|
---|
94 | * encountering the first argument. */
|
---|
95 |
|
---|
96 | return VERR_GETOPT_UNKNOWN_OPTION;
|
---|
97 | }
|
---|
98 |
|
---|