Changeset 23644 in vbox for trunk/src/VBox/Runtime/common
- Timestamp:
- Oct 9, 2009 12:28:56 PM (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Runtime/common/misc/getopt.cpp
r21337 r23644 192 192 * A value is required with the argument. We're trying to be very 193 193 * understanding here and will permit any of the following: 194 * --long:value, --long=value, --long value, 195 * --long: value, --long= value 194 * --long12:value, --long12=value, --long12 value, 195 * --long:value, --long=value, --long value, 196 * --long: value, --long= value 197 * 198 * If the option is index, then all trailing chars must be 199 * digits. For error reporting reasons we also match where 200 * there is no index. 196 201 */ 197 202 size_t cchLong = strlen(pOpt->pszLong); 198 if ( !strncmp(pszOption, pOpt->pszLong, cchLong) 199 && ( pszOption[cchLong] == '\0' 203 if (!strncmp(pszOption, pOpt->pszLong, cchLong)) 204 { 205 if (pOpt->fFlags & RTGETOPT_FLAG_INDEX) 206 while (RT_C_IS_DIGIT(pszOption[cchLong])) 207 cchLong++; 208 if ( pszOption[cchLong] == '\0' 200 209 || pszOption[cchLong] == ':' 201 || pszOption[cchLong] == '=')) 202 return pOpt; 210 || pszOption[cchLong] == '=') 211 return pOpt; 212 } 213 } 214 else if (pOpt->fFlags & RTGETOPT_FLAG_INDEX) 215 { 216 /* 217 * The option takes an index but no value. 218 * As above, we also match where there is no index. 219 */ 220 size_t cchLong = strlen(pOpt->pszLong); 221 if (!strncmp(pszOption, pOpt->pszLong, cchLong)) 222 { 223 while (RT_C_IS_DIGIT(pszOption[cchLong])) 224 cchLong++; 225 if (pszOption[cchLong] == '\0') 226 return pOpt; 227 } 203 228 } 204 229 else if (!strcmp(pszOption, pOpt->pszLong)) … … 234 259 RTDECL(int) RTGetOpt(PRTGETOPTSTATE pState, PRTGETOPTUNION pValueUnion) 235 260 { 261 /* 262 * Reset the variables kept in state. 263 */ 236 264 pState->pDef = NULL; 265 pState->uIndex = UINT64_MAX; 266 237 267 /* 238 268 * Make sure the union is completely cleared out, whatever happens below. … … 338 368 { 339 369 size_t cchLong = strlen(pOpt->pszLong); 340 if ( pszArgThis[cchLong] == '\0' 341 || pszArgThis[cchLong + 1] == '\0') 342 { 343 if (iThis + 1 >= pState->argc) 344 return VERR_GETOPT_REQUIRED_ARGUMENT_MISSING; 345 pszValue = pState->argv[iThis + 1]; 346 pState->iNext++; 347 } 348 else /* same argument. */ 349 pszValue = &pszArgThis[cchLong + 1]; 370 if (pOpt->fFlags & RTGETOPT_FLAG_INDEX) 371 { 372 373 if (pszArgThis[cchLong] == '\0') 374 return VERR_GETOPT_INDEX_MISSING; 375 376 uint64_t uIndex; 377 char *pszRet = NULL; 378 int rc = RTStrToUInt64Ex(&pszArgThis[cchLong], &pszRet, 10, &uIndex); 379 if (rc == VWRN_TRAILING_CHARS) 380 { 381 if ( pszRet[0] != ':' 382 && pszRet[0] != '=') 383 return VERR_GETOPT_INVALID_ARGUMENT_FORMAT; 384 pState->uIndex = uIndex; 385 pszValue = pszRet + 1; 386 } 387 else if (rc == VINF_SUCCESS) 388 { 389 if (iThis + 1 >= pState->argc) 390 return VERR_GETOPT_REQUIRED_ARGUMENT_MISSING; 391 pState->uIndex = uIndex; 392 pszValue = pState->argv[iThis + 1]; 393 pState->iNext++; 394 } 395 else 396 AssertMsgFailedReturn(("%s\n", pszArgThis), VERR_GETOPT_INVALID_ARGUMENT_FORMAT); /* search bug */ 397 } 398 else 399 { 400 if ( pszArgThis[cchLong] == '\0' 401 || pszArgThis[cchLong + 1] == '\0') 402 { 403 if (iThis + 1 >= pState->argc) 404 return VERR_GETOPT_REQUIRED_ARGUMENT_MISSING; 405 pszValue = pState->argv[iThis + 1]; 406 pState->iNext++; 407 } 408 else /* same argument. */ 409 pszValue = &pszArgThis[cchLong + 1]; 410 } 350 411 } 351 412 … … 356 417 * generic ints as octals. 357 418 */ 358 switch (pOpt->fFlags & (RTGETOPT_REQ_MASK | RTGETOPT_FLAG_HEX | RTGETOPT_FLAG_OCT | RTGETOPT_FLAG_DEC)) 419 switch (pOpt->fFlags & ( RTGETOPT_REQ_MASK 420 | RTGETOPT_FLAG_HEX 421 | RTGETOPT_FLAG_DEC 422 | RTGETOPT_FLAG_OCT)) 359 423 { 360 424 case RTGETOPT_REQ_STRING: … … 480 544 } 481 545 } 546 else if (pOpt->fFlags & RTGETOPT_FLAG_INDEX) 547 { 548 size_t cchLong = strlen(pOpt->pszLong); 549 if (pszArgThis[cchLong] == '\0') 550 return VERR_GETOPT_INDEX_MISSING; 551 552 uint64_t uIndex; 553 char *pszRet = NULL; 554 if (RTStrToUInt64Full(&pszArgThis[cchLong], 10, &uIndex) == VINF_SUCCESS) 555 pState->uIndex = uIndex; 556 else 557 AssertMsgFailedReturn(("%s\n", pszArgThis), VERR_GETOPT_INVALID_ARGUMENT_FORMAT); /* search bug */ 558 } 482 559 483 560 pState->pDef = pOpt;
Note:
See TracChangeset
for help on using the changeset viewer.