getopt.cpp revision 01b2e367e947049139a5d5813ccc1fa162b11f76
65fea56f17cd614bc8908264df980a62e1931468vboxsync * IPRT - Command Line Parsing
65fea56f17cd614bc8908264df980a62e1931468vboxsync * Copyright (C) 2007-2011 Oracle Corporation
65fea56f17cd614bc8908264df980a62e1931468vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
65fea56f17cd614bc8908264df980a62e1931468vboxsync * available from http://www.virtualbox.org. This file is free software;
65fea56f17cd614bc8908264df980a62e1931468vboxsync * you can redistribute it and/or modify it under the terms of the GNU
65fea56f17cd614bc8908264df980a62e1931468vboxsync * General Public License (GPL) as published by the Free Software
65fea56f17cd614bc8908264df980a62e1931468vboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
65fea56f17cd614bc8908264df980a62e1931468vboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
65fea56f17cd614bc8908264df980a62e1931468vboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
65fea56f17cd614bc8908264df980a62e1931468vboxsync * The contents of this file may alternatively be used under the terms
65fea56f17cd614bc8908264df980a62e1931468vboxsync * of the Common Development and Distribution License Version 1.0
65fea56f17cd614bc8908264df980a62e1931468vboxsync * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
65fea56f17cd614bc8908264df980a62e1931468vboxsync * VirtualBox OSE distribution, in which case the provisions of the
65fea56f17cd614bc8908264df980a62e1931468vboxsync * CDDL are applicable instead of those of the GPL.
65fea56f17cd614bc8908264df980a62e1931468vboxsync * You may elect to license modified versions of this file under the
65fea56f17cd614bc8908264df980a62e1931468vboxsync * terms and conditions of either the GPL or the CDDL or both.
65fea56f17cd614bc8908264df980a62e1931468vboxsync/*******************************************************************************
65fea56f17cd614bc8908264df980a62e1931468vboxsync* Header Files *
65fea56f17cd614bc8908264df980a62e1931468vboxsync*******************************************************************************/
65fea56f17cd614bc8908264df980a62e1931468vboxsync#include <iprt/net.h> /* must come before getopt.h */
65fea56f17cd614bc8908264df980a62e1931468vboxsync/*******************************************************************************
65fea56f17cd614bc8908264df980a62e1931468vboxsync* Global Variables *
65fea56f17cd614bc8908264df980a62e1931468vboxsync*******************************************************************************/
65fea56f17cd614bc8908264df980a62e1931468vboxsync * Standard options that gets included unless RTGETOPTINIT_FLAGS_NO_STD_OPTS is
65fea56f17cd614bc8908264df980a62e1931468vboxsync/** The index of --help in g_aStdOptions. Used for some trickery. */
65fea56f17cd614bc8908264df980a62e1931468vboxsyncRTDECL(int) RTGetOptInit(PRTGETOPTSTATE pState, int argc, char **argv,
65fea56f17cd614bc8908264df980a62e1931468vboxsync AssertReturn(!(fFlags & ~(RTGETOPTINIT_FLAGS_OPTS_FIRST | RTGETOPTINIT_FLAGS_NO_STD_OPTS)), VERR_INVALID_PARAMETER);
65fea56f17cd614bc8908264df980a62e1931468vboxsync /* validate the options. */
65fea56f17cd614bc8908264df980a62e1931468vboxsync Assert(!(paOptions[i].fFlags & ~RTGETOPT_VALID_MASK));
65fea56f17cd614bc8908264df980a62e1931468vboxsync Assert(paOptions[i].iShort != VINF_GETOPT_NOT_OPTION);
65fea56f17cd614bc8908264df980a62e1931468vboxsync * Converts an stringified IPv4 address into the RTNETADDRIPV4 representation.
65fea56f17cd614bc8908264df980a62e1931468vboxsync * @todo This should be move to some generic part of the runtime.
65fea56f17cd614bc8908264df980a62e1931468vboxsync * @returns VINF_SUCCESS on success, VERR_GETOPT_INVALID_ARGUMENT_FORMAT on
65fea56f17cd614bc8908264df980a62e1931468vboxsync * @param pszValue The value to convert.
65fea56f17cd614bc8908264df980a62e1931468vboxsync * @param pAddr Where to store the result.
65fea56f17cd614bc8908264df980a62e1931468vboxsyncstatic int rtgetoptConvertIPv4Addr(const char *pszValue, PRTNETADDRIPV4 pAddr)
65fea56f17cd614bc8908264df980a62e1931468vboxsync int rc = RTStrToUInt8Ex(RTStrStripL(pszValue), &pszNext, 10, &pAddr->au8[0]);
65fea56f17cd614bc8908264df980a62e1931468vboxsync if (rc != VINF_SUCCESS && rc != VWRN_TRAILING_CHARS)
65fea56f17cd614bc8908264df980a62e1931468vboxsync rc = RTStrToUInt8Ex(pszNext, &pszNext, 10, &pAddr->au8[1]);
65fea56f17cd614bc8908264df980a62e1931468vboxsync if (rc != VINF_SUCCESS && rc != VWRN_TRAILING_CHARS)
if (*pszNext)
return VINF_SUCCESS;
char *pszNext;
pszNext++;
if (*pszNext)
return VINF_SUCCESS;
static PCRTGETOPTDEF rtGetOptSearchLong(const char *pszOption, PCRTGETOPTDEF paOptions, size_t cOptions, uint32_t fFlags)
while (cOptions-- > 0)
cchLong++;
return pOpt;
cchLong++;
return pOpt;
return pOpt;
pOpt++;
return &g_aStdOptions[i];
return NULL;
static PCRTGETOPTDEF rtGetOptSearchShort(int chOption, PCRTGETOPTDEF paOptions, size_t cOptions, uint32_t fFlags)
while (cOptions-- > 0)
return pOpt;
pOpt++;
return &g_aStdOptions[i];
return NULL;
case RTGETOPT_REQ_STRING:
case RTGETOPT_REQ_BOOL:
pValueUnion->f = true;
pValueUnion->f = false;
return VERR_GETOPT_UNKNOWN_OPTION;
case RTGETOPT_REQ_BOOL_ONOFF:
pValueUnion->f = true;
pValueUnion->f = false;
return VERR_GETOPT_UNKNOWN_OPTION;
case req: \
return VERR_GETOPT_INVALID_ARGUMENT_FORMAT; \
case req: \
return VERR_GETOPT_INVALID_ARGUMENT_FORMAT; \
case RTGETOPT_REQ_IPV4ADDR:
case RTGETOPT_REQ_MACADDR:
case RTGETOPT_REQ_UUID:
return VERR_INTERNAL_ERROR;
return VINF_SUCCESS;
bool fShort;
int iThis;
const char *pszArgThis;
pOpt = rtGetOptSearchShort(*pState->pszNextShort, pState->paOptions, pState->cOptions, pState->fFlags);
if (!pOpt)
return VERR_GETOPT_UNKNOWN_OPTION;
fShort = true;
return VINF_GETOPT_NOT_OPTION;
if ( !pOpt
fShort = false;
if (pOpt)
return VERR_GETOPT_UNKNOWN_OPTION;
if (pOpt)
const char *pszValue;
if (fShort)
return VERR_GETOPT_INDEX_MISSING;
return rc;
else if (fShort)
return VERR_GETOPT_INDEX_MISSING;
return VERR_GETOPT_UNKNOWN_OPTION;
return VINF_GETOPT_NOT_OPTION;
const char *pszValue = pState->argv[iThis + (pState->cNonOptions != INT32_MAX ? pState->cNonOptions : 0)];
else if (ch > 0)
return RTEXITCODE_SYNTAX;