getopt.cpp revision 3a491eb2162ca9e2024712f737c6317882307e13
5b281ba489ca18f0380d7efc7a5108b606cce449vboxsync * IPRT - Command Line Parsing
833f83ce101b6e9168f519decc0dc7a1079d35f7vboxsync * Copyright (C) 2007-2013 Oracle Corporation
833f83ce101b6e9168f519decc0dc7a1079d35f7vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
833f83ce101b6e9168f519decc0dc7a1079d35f7vboxsync * available from http://www.virtualbox.org. This file is free software;
833f83ce101b6e9168f519decc0dc7a1079d35f7vboxsync * you can redistribute it and/or modify it under the terms of the GNU
833f83ce101b6e9168f519decc0dc7a1079d35f7vboxsync * General Public License (GPL) as published by the Free Software
833f83ce101b6e9168f519decc0dc7a1079d35f7vboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
833f83ce101b6e9168f519decc0dc7a1079d35f7vboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
833f83ce101b6e9168f519decc0dc7a1079d35f7vboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
833f83ce101b6e9168f519decc0dc7a1079d35f7vboxsync * The contents of this file may alternatively be used under the terms
833f83ce101b6e9168f519decc0dc7a1079d35f7vboxsync * of the Common Development and Distribution License Version 1.0
833f83ce101b6e9168f519decc0dc7a1079d35f7vboxsync * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
833f83ce101b6e9168f519decc0dc7a1079d35f7vboxsync * VirtualBox OSE distribution, in which case the provisions of the
833f83ce101b6e9168f519decc0dc7a1079d35f7vboxsync * CDDL are applicable instead of those of the GPL.
833f83ce101b6e9168f519decc0dc7a1079d35f7vboxsync * You may elect to license modified versions of this file under the
833f83ce101b6e9168f519decc0dc7a1079d35f7vboxsync * terms and conditions of either the GPL or the CDDL or both.
833f83ce101b6e9168f519decc0dc7a1079d35f7vboxsync/*******************************************************************************
833f83ce101b6e9168f519decc0dc7a1079d35f7vboxsync* Header Files *
833f83ce101b6e9168f519decc0dc7a1079d35f7vboxsync*******************************************************************************/
833f83ce101b6e9168f519decc0dc7a1079d35f7vboxsync#include <iprt/net.h> /* must come before getopt.h */
833f83ce101b6e9168f519decc0dc7a1079d35f7vboxsync/*******************************************************************************
833f83ce101b6e9168f519decc0dc7a1079d35f7vboxsync* Global Variables *
833f83ce101b6e9168f519decc0dc7a1079d35f7vboxsync*******************************************************************************/
833f83ce101b6e9168f519decc0dc7a1079d35f7vboxsync * Standard options that gets included unless RTGETOPTINIT_FLAGS_NO_STD_OPTS is
4d32c71a938b979c6aeee321ae325d5793a5ccdbvboxsync/** The index of --help in g_aStdOptions. Used for some trickery. */
4d32c71a938b979c6aeee321ae325d5793a5ccdbvboxsyncRTDECL(int) RTGetOptInit(PRTGETOPTSTATE pState, int argc, char **argv,
4d32c71a938b979c6aeee321ae325d5793a5ccdbvboxsync AssertReturn(!(fFlags & ~(RTGETOPTINIT_FLAGS_OPTS_FIRST | RTGETOPTINIT_FLAGS_NO_STD_OPTS)), VERR_INVALID_PARAMETER);
5eda82e218d35ae0691febd531e1bfc0324cc4a6vboxsync /* validate the options. */
7f4b4ec1cadbf891d2756ce77dc4a4ec220a03bcvboxsync Assert(!(paOptions[i].fFlags & ~RTGETOPT_VALID_MASK));
7f4b4ec1cadbf891d2756ce77dc4a4ec220a03bcvboxsync Assert(paOptions[i].iShort != VINF_GETOPT_NOT_OPTION);
7f4b4ec1cadbf891d2756ce77dc4a4ec220a03bcvboxsync * Converts an stringified IPv4 address into the RTNETADDRIPV4 representation.
7f4b4ec1cadbf891d2756ce77dc4a4ec220a03bcvboxsync * @returns VINF_SUCCESS on success, VERR_GETOPT_INVALID_ARGUMENT_FORMAT on
7f4b4ec1cadbf891d2756ce77dc4a4ec220a03bcvboxsync * @param pszValue The value to convert.
7f4b4ec1cadbf891d2756ce77dc4a4ec220a03bcvboxsync * @param pAddr Where to store the result.
7f4b4ec1cadbf891d2756ce77dc4a4ec220a03bcvboxsyncstatic int rtgetoptConvertIPv4Addr(const char *pszValue, PRTNETADDRIPV4 pAddr)
7f4b4ec1cadbf891d2756ce77dc4a4ec220a03bcvboxsync if (RT_FAILURE(RTNetStrToIPv4Addr(pszValue, pAddr)))
7f4b4ec1cadbf891d2756ce77dc4a4ec220a03bcvboxsync * Converts an stringified Ethernet MAC address into the RTMAC representation.
7f4b4ec1cadbf891d2756ce77dc4a4ec220a03bcvboxsync * @todo This should be move to some generic part of the runtime.
7f4b4ec1cadbf891d2756ce77dc4a4ec220a03bcvboxsync * @returns VINF_SUCCESS on success, VERR_GETOPT_INVALID_ARGUMENT_FORMAT on
7f4b4ec1cadbf891d2756ce77dc4a4ec220a03bcvboxsync * @param pszValue The value to convert.
833f83ce101b6e9168f519decc0dc7a1079d35f7vboxsync * @param pAddr Where to store the result.
5eda82e218d35ae0691febd531e1bfc0324cc4a6vboxsyncstatic int rtgetoptConvertMacAddr(const char *pszValue, PRTMAC pAddr)
d60d5da33bb93fc7a8717802f21b13aa37914799vboxsync * Not quite sure if I should accept stuff like "08::27:::1" here...
833f83ce101b6e9168f519decc0dc7a1079d35f7vboxsync * The code is accepting "::" patterns now, except for for the first
833f83ce101b6e9168f519decc0dc7a1079d35f7vboxsync * and last parts.
d60d5da33bb93fc7a8717802f21b13aa37914799vboxsync /* first */
ee6e48c229ef52aee5e968d956ebd066073811abvboxsync int rc = RTStrToUInt8Ex(RTStrStripL(pszValue), &pszNext, 16, &pAddr->au8[0]);
833f83ce101b6e9168f519decc0dc7a1079d35f7vboxsync if (rc != VINF_SUCCESS && rc != VWRN_TRAILING_CHARS)
7f4b4ec1cadbf891d2756ce77dc4a4ec220a03bcvboxsync /* middle */
7f4b4ec1cadbf891d2756ce77dc4a4ec220a03bcvboxsync rc = RTStrToUInt8Ex(pszNext, &pszNext, 16, &pAddr->au8[i]);
d60d5da33bb93fc7a8717802f21b13aa37914799vboxsync if (rc != VINF_SUCCESS && rc != VWRN_TRAILING_CHARS)
d60d5da33bb93fc7a8717802f21b13aa37914799vboxsync rc = RTStrToUInt8Ex(pszNext, &pszNext, 16, &pAddr->au8[5]);
d60d5da33bb93fc7a8717802f21b13aa37914799vboxsync if (rc != VINF_SUCCESS && rc != VWRN_TRAILING_SPACES)
d60d5da33bb93fc7a8717802f21b13aa37914799vboxsync * Searches for a long option.
d60d5da33bb93fc7a8717802f21b13aa37914799vboxsync * @returns Pointer to a matching option.
d60d5da33bb93fc7a8717802f21b13aa37914799vboxsync * @param pszOption The alleged long option.
d60d5da33bb93fc7a8717802f21b13aa37914799vboxsync * @param paOptions Option array.
e5bfc5c34142a7550be3564a8e01a037b1db5b31vboxsync * @param cOptions Number of items in the array.
d60d5da33bb93fc7a8717802f21b13aa37914799vboxsync * @param fFlags Init flags.
d60d5da33bb93fc7a8717802f21b13aa37914799vboxsyncstatic PCRTGETOPTDEF rtGetOptSearchLong(const char *pszOption, PCRTGETOPTDEF paOptions, size_t cOptions, uint32_t fFlags)
d60d5da33bb93fc7a8717802f21b13aa37914799vboxsync while (cOptions-- > 0)
ab02aeaf2a0312fe9267a292e3911728e4531332vboxsync if ((pOpt->fFlags & RTGETOPT_REQ_MASK) != RTGETOPT_REQ_NOTHING)
ab02aeaf2a0312fe9267a292e3911728e4531332vboxsync * A value is required with the argument. We're trying to be
c889bbab784ba8552102ce776b6c67b982017861vboxsync * understanding here and will permit any of the following:
c889bbab784ba8552102ce776b6c67b982017861vboxsync * --long12:value, --long12=value, --long12 value,
ab02aeaf2a0312fe9267a292e3911728e4531332vboxsync * --long:value, --long=value, --long value,
d60d5da33bb93fc7a8717802f21b13aa37914799vboxsync * If the option is index, then all trailing chars must be
e5bfc5c34142a7550be3564a8e01a037b1db5b31vboxsync * digits. For error reporting reasons we also match where
d60d5da33bb93fc7a8717802f21b13aa37914799vboxsync * there is no index.
7f4b4ec1cadbf891d2756ce77dc4a4ec220a03bcvboxsync && !RTStrNICmp(pszOption, pOpt->pszLong, cchLong)))
234af146205f61c4aa0be736abb06601a89facb8vboxsync * The option takes an index but no value.
234af146205f61c4aa0be736abb06601a89facb8vboxsync * As above, we also match where there is no index.
833f83ce101b6e9168f519decc0dc7a1079d35f7vboxsync && !RTStrNICmp(pszOption, pOpt->pszLong, cchLong)))
e5bfc5c34142a7550be3564a8e01a037b1db5b31vboxsync for (uint32_t i = 0; i < RT_ELEMENTS(g_aStdOptions); i++)
7f4b4ec1cadbf891d2756ce77dc4a4ec220a03bcvboxsync && !RTStrICmp(pszOption, g_aStdOptions[i].pszLong)))
7f4b4ec1cadbf891d2756ce77dc4a4ec220a03bcvboxsync * Searches for a matching short option.
7f4b4ec1cadbf891d2756ce77dc4a4ec220a03bcvboxsync * @returns Pointer to a matching option.
7f4b4ec1cadbf891d2756ce77dc4a4ec220a03bcvboxsync * @param chOption The option char.
7f4b4ec1cadbf891d2756ce77dc4a4ec220a03bcvboxsync * @param paOptions Option array.
833f83ce101b6e9168f519decc0dc7a1079d35f7vboxsync * @param cOptions Number of items in the array.
7f4b4ec1cadbf891d2756ce77dc4a4ec220a03bcvboxsync * @param fFlags Init flags.
7f4b4ec1cadbf891d2756ce77dc4a4ec220a03bcvboxsyncstatic PCRTGETOPTDEF rtGetOptSearchShort(int chOption, PCRTGETOPTDEF paOptions, size_t cOptions, uint32_t fFlags)
81fcf0038d2d6c76ab2c8b02103dc18c24efe0a1vboxsync while (cOptions-- > 0)
7f4b4ec1cadbf891d2756ce77dc4a4ec220a03bcvboxsync for (uint32_t i = 0; i < RT_ELEMENTS(g_aStdOptions); i++)
7f4b4ec1cadbf891d2756ce77dc4a4ec220a03bcvboxsync return &g_aStdOptions[RTGETOPT_STD_OPTIONS_HELP_IDX];
7f4b4ec1cadbf891d2756ce77dc4a4ec220a03bcvboxsync * Value string -> Value union.
81fcf0038d2d6c76ab2c8b02103dc18c24efe0a1vboxsync * @returns IPRT status code.
7f4b4ec1cadbf891d2756ce77dc4a4ec220a03bcvboxsync * @param fFlags The value flags.
7f4b4ec1cadbf891d2756ce77dc4a4ec220a03bcvboxsync * @param pszValue The value string.
7f4b4ec1cadbf891d2756ce77dc4a4ec220a03bcvboxsync * @param pValueUnion Where to return the processed value.
7f4b4ec1cadbf891d2756ce77dc4a4ec220a03bcvboxsyncstatic int rtGetOptProcessValue(uint32_t fFlags, const char *pszValue, PRTGETOPTUNION pValueUnion)
00cc5d93f7446d9394a0f6b7ad790b9fb9d6005cvboxsync * Transform into a option value as requested.
7f4b4ec1cadbf891d2756ce77dc4a4ec220a03bcvboxsync * If decimal conversion fails, we'll check for "0x<xdigit>" and
7f4b4ec1cadbf891d2756ce77dc4a4ec220a03bcvboxsync * try a 16 based conversion. We will not interpret any of the
7f4b4ec1cadbf891d2756ce77dc4a4ec220a03bcvboxsync * generic ints as octals.
d60d5da33bb93fc7a8717802f21b13aa37914799vboxsync if ( convfn(pszValue, 10, &Value) != VINF_SUCCESS \
d60d5da33bb93fc7a8717802f21b13aa37914799vboxsync || convfn(pszValue, 16, &Value) != VINF_SUCCESS ) ) \
833f83ce101b6e9168f519decc0dc7a1079d35f7vboxsync#define MY_BASE_INT_CASE(req, type, memb, convfn, base) \
234af146205f61c4aa0be736abb06601a89facb8vboxsync if (convfn(pszValue, base, &Value) != VINF_SUCCESS) \
833f83ce101b6e9168f519decc0dc7a1079d35f7vboxsync MY_INT_CASE(RTGETOPT_REQ_INT8, int8_t, i8, RTStrToInt8Full)
ee6e48c229ef52aee5e968d956ebd066073811abvboxsync MY_INT_CASE(RTGETOPT_REQ_INT16, int16_t, i16, RTStrToInt16Full)
833f83ce101b6e9168f519decc0dc7a1079d35f7vboxsync MY_INT_CASE(RTGETOPT_REQ_INT32, int32_t, i32, RTStrToInt32Full)
833f83ce101b6e9168f519decc0dc7a1079d35f7vboxsync MY_INT_CASE(RTGETOPT_REQ_INT64, int64_t, i64, RTStrToInt64Full)
833f83ce101b6e9168f519decc0dc7a1079d35f7vboxsync MY_INT_CASE(RTGETOPT_REQ_UINT8, uint8_t, u8, RTStrToUInt8Full)
833f83ce101b6e9168f519decc0dc7a1079d35f7vboxsync MY_INT_CASE(RTGETOPT_REQ_UINT16, uint16_t, u16, RTStrToUInt16Full)
ee6e48c229ef52aee5e968d956ebd066073811abvboxsync MY_INT_CASE(RTGETOPT_REQ_UINT32, uint32_t, u32, RTStrToUInt32Full)
ee6e48c229ef52aee5e968d956ebd066073811abvboxsync MY_INT_CASE(RTGETOPT_REQ_UINT64, uint64_t, u64, RTStrToUInt64Full)
ee6e48c229ef52aee5e968d956ebd066073811abvboxsync MY_BASE_INT_CASE(RTGETOPT_REQ_INT8 | RTGETOPT_FLAG_HEX, int8_t, i8, RTStrToInt8Full, 16)
ee6e48c229ef52aee5e968d956ebd066073811abvboxsync MY_BASE_INT_CASE(RTGETOPT_REQ_INT16 | RTGETOPT_FLAG_HEX, int16_t, i16, RTStrToInt16Full, 16)
ee6e48c229ef52aee5e968d956ebd066073811abvboxsync MY_BASE_INT_CASE(RTGETOPT_REQ_INT32 | RTGETOPT_FLAG_HEX, int32_t, i32, RTStrToInt32Full, 16)
ad27e1d5e48ca41245120c331cc88b50464813cevboxsync MY_BASE_INT_CASE(RTGETOPT_REQ_INT64 | RTGETOPT_FLAG_HEX, int64_t, i64, RTStrToInt64Full, 16)
ee6e48c229ef52aee5e968d956ebd066073811abvboxsync MY_BASE_INT_CASE(RTGETOPT_REQ_UINT8 | RTGETOPT_FLAG_HEX, uint8_t, u8, RTStrToUInt8Full, 16)
833f83ce101b6e9168f519decc0dc7a1079d35f7vboxsync MY_BASE_INT_CASE(RTGETOPT_REQ_UINT16 | RTGETOPT_FLAG_HEX, uint16_t, u16, RTStrToUInt16Full, 16)
ee6e48c229ef52aee5e968d956ebd066073811abvboxsync MY_BASE_INT_CASE(RTGETOPT_REQ_UINT32 | RTGETOPT_FLAG_HEX, uint32_t, u32, RTStrToUInt32Full, 16)
833f83ce101b6e9168f519decc0dc7a1079d35f7vboxsync MY_BASE_INT_CASE(RTGETOPT_REQ_UINT64 | RTGETOPT_FLAG_HEX, uint64_t, u64, RTStrToUInt64Full, 16)
833f83ce101b6e9168f519decc0dc7a1079d35f7vboxsync MY_BASE_INT_CASE(RTGETOPT_REQ_INT8 | RTGETOPT_FLAG_DEC, int8_t, i8, RTStrToInt8Full, 10)
e5bfc5c34142a7550be3564a8e01a037b1db5b31vboxsync MY_BASE_INT_CASE(RTGETOPT_REQ_INT16 | RTGETOPT_FLAG_DEC, int16_t, i16, RTStrToInt16Full, 10)
ee6e48c229ef52aee5e968d956ebd066073811abvboxsync MY_BASE_INT_CASE(RTGETOPT_REQ_INT32 | RTGETOPT_FLAG_DEC, int32_t, i32, RTStrToInt32Full, 10)
833f83ce101b6e9168f519decc0dc7a1079d35f7vboxsync MY_BASE_INT_CASE(RTGETOPT_REQ_INT64 | RTGETOPT_FLAG_DEC, int64_t, i64, RTStrToInt64Full, 10)
ee6e48c229ef52aee5e968d956ebd066073811abvboxsync MY_BASE_INT_CASE(RTGETOPT_REQ_UINT8 | RTGETOPT_FLAG_DEC, uint8_t, u8, RTStrToUInt8Full, 10)
833f83ce101b6e9168f519decc0dc7a1079d35f7vboxsync MY_BASE_INT_CASE(RTGETOPT_REQ_UINT16 | RTGETOPT_FLAG_DEC, uint16_t, u16, RTStrToUInt16Full, 10)
833f83ce101b6e9168f519decc0dc7a1079d35f7vboxsync MY_BASE_INT_CASE(RTGETOPT_REQ_UINT32 | RTGETOPT_FLAG_DEC, uint32_t, u32, RTStrToUInt32Full, 10)
5eda82e218d35ae0691febd531e1bfc0324cc4a6vboxsync MY_BASE_INT_CASE(RTGETOPT_REQ_UINT64 | RTGETOPT_FLAG_DEC, uint64_t, u64, RTStrToUInt64Full, 10)
7f4b4ec1cadbf891d2756ce77dc4a4ec220a03bcvboxsync MY_BASE_INT_CASE(RTGETOPT_REQ_INT8 | RTGETOPT_FLAG_OCT, int8_t, i8, RTStrToInt8Full, 8)
7f4b4ec1cadbf891d2756ce77dc4a4ec220a03bcvboxsync MY_BASE_INT_CASE(RTGETOPT_REQ_INT16 | RTGETOPT_FLAG_OCT, int16_t, i16, RTStrToInt16Full, 8)
7f4b4ec1cadbf891d2756ce77dc4a4ec220a03bcvboxsync MY_BASE_INT_CASE(RTGETOPT_REQ_INT32 | RTGETOPT_FLAG_OCT, int32_t, i32, RTStrToInt32Full, 8)
7f4b4ec1cadbf891d2756ce77dc4a4ec220a03bcvboxsync MY_BASE_INT_CASE(RTGETOPT_REQ_INT64 | RTGETOPT_FLAG_OCT, int64_t, i64, RTStrToInt64Full, 8)
7f4b4ec1cadbf891d2756ce77dc4a4ec220a03bcvboxsync MY_BASE_INT_CASE(RTGETOPT_REQ_UINT8 | RTGETOPT_FLAG_OCT, uint8_t, u8, RTStrToUInt8Full, 8)
7f4b4ec1cadbf891d2756ce77dc4a4ec220a03bcvboxsync MY_BASE_INT_CASE(RTGETOPT_REQ_UINT16 | RTGETOPT_FLAG_OCT, uint16_t, u16, RTStrToUInt16Full, 8)
7f4b4ec1cadbf891d2756ce77dc4a4ec220a03bcvboxsync MY_BASE_INT_CASE(RTGETOPT_REQ_UINT32 | RTGETOPT_FLAG_OCT, uint32_t, u32, RTStrToUInt32Full, 8)
7f4b4ec1cadbf891d2756ce77dc4a4ec220a03bcvboxsync MY_BASE_INT_CASE(RTGETOPT_REQ_UINT64 | RTGETOPT_FLAG_OCT, uint64_t, u64, RTStrToUInt64Full, 8)
4d32c71a938b979c6aeee321ae325d5793a5ccdbvboxsync if (rtgetoptConvertIPv4Addr(pszValue, &Addr) != VINF_SUCCESS)
4d32c71a938b979c6aeee321ae325d5793a5ccdbvboxsync if (RT_FAILURE(RTCidrStrToIPv4(pszValue, &network, &netmask)))
4d32c71a938b979c6aeee321ae325d5793a5ccdbvboxsync if (rtgetoptConvertMacAddr(pszValue, &Addr) != VINF_SUCCESS)
4d32c71a938b979c6aeee321ae325d5793a5ccdbvboxsync if (RTUuidFromStr(&Uuid, pszValue) != VINF_SUCCESS)
7f4b4ec1cadbf891d2756ce77dc4a4ec220a03bcvboxsync * Moves one argv option entries.
7f4b4ec1cadbf891d2756ce77dc4a4ec220a03bcvboxsync * @param papszTo Destination.
4d32c71a938b979c6aeee321ae325d5793a5ccdbvboxsync * @param papszFrom Source.
4d32c71a938b979c6aeee321ae325d5793a5ccdbvboxsyncstatic void rtGetOptMoveArgvEntries(char **papszTo, char **papszFrom)
4d32c71a938b979c6aeee321ae325d5793a5ccdbvboxsync memmove(&papszTo[1], &papszTo[0], (uintptr_t)papszFrom - (uintptr_t)papszTo);
4d32c71a938b979c6aeee321ae325d5793a5ccdbvboxsyncRTDECL(int) RTGetOpt(PRTGETOPTSTATE pState, PRTGETOPTUNION pValueUnion)
4d32c71a938b979c6aeee321ae325d5793a5ccdbvboxsync * Reset the variables kept in state.
4d32c71a938b979c6aeee321ae325d5793a5ccdbvboxsync * Make sure the union is completely cleared out, whatever happens below.
00cc5d93f7446d9394a0f6b7ad790b9fb9d6005cvboxsync * The next option.
00cc5d93f7446d9394a0f6b7ad790b9fb9d6005cvboxsync * We've got short options left over from the previous call.
00cc5d93f7446d9394a0f6b7ad790b9fb9d6005cvboxsync pOpt = rtGetOptSearchShort(*pState->pszNextShort, pState->paOptions, pState->cOptions, pState->fFlags);
833f83ce101b6e9168f519decc0dc7a1079d35f7vboxsync * Pop off the next argument. Sorting options and dealing with the
5eda82e218d35ae0691febd531e1bfc0324cc4a6vboxsync * dash-dash makes this a little extra complicated.
ee6e48c229ef52aee5e968d956ebd066073811abvboxsync if (pState->iNext + pState->cNonOptions >= pState->argc)
833f83ce101b6e9168f519decc0dc7a1079d35f7vboxsync pszArgThis = pState->argv[iThis + pState->cNonOptions];
833f83ce101b6e9168f519decc0dc7a1079d35f7vboxsync * Do a long option search first and then a short option one.
833f83ce101b6e9168f519decc0dc7a1079d35f7vboxsync * This way we can make sure single dash long options doesn't
7f4b4ec1cadbf891d2756ce77dc4a4ec220a03bcvboxsync * get mixed up with short ones.
7f4b4ec1cadbf891d2756ce77dc4a4ec220a03bcvboxsync pOpt = rtGetOptSearchLong(pszArgThis, pState->paOptions, pState->cOptions, pState->fFlags);
db3d025f28c59aececbbda4174fa513496c89b2bvboxsync pOpt = rtGetOptSearchShort(pszArgThis[1], pState->paOptions, pState->cOptions, pState->fFlags);
e5bfc5c34142a7550be3564a8e01a037b1db5b31vboxsync /* Look for dash-dash. */
d60d5da33bb93fc7a8717802f21b13aa37914799vboxsync rtGetOptMoveArgvEntries(&pState->argv[iThis], &pState->argv[iThis + pState->cNonOptions]);
7f18b4c79440e724c5e08f007674ea7883ce6c01vboxsync /* Options first hacks. */
d60d5da33bb93fc7a8717802f21b13aa37914799vboxsync if (pState->fFlags & RTGETOPTINIT_FLAGS_OPTS_FIRST)
833f83ce101b6e9168f519decc0dc7a1079d35f7vboxsync rtGetOptMoveArgvEntries(&pState->argv[iThis], &pState->argv[iThis + pState->cNonOptions]);
5eda82e218d35ae0691febd531e1bfc0324cc4a6vboxsync /* not an option, add it to the non-options and try again. */
7f4b4ec1cadbf891d2756ce77dc4a4ec220a03bcvboxsync /* Switch to returning non-options if we've reached the end. */
7f4b4ec1cadbf891d2756ce77dc4a4ec220a03bcvboxsync if (pState->iNext + pState->cNonOptions >= pState->argc)
7f4b4ec1cadbf891d2756ce77dc4a4ec220a03bcvboxsync pValueUnion->pDef = pOpt; /* in case of no value or error. */
7f18b4c79440e724c5e08f007674ea7883ce6c01vboxsync if ((pOpt->fFlags & RTGETOPT_REQ_MASK) != RTGETOPT_REQ_NOTHING)
833f83ce101b6e9168f519decc0dc7a1079d35f7vboxsync * Find the argument value.
d60d5da33bb93fc7a8717802f21b13aa37914799vboxsync * A value is required with the argument. We're trying to be
e5bfc5c34142a7550be3564a8e01a037b1db5b31vboxsync * understanding here and will permit any of the following:
d60d5da33bb93fc7a8717802f21b13aa37914799vboxsync * -svalue, -s value, -s:value and -s=value
d60d5da33bb93fc7a8717802f21b13aa37914799vboxsync * (Ditto for long options.)
d60d5da33bb93fc7a8717802f21b13aa37914799vboxsync const char *pszValue;
d60d5da33bb93fc7a8717802f21b13aa37914799vboxsync pszValue = pState->argv[iThis + pState->cNonOptions + 1];
d60d5da33bb93fc7a8717802f21b13aa37914799vboxsync rtGetOptMoveArgvEntries(&pState->argv[iThis + 1], &pState->argv[iThis + pState->cNonOptions + 1]);
ad1e35f9fb71d147c2126449a25adf0f8e155aaavboxsync else /* same argument. */
ad1e35f9fb71d147c2126449a25adf0f8e155aaavboxsync pszValue = &pszArgThis[2 + (pszArgThis[2] == ':' || pszArgThis[2] == '=')];
d60d5da33bb93fc7a8717802f21b13aa37914799vboxsync int rc = RTStrToUInt32Ex(&pszArgThis[cchLong], &pszRet, 10, &uIndex);
d60d5da33bb93fc7a8717802f21b13aa37914799vboxsync pszValue = pState->argv[iThis + pState->cNonOptions + 1];
d60d5da33bb93fc7a8717802f21b13aa37914799vboxsync rtGetOptMoveArgvEntries(&pState->argv[iThis + 1], &pState->argv[iThis + pState->cNonOptions + 1]);
d60d5da33bb93fc7a8717802f21b13aa37914799vboxsync AssertMsgFailedReturn(("%s\n", pszArgThis), VERR_GETOPT_INVALID_ARGUMENT_FORMAT); /* search bug */
d60d5da33bb93fc7a8717802f21b13aa37914799vboxsync pszValue = pState->argv[iThis + pState->cNonOptions + 1];
d60d5da33bb93fc7a8717802f21b13aa37914799vboxsync rtGetOptMoveArgvEntries(&pState->argv[iThis + 1], &pState->argv[iThis + pState->cNonOptions + 1]);
d60d5da33bb93fc7a8717802f21b13aa37914799vboxsync else /* same argument. */
d60d5da33bb93fc7a8717802f21b13aa37914799vboxsync * Set up the ValueUnion.
e5bfc5c34142a7550be3564a8e01a037b1db5b31vboxsync int rc = rtGetOptProcessValue(pOpt->fFlags, pszValue, pValueUnion);
833f83ce101b6e9168f519decc0dc7a1079d35f7vboxsync * Deal with "compressed" short option lists, correcting the next
833f83ce101b6e9168f519decc0dc7a1079d35f7vboxsync * state variables for the start and end cases.
833f83ce101b6e9168f519decc0dc7a1079d35f7vboxsync /* start */
833f83ce101b6e9168f519decc0dc7a1079d35f7vboxsync if (RTStrToUInt32Full(&pszArgThis[cchLong], 10, &uIndex) == VINF_SUCCESS)
ab02aeaf2a0312fe9267a292e3911728e4531332vboxsync AssertMsgFailedReturn(("%s\n", pszArgThis), VERR_GETOPT_INVALID_ARGUMENT_FORMAT); /* search bug */
ad1e35f9fb71d147c2126449a25adf0f8e155aaavboxsync * Not a known option argument. If it starts with a switch char (-) we'll
ad1e35f9fb71d147c2126449a25adf0f8e155aaavboxsync * fail with unknown option, and if it doesn't we'll return it as a non-option.
ab02aeaf2a0312fe9267a292e3911728e4531332vboxsyncRTDECL(int) RTGetOptFetchValue(PRTGETOPTSTATE pState, PRTGETOPTUNION pValueUnion, uint32_t fFlags)
ab02aeaf2a0312fe9267a292e3911728e4531332vboxsync * Validate input.
c889bbab784ba8552102ce776b6c67b982017861vboxsync AssertReturn(!(fFlags & ~RTGETOPT_VALID_MASK), VERR_INVALID_PARAMETER);
ab02aeaf2a0312fe9267a292e3911728e4531332vboxsync AssertReturn((fFlags & RTGETOPT_REQ_MASK) != RTGETOPT_REQ_NOTHING, VERR_INVALID_PARAMETER);
81fcf0038d2d6c76ab2c8b02103dc18c24efe0a1vboxsync * Make sure the union is completely cleared out, whatever happens below.
4d32c71a938b979c6aeee321ae325d5793a5ccdbvboxsync * Pop off the next argument and convert it into a value union.
81fcf0038d2d6c76ab2c8b02103dc18c24efe0a1vboxsync const char *pszValue = pState->argv[iThis + (pState->cNonOptions != INT32_MAX ? pState->cNonOptions : 0)];
ab02aeaf2a0312fe9267a292e3911728e4531332vboxsync pValueUnion->pDef = pOpt; /* in case of no value or error. */
81fcf0038d2d6c76ab2c8b02103dc18c24efe0a1vboxsync if (pState->cNonOptions && pState->cNonOptions != INT32_MAX)
81fcf0038d2d6c76ab2c8b02103dc18c24efe0a1vboxsync rtGetOptMoveArgvEntries(&pState->argv[iThis], &pState->argv[iThis + pState->cNonOptions]);
4d32c71a938b979c6aeee321ae325d5793a5ccdbvboxsync return rtGetOptProcessValue(fFlags, pszValue, pValueUnion);
7f4b4ec1cadbf891d2756ce77dc4a4ec220a03bcvboxsyncRTDECL(RTEXITCODE) RTGetOptPrintError(int ch, PCRTGETOPTUNION pValueUnion)
00cc5d93f7446d9394a0f6b7ad790b9fb9d6005cvboxsync RTMsgError("Invalid parameter: %s", pValueUnion->psz);
00cc5d93f7446d9394a0f6b7ad790b9fb9d6005cvboxsync else if (ch > 0)
00cc5d93f7446d9394a0f6b7ad790b9fb9d6005cvboxsync RTMsgError("Unknown option: '%s'", pValueUnion->psz);
00cc5d93f7446d9394a0f6b7ad790b9fb9d6005cvboxsync else if (ch == VERR_GETOPT_INVALID_ARGUMENT_FORMAT)
00cc5d93f7446d9394a0f6b7ad790b9fb9d6005cvboxsync /** @todo r=klaus not really ideal, as the value isn't available */
00cc5d93f7446d9394a0f6b7ad790b9fb9d6005cvboxsync RTMsgError("The value given '%s' has an invalid format.", pValueUnion->pDef->pszLong);
00cc5d93f7446d9394a0f6b7ad790b9fb9d6005cvboxsync RTMsgError("%s: %Rrs\n", pValueUnion->pDef->pszLong, ch);