/* $Id: getopt.cpp 6000 2007-12-07 15:12:49Z vboxsync $ */ /** @file * innotek Portable Runtime - Command Line Parsing */ /* * Copyright (C) 2007 innotek GmbH * * This file is part of VirtualBox Open Source Edition (OSE), as * available from http://www.virtualbox.org. This file is free software; * you can redistribute it and/or modify it under the terms of the GNU * General Public License (GPL) as published by the Free Software * Foundation, in version 2 as it comes in the "COPYING" file of the * VirtualBox OSE distribution. VirtualBox OSE is distributed in the * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind. * * The contents of this file may alternatively be used under the terms * of the Common Development and Distribution License Version 1.0 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the * VirtualBox OSE distribution, in which case the provisions of the * CDDL are applicable instead of those of the GPL. * * You may elect to license modified versions of this file under the * terms and conditions of either the GPL or the CDDL or both. */ /******************************************************************************* * Header Files * *******************************************************************************/ #include #include #include #include RTDECL(int) RTGetOpt(int argc, char *argv[], PCRTOPTIONDEF paOptions, size_t cOptions, int *piThis, PRTOPTIONUNION pValueUnion) { pValueUnion->pDef = NULL; if ( !piThis || *piThis >= argc ) return 0; int iThis = (*piThis)++; const char *pszArgThis = argv[iThis]; if (*pszArgThis == '-') { for (size_t i = 0; i < cOptions; i++) { bool fShort = false; if ( ( paOptions[i].pszLong && !strcmp(pszArgThis, paOptions[i].pszLong)) || ( (fShort = (pszArgThis[1] == paOptions[i].iShort)) && pszArgThis[2] == '\0' ) ) { Assert(!(paOptions[i].fFlags & ~RTGETOPT_REQ_MASK)); pValueUnion->pDef = &paOptions[i]; if ((paOptions[i].fFlags & RTGETOPT_REQ_MASK) != RTGETOPT_REQ_NOTHING) { if (iThis >= argc - 1) return VERR_GETOPT_REQUIRED_ARGUMENT_MISSING; int iNext = (*piThis)++; switch (paOptions[i].fFlags & RTGETOPT_REQ_MASK) { case RTGETOPT_REQ_STRING: pValueUnion->psz = argv[iNext]; break; case RTGETOPT_REQ_INT32: { int32_t i32; if (RTStrToInt32Full(argv[iNext], 10, &i32)) return VERR_GETOPT_INVALID_ARGUMENT_FORMAT; pValueUnion->i32 = i32; break; } case RTGETOPT_REQ_UINT32: { uint32_t u32; if (RTStrToUInt32Full(argv[iNext], 10, &u32)) return VERR_GETOPT_INVALID_ARGUMENT_FORMAT; pValueUnion->u32 = u32; break; } default: AssertMsgFailed(("i=%d f=%#x\n", i, paOptions[i].fFlags)); return VERR_INTERNAL_ERROR; } } return paOptions[i].iShort; } } } /** @todo Sort options and arguments (i.e. stuff that doesn't start with '-'), stop when * encountering the first argument. */ return VERR_GETOPT_UNKNOWN_OPTION; }