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