VirtualBox

Ignore:
Timestamp:
Sep 14, 2022 9:14:53 AM (2 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
153615
Message:

IPRT/getopt: Added index variant with 0 or 1 and optionally and w/ implicit dash. Thought I needed for converting some options to use indexes, but here it is anyways.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Runtime/common/misc/getopt.cpp

    r96407 r96729  
    102102    {
    103103        Assert(!(paOptions[i].fFlags & ~RTGETOPT_VALID_MASK));
     104        Assert(   !(paOptions[i].fFlags & (RTGETOPT_FLAG_INDEX_DEF_MASK | RTGETOPT_FLAG_INDEX_DEF_DASH))
     105               || (paOptions[i].fFlags & RTGETOPT_FLAG_INDEX) );
    104106        Assert(paOptions[i].iShort > 0);
    105107        Assert(paOptions[i].iShort != VINF_GETOPT_NOT_OPTION);
     
    175177        if (pOpt->pszLong)
    176178        {
    177             if ((pOpt->fFlags & RTGETOPT_REQ_MASK) != RTGETOPT_REQ_NOTHING)
     179            uint32_t const fOptFlags = pOpt->fFlags;
     180            if ((fOptFlags & RTGETOPT_REQ_MASK) != RTGETOPT_REQ_NOTHING)
    178181            {
    179182                /*
     
    189192                size_t cchLong = strlen(pOpt->pszLong);
    190193                if (   !strncmp(pszOption, pOpt->pszLong, cchLong)
    191                     || (   pOpt->fFlags & RTGETOPT_FLAG_ICASE
     194                    || (   (fOptFlags & RTGETOPT_FLAG_ICASE)
    192195                        && !RTStrNICmp(pszOption, pOpt->pszLong, cchLong)))
    193196                {
    194                     if (pOpt->fFlags & RTGETOPT_FLAG_INDEX)
     197                    if (   (fOptFlags & RTGETOPT_FLAG_INDEX_DEF_DASH)
     198                        && pszOption[cchLong] == '-'
     199                        && RT_C_IS_DIGIT(pszOption[cchLong + 1])) /* given "--long" we match "--long-1" but not "--long-". */
     200                        cchLong++;
     201                    if (fOptFlags & RTGETOPT_FLAG_INDEX)
    195202                        while (RT_C_IS_DIGIT(pszOption[cchLong]))
    196203                            cchLong++;
     
    201208                }
    202209            }
    203             else if (pOpt->fFlags & RTGETOPT_FLAG_INDEX)
     210            else if (fOptFlags & RTGETOPT_FLAG_INDEX)
    204211            {
    205212                /*
     
    209216                size_t cchLong = strlen(pOpt->pszLong);
    210217                if (   !strncmp(pszOption, pOpt->pszLong, cchLong)
    211                     || (   pOpt->fFlags & RTGETOPT_FLAG_ICASE
     218                    || (   (fOptFlags & RTGETOPT_FLAG_ICASE)
    212219                        && !RTStrNICmp(pszOption, pOpt->pszLong, cchLong)))
    213220                {
     221                    if (   (fOptFlags & RTGETOPT_FLAG_INDEX_DEF_DASH)
     222                        && pszOption[cchLong] == '-'
     223                        && RT_C_IS_DIGIT(pszOption[cchLong + 1]))
     224                        cchLong++;
    214225                    while (RT_C_IS_DIGIT(pszOption[cchLong]))
    215226                        cchLong++;
     
    219230            }
    220231            else if (   !strcmp(pszOption, pOpt->pszLong)
    221                      || (   pOpt->fFlags & RTGETOPT_FLAG_ICASE
     232                     || (   (fOptFlags & RTGETOPT_FLAG_ICASE)
    222233                         && !RTStrICmp(pszOption, pOpt->pszLong)))
    223234                return pOpt;
     
    660671        pValueUnion->pDef = pOpt; /* in case of no value or error. */
    661672
    662         if ((pOpt->fFlags & RTGETOPT_REQ_MASK) != RTGETOPT_REQ_NOTHING)
     673        uint32_t const fOptFlags = pOpt->fFlags;
     674        if ((fOptFlags & RTGETOPT_REQ_MASK) != RTGETOPT_REQ_NOTHING)
    663675        {
    664676            /*
     
    692704            {
    693705                size_t cchLong = strlen(pOpt->pszLong);
    694                 if (pOpt->fFlags & RTGETOPT_FLAG_INDEX)
    695                 {
    696 
    697                     if (pszArgThis[cchLong] == '\0')
    698                         return VERR_GETOPT_INDEX_MISSING;
    699 
    700                     uint32_t uIndex;
    701                     char *pszRet = NULL;
    702                     int rc = RTStrToUInt32Ex(&pszArgThis[cchLong], &pszRet, 10, &uIndex);
    703                     if (rc == VWRN_TRAILING_CHARS)
     706                if (fOptFlags & RTGETOPT_FLAG_INDEX)
     707                {
     708                    if (   pszArgThis[cchLong] != '\0'
     709                        || (fOptFlags & RTGETOPT_FLAG_INDEX_DEF_MASK))
    704710                    {
    705                         if (   pszRet[0] != ':'
    706                             && pszRet[0] != '=')
    707                             return VERR_GETOPT_INVALID_ARGUMENT_FORMAT;
    708                         pState->uIndex = uIndex;
    709                         pszValue = pszRet + 1;
    710                     }
    711                     else if (rc == VINF_SUCCESS)
    712                     {
    713                         if (iThis + 1 + pState->cNonOptions >= pState->argc)
    714                             return VERR_GETOPT_REQUIRED_ARGUMENT_MISSING;
    715                         pState->uIndex = uIndex;
    716                         pszValue = pState->argv[iThis + pState->cNonOptions + 1];
    717                         rtGetOptMoveArgvEntries(&pState->argv[iThis + 1], &pState->argv[iThis + pState->cNonOptions + 1]);
    718                         pState->iNext++;
     711                        if (   (fOptFlags & RTGETOPT_FLAG_INDEX_DEF_DASH)
     712                            && pszArgThis[cchLong] == '-')
     713                            cchLong++;
     714
     715                        uint32_t uIndex;
     716                        char *pszRet = NULL;
     717                        int rc = RTStrToUInt32Ex(&pszArgThis[cchLong], &pszRet, 10, &uIndex);
     718                        if (   rc == VERR_NO_DIGITS
     719                            && (fOptFlags & RTGETOPT_FLAG_INDEX_DEF_MASK))
     720                        {
     721                            uIndex = ((fOptFlags & RTGETOPT_FLAG_INDEX_DEF_MASK) >> RTGETOPT_FLAG_INDEX_DEF_SHIFT) - 1;
     722                            rc = pszRet[0] == '\0' ? VINF_SUCCESS : VWRN_TRAILING_CHARS;
     723                        }
     724                        if (rc == VWRN_TRAILING_CHARS)
     725                        {
     726                            if (   pszRet[0] != ':'
     727                                && pszRet[0] != '=')
     728                                return VERR_GETOPT_INVALID_ARGUMENT_FORMAT;
     729                            pState->uIndex = uIndex;
     730                            pszValue = pszRet + 1;
     731                        }
     732                        else if (rc == VINF_SUCCESS)
     733                        {
     734                            if (iThis + 1 + pState->cNonOptions >= pState->argc)
     735                                return VERR_GETOPT_REQUIRED_ARGUMENT_MISSING;
     736                            pState->uIndex = uIndex;
     737                            pszValue = pState->argv[iThis + pState->cNonOptions + 1];
     738                            rtGetOptMoveArgvEntries(&pState->argv[iThis + 1], &pState->argv[iThis + pState->cNonOptions + 1]);
     739                            pState->iNext++;
     740                        }
     741                        else
     742                            AssertMsgFailedReturn(("%s\n", pszArgThis), VERR_GETOPT_INVALID_ARGUMENT_FORMAT); /* search bug */
    719743                    }
    720744                    else
    721                         AssertMsgFailedReturn(("%s\n", pszArgThis), VERR_GETOPT_INVALID_ARGUMENT_FORMAT); /* search bug */
     745                        return VERR_GETOPT_INDEX_MISSING;
    722746                }
    723747                else
     
    739763             * Set up the ValueUnion.
    740764             */
    741             int rc = rtGetOptProcessValue(pOpt->fFlags, pszValue, pValueUnion);
     765            int rc = rtGetOptProcessValue(fOptFlags, pszValue, pValueUnion);
    742766            if (RT_FAILURE(rc))
    743767                return rc;
     
    765789            }
    766790        }
    767         else if (pOpt->fFlags & RTGETOPT_FLAG_INDEX)
    768         {
    769             size_t cchLong = strlen(pOpt->pszLong);
    770             if (pszArgThis[cchLong] == '\0')
     791        else if (fOptFlags & RTGETOPT_FLAG_INDEX)
     792        {
     793            size_t   cchLong = strlen(pOpt->pszLong);
     794            uint32_t uIndex;
     795            if (pszArgThis[cchLong] != '\0')
     796            {
     797                if (   (fOptFlags & RTGETOPT_FLAG_INDEX_DEF_DASH)
     798                    && pszArgThis[cchLong] == '-')
     799                    cchLong++;
     800                if (RTStrToUInt32Full(&pszArgThis[cchLong], 10, &uIndex) == VINF_SUCCESS)
     801                    pState->uIndex = uIndex;
     802                else
     803                    AssertMsgFailedReturn(("%s\n", pszArgThis), VERR_GETOPT_INVALID_ARGUMENT_FORMAT); /* search bug */
     804            }
     805            else if (fOptFlags & RTGETOPT_FLAG_INDEX_DEF_MASK)
     806                uIndex = ((fOptFlags & RTGETOPT_FLAG_INDEX_DEF_MASK) >> RTGETOPT_FLAG_INDEX_DEF_SHIFT) - 1;
     807            else
    771808                return VERR_GETOPT_INDEX_MISSING;
    772 
    773             uint32_t uIndex;
    774             if (RTStrToUInt32Full(&pszArgThis[cchLong], 10, &uIndex) == VINF_SUCCESS)
    775                 pState->uIndex = uIndex;
    776             else
    777                 AssertMsgFailedReturn(("%s\n", pszArgThis), VERR_GETOPT_INVALID_ARGUMENT_FORMAT); /* search bug */
    778809        }
    779810
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette