getopt.cpp revision 5b281ba489ca18f0380d7efc7a5108b606cce449
5b281ba489ca18f0380d7efc7a5108b606cce449vboxsync * IPRT - Command Line Parsing
1c94c0a63ba68be1a7b2c640e70d7a06464e4fcavboxsync * Copyright (C) 2007 Sun Microsystems, Inc.
bd8e360cd1db83dcb2694ea9122ce3bc5bae678avboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
bd8e360cd1db83dcb2694ea9122ce3bc5bae678avboxsync * available from http://www.virtualbox.org. This file is free software;
bd8e360cd1db83dcb2694ea9122ce3bc5bae678avboxsync * you can redistribute it and/or modify it under the terms of the GNU
bd8e360cd1db83dcb2694ea9122ce3bc5bae678avboxsync * General Public License (GPL) as published by the Free Software
bd8e360cd1db83dcb2694ea9122ce3bc5bae678avboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
bd8e360cd1db83dcb2694ea9122ce3bc5bae678avboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
bd8e360cd1db83dcb2694ea9122ce3bc5bae678avboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
bd8e360cd1db83dcb2694ea9122ce3bc5bae678avboxsync * The contents of this file may alternatively be used under the terms
bd8e360cd1db83dcb2694ea9122ce3bc5bae678avboxsync * of the Common Development and Distribution License Version 1.0
bd8e360cd1db83dcb2694ea9122ce3bc5bae678avboxsync * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
bd8e360cd1db83dcb2694ea9122ce3bc5bae678avboxsync * VirtualBox OSE distribution, in which case the provisions of the
bd8e360cd1db83dcb2694ea9122ce3bc5bae678avboxsync * CDDL are applicable instead of those of the GPL.
bd8e360cd1db83dcb2694ea9122ce3bc5bae678avboxsync * You may elect to license modified versions of this file under the
bd8e360cd1db83dcb2694ea9122ce3bc5bae678avboxsync * terms and conditions of either the GPL or the CDDL or both.
1c94c0a63ba68be1a7b2c640e70d7a06464e4fcavboxsync * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
1c94c0a63ba68be1a7b2c640e70d7a06464e4fcavboxsync * Clara, CA 95054 USA or visit http://www.sun.com if you need
1c94c0a63ba68be1a7b2c640e70d7a06464e4fcavboxsync * additional information or have any questions.
4d6dcfe00aab559241d9ed05b89f803ab5ddf611vboxsync/*******************************************************************************
4d6dcfe00aab559241d9ed05b89f803ab5ddf611vboxsync* Header Files *
4d6dcfe00aab559241d9ed05b89f803ab5ddf611vboxsync*******************************************************************************/
d618bed3882cd227cf8f5d0d2824f8db42fad415vboxsyncRTDECL(int) RTGetOpt(int argc, char **argv, PCRTOPTIONDEF paOptions, size_t cOptions, int *piThis, PRTOPTIONUNION pValueUnion)
d618bed3882cd227cf8f5d0d2824f8db42fad415vboxsync Assert(!(paOptions[i].fFlags & ~RTGETOPT_VALID_MASK));
d618bed3882cd227cf8f5d0d2824f8db42fad415vboxsync if ((paOptions[i].fFlags & RTGETOPT_REQ_MASK) != RTGETOPT_REQ_NOTHING)
d618bed3882cd227cf8f5d0d2824f8db42fad415vboxsync * A value is required with the argument. We're trying to very
d618bed3882cd227cf8f5d0d2824f8db42fad415vboxsync * understanding here and will permit any of the following:
d618bed3882cd227cf8f5d0d2824f8db42fad415vboxsync * -svalue, -s:value, -s=value,
d618bed3882cd227cf8f5d0d2824f8db42fad415vboxsync * -s value, -s: value, -s= value
d618bed3882cd227cf8f5d0d2824f8db42fad415vboxsync * (Ditto for long options.)
d618bed3882cd227cf8f5d0d2824f8db42fad415vboxsync bool fShort = false;
d618bed3882cd227cf8f5d0d2824f8db42fad415vboxsync && !strncmp(pszArgThis, paOptions[i].pszLong, (cchLong = strlen(paOptions[i].pszLong)))
d618bed3882cd227cf8f5d0d2824f8db42fad415vboxsync || (fShort = (pszArgThis[1] == paOptions[i].iShort))
d618bed3882cd227cf8f5d0d2824f8db42fad415vboxsync pValueUnion->pDef = &paOptions[i]; /* in case of error. */
d618bed3882cd227cf8f5d0d2824f8db42fad415vboxsync * Find the argument value
d618bed3882cd227cf8f5d0d2824f8db42fad415vboxsync const char *pszValue;
d618bed3882cd227cf8f5d0d2824f8db42fad415vboxsync || ((pszArgThis[2] == ':' || pszArgThis[2] == '=') && pszArgThis[3] == '\0')
d618bed3882cd227cf8f5d0d2824f8db42fad415vboxsync : pszArgThis[cchLong] == '\0' || pszArgThis[cchLong + 1] == '\0')
d618bed3882cd227cf8f5d0d2824f8db42fad415vboxsync else /* same argument. */
d618bed3882cd227cf8f5d0d2824f8db42fad415vboxsync ? &pszArgThis[2 + (pszArgThis[2] == ':' || pszArgThis[2] == '=')]
d618bed3882cd227cf8f5d0d2824f8db42fad415vboxsync * Transform into a option value as requested.
d618bed3882cd227cf8f5d0d2824f8db42fad415vboxsync * If decimal conversion fails, we'll check for "0x<xdigit>" and
d618bed3882cd227cf8f5d0d2824f8db42fad415vboxsync * try a 16 based conversion. We will not interpret any of the
d618bed3882cd227cf8f5d0d2824f8db42fad415vboxsync * generic ints as octals.
d618bed3882cd227cf8f5d0d2824f8db42fad415vboxsync switch (paOptions[i].fFlags & (RTGETOPT_REQ_MASK | RTGETOPT_FLAG_HEX | RTGETOPT_FLAG_OCT | RTGETOPT_FLAG_DEC))
d618bed3882cd227cf8f5d0d2824f8db42fad415vboxsync if ( convfn(pszValue, 10, &Value) != VINF_SUCCESS \
d618bed3882cd227cf8f5d0d2824f8db42fad415vboxsync || convfn(pszValue, 16, &Value) != VINF_SUCCESS ) ) \
d618bed3882cd227cf8f5d0d2824f8db42fad415vboxsync#define MY_BASE_INT_CASE(req,type,memb,convfn,base) \
d618bed3882cd227cf8f5d0d2824f8db42fad415vboxsync if (convfn(pszValue, base, &Value) != VINF_SUCCESS) \
d618bed3882cd227cf8f5d0d2824f8db42fad415vboxsync MY_INT_CASE(RTGETOPT_REQ_INT8, int8_t, i, RTStrToInt8Full)
d618bed3882cd227cf8f5d0d2824f8db42fad415vboxsync MY_INT_CASE(RTGETOPT_REQ_INT16, int16_t, i, RTStrToInt16Full)
d618bed3882cd227cf8f5d0d2824f8db42fad415vboxsync MY_INT_CASE(RTGETOPT_REQ_INT32, int32_t, i, RTStrToInt32Full)
d618bed3882cd227cf8f5d0d2824f8db42fad415vboxsync MY_INT_CASE(RTGETOPT_REQ_INT64, int64_t, i, RTStrToInt64Full)
d618bed3882cd227cf8f5d0d2824f8db42fad415vboxsync MY_INT_CASE(RTGETOPT_REQ_UINT8, uint8_t, u, RTStrToUInt8Full)
d618bed3882cd227cf8f5d0d2824f8db42fad415vboxsync MY_INT_CASE(RTGETOPT_REQ_UINT16, uint16_t, u, RTStrToUInt16Full)
d618bed3882cd227cf8f5d0d2824f8db42fad415vboxsync MY_INT_CASE(RTGETOPT_REQ_UINT32, uint32_t, u, RTStrToUInt32Full)
d618bed3882cd227cf8f5d0d2824f8db42fad415vboxsync MY_INT_CASE(RTGETOPT_REQ_UINT64, uint64_t, u, RTStrToUInt64Full)
d618bed3882cd227cf8f5d0d2824f8db42fad415vboxsync MY_BASE_INT_CASE(RTGETOPT_REQ_INT8 | RTGETOPT_FLAG_HEX, int8_t, i, RTStrToInt8Full, 16)
d618bed3882cd227cf8f5d0d2824f8db42fad415vboxsync MY_BASE_INT_CASE(RTGETOPT_REQ_INT16 | RTGETOPT_FLAG_HEX, int16_t, i, RTStrToInt16Full, 16)
d618bed3882cd227cf8f5d0d2824f8db42fad415vboxsync MY_BASE_INT_CASE(RTGETOPT_REQ_INT32 | RTGETOPT_FLAG_HEX, int32_t, i, RTStrToInt32Full, 16)
d618bed3882cd227cf8f5d0d2824f8db42fad415vboxsync MY_BASE_INT_CASE(RTGETOPT_REQ_INT64 | RTGETOPT_FLAG_HEX, int64_t, i, RTStrToInt64Full, 16)
d618bed3882cd227cf8f5d0d2824f8db42fad415vboxsync MY_BASE_INT_CASE(RTGETOPT_REQ_UINT8 | RTGETOPT_FLAG_HEX, uint8_t, u, RTStrToUInt8Full, 16)
d618bed3882cd227cf8f5d0d2824f8db42fad415vboxsync MY_BASE_INT_CASE(RTGETOPT_REQ_UINT16 | RTGETOPT_FLAG_HEX, uint16_t, u, RTStrToUInt16Full, 16)
d618bed3882cd227cf8f5d0d2824f8db42fad415vboxsync MY_BASE_INT_CASE(RTGETOPT_REQ_UINT32 | RTGETOPT_FLAG_HEX, uint32_t, u, RTStrToUInt32Full, 16)
d618bed3882cd227cf8f5d0d2824f8db42fad415vboxsync MY_BASE_INT_CASE(RTGETOPT_REQ_UINT64 | RTGETOPT_FLAG_HEX, uint64_t, u, RTStrToUInt64Full, 16)
d618bed3882cd227cf8f5d0d2824f8db42fad415vboxsync MY_BASE_INT_CASE(RTGETOPT_REQ_INT8 | RTGETOPT_FLAG_DEC, int8_t, i, RTStrToInt8Full, 10)
d618bed3882cd227cf8f5d0d2824f8db42fad415vboxsync MY_BASE_INT_CASE(RTGETOPT_REQ_INT16 | RTGETOPT_FLAG_DEC, int16_t, i, RTStrToInt16Full, 10)
d618bed3882cd227cf8f5d0d2824f8db42fad415vboxsync MY_BASE_INT_CASE(RTGETOPT_REQ_INT32 | RTGETOPT_FLAG_DEC, int32_t, i, RTStrToInt32Full, 10)
d618bed3882cd227cf8f5d0d2824f8db42fad415vboxsync MY_BASE_INT_CASE(RTGETOPT_REQ_INT64 | RTGETOPT_FLAG_DEC, int64_t, i, RTStrToInt64Full, 10)
d618bed3882cd227cf8f5d0d2824f8db42fad415vboxsync MY_BASE_INT_CASE(RTGETOPT_REQ_UINT8 | RTGETOPT_FLAG_DEC, uint8_t, u, RTStrToUInt8Full, 10)
d618bed3882cd227cf8f5d0d2824f8db42fad415vboxsync MY_BASE_INT_CASE(RTGETOPT_REQ_UINT16 | RTGETOPT_FLAG_DEC, uint16_t, u, RTStrToUInt16Full, 10)
d618bed3882cd227cf8f5d0d2824f8db42fad415vboxsync MY_BASE_INT_CASE(RTGETOPT_REQ_UINT32 | RTGETOPT_FLAG_DEC, uint32_t, u, RTStrToUInt32Full, 10)
d618bed3882cd227cf8f5d0d2824f8db42fad415vboxsync MY_BASE_INT_CASE(RTGETOPT_REQ_UINT64 | RTGETOPT_FLAG_DEC, uint64_t, u, RTStrToUInt64Full, 10)
d618bed3882cd227cf8f5d0d2824f8db42fad415vboxsync MY_BASE_INT_CASE(RTGETOPT_REQ_INT8 | RTGETOPT_FLAG_OCT, int8_t, i, RTStrToInt8Full, 8)
d618bed3882cd227cf8f5d0d2824f8db42fad415vboxsync MY_BASE_INT_CASE(RTGETOPT_REQ_INT16 | RTGETOPT_FLAG_OCT, int16_t, i, RTStrToInt16Full, 8)
d618bed3882cd227cf8f5d0d2824f8db42fad415vboxsync MY_BASE_INT_CASE(RTGETOPT_REQ_INT32 | RTGETOPT_FLAG_OCT, int32_t, i, RTStrToInt32Full, 8)
d618bed3882cd227cf8f5d0d2824f8db42fad415vboxsync MY_BASE_INT_CASE(RTGETOPT_REQ_INT64 | RTGETOPT_FLAG_OCT, int64_t, i, RTStrToInt64Full, 8)
d618bed3882cd227cf8f5d0d2824f8db42fad415vboxsync MY_BASE_INT_CASE(RTGETOPT_REQ_UINT8 | RTGETOPT_FLAG_OCT, uint8_t, u, RTStrToUInt8Full, 8)
d618bed3882cd227cf8f5d0d2824f8db42fad415vboxsync MY_BASE_INT_CASE(RTGETOPT_REQ_UINT16 | RTGETOPT_FLAG_OCT, uint16_t, u, RTStrToUInt16Full, 8)
d618bed3882cd227cf8f5d0d2824f8db42fad415vboxsync MY_BASE_INT_CASE(RTGETOPT_REQ_UINT32 | RTGETOPT_FLAG_OCT, uint32_t, u, RTStrToUInt32Full, 8)
d618bed3882cd227cf8f5d0d2824f8db42fad415vboxsync MY_BASE_INT_CASE(RTGETOPT_REQ_UINT64 | RTGETOPT_FLAG_OCT, uint64_t, u, RTStrToUInt64Full, 8)
dccbafa70b5a9a6f933c5566e4542caf9f379b97vboxsync AssertMsgFailed(("i=%d f=%#x\n", i, paOptions[i].fFlags));
d618bed3882cd227cf8f5d0d2824f8db42fad415vboxsync && pszArgThis[2] == '\0') /** @todo implement support for ls -lsR like stuff? */
d618bed3882cd227cf8f5d0d2824f8db42fad415vboxsync /** @todo Sort options and arguments (i.e. stuff that doesn't start with '-'), stop when
dccbafa70b5a9a6f933c5566e4542caf9f379b97vboxsync * encountering the first argument. */