VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/misc/getopt.cpp@ 5912

Last change on this file since 5912 was 5912, checked in by vboxsync, 17 years ago

backed out r26450

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 3.4 KB
Line 
1/* $Id: getopt.cpp 5912 2007-12-02 21:32:00Z vboxsync $ */
2/** @file
3 * innotek Portable Runtime - Command Line Parsing
4 */
5
6/*
7 * Copyright (C) 2007 innotek GmbH
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License as published by the Free Software Foundation,
13 * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
14 * distribution. VirtualBox OSE is distributed in the hope that it will
15 * be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18/*******************************************************************************
19* Header Files *
20*******************************************************************************/
21#include <iprt/getopt.h>
22#include <iprt/err.h>
23#include <iprt/string.h>
24#include <iprt/assert.h>
25
26
27
28RTDECL(int) RTGetOpt(int argc, char *argv[], PCRTOPTIONDEF paOptions, size_t cOptions, int *piThis, PRTOPTIONUNION pValueUnion)
29{
30 pValueUnion->pDef = NULL;
31
32 if ( !piThis
33 || *piThis >= argc
34 )
35 return 0;
36
37 int iThis = (*piThis)++;
38 const char *pszArgThis = argv[iThis];
39
40 if (*pszArgThis == '-')
41 {
42 for (size_t i = 0; i < cOptions; i++)
43 {
44 bool fShort = false;
45 if ( ( paOptions[i].pszLong
46 && !strcmp(pszArgThis, paOptions[i].pszLong))
47 || ( (fShort = (pszArgThis[1] == paOptions[i].iShort))
48 && pszArgThis[2] == '\0'
49 )
50 )
51 {
52 Assert(!(paOptions[i].fFlags & ~RTGETOPT_REQ_MASK));
53 pValueUnion->pDef = &paOptions[i];
54
55 if ((paOptions[i].fFlags & RTGETOPT_REQ_MASK) != RTGETOPT_REQ_NOTHING)
56 {
57 if (iThis >= argc - 1)
58 return VERR_GETOPT_REQUIRED_ARGUMENT_MISSING;
59
60 int iNext = (*piThis)++;
61 switch (paOptions[i].fFlags & RTGETOPT_REQ_MASK)
62 {
63 case RTGETOPT_REQ_STRING:
64 pValueUnion->psz = argv[iNext];
65 break;
66
67 case RTGETOPT_REQ_INT32:
68 {
69 int32_t i32;
70 if (RTStrToInt32Full(argv[iNext], 10, &i32))
71 return VERR_GETOPT_INVALID_ARGUMENT_FORMAT;
72
73 pValueUnion->i32 = i32;
74 break;
75 }
76
77 case RTGETOPT_REQ_UINT32:
78 {
79 uint32_t u32;
80 if (RTStrToUInt32Full(argv[iNext], 10, &u32))
81 return VERR_GETOPT_INVALID_ARGUMENT_FORMAT;
82
83 pValueUnion->u32 = u32;
84 break;
85 }
86
87 default:
88 AssertMsgFailed(("i=%d f=%#x\n", i, paOptions[i].fFlags));
89 return VERR_INTERNAL_ERROR;
90 }
91 }
92
93 return paOptions[i].iShort;
94 }
95 }
96 }
97
98 /** @todo Sort options and arguments (i.e. stuff that doesn't start with '-'), stop when
99 * encountering the first argument. */
100
101 return VERR_GETOPT_UNKNOWN_OPTION;
102}
103
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