comparison lwlib/lw_cmdline.c @ 103:8b0be0fc42cf

Fixed arg handling for short args in command line option parser - optional args for short opts should now work and options no longer need a space between option character and argument
author lost@l-w.ca
date Sat, 06 Aug 2011 22:35:58 -0600
parents 2077b755b8b4
children bc82df7f6bbe
comparison
equal deleted inserted replaced
102:f7a87c3a8e0e 103:8b0be0fc42cf
393 for (j = 0; parser -> options[j].name; j++) 393 for (j = 0; parser -> options[j].name; j++)
394 if (parser -> options[j].key == argv[i][cch]) 394 if (parser -> options[j].key == argv[i][cch])
395 break; 395 break;
396 cch++; 396 cch++;
397 tstr = argv[i] + cch; 397 tstr = argv[i] + cch;
398 if (!*tstr) 398 if (!*tstr && (parser -> options[j].flags & lw_cmdline_opt_optional) == 0)
399 { 399 {
400 /* only consume the next arg if the argument is optional */
400 if (nextarg < argc) 401 if (nextarg < argc)
401 tstr = argv[nextarg]; 402 tstr = argv[nextarg];
402 else 403 else
403 tstr = NULL; 404 tstr = NULL;
404 } 405 }
436 /* cch will be zero and tstr will point to the arg if it's a long option */ 437 /* cch will be zero and tstr will point to the arg if it's a long option */
437 /* cch will be > 0 and tstr points to the theoretical option, either within */ 438 /* cch will be > 0 and tstr points to the theoretical option, either within */
438 /* this string or "nextarg" */ 439 /* this string or "nextarg" */
439 if (parser -> options[j].name == NULL) 440 if (parser -> options[j].name == NULL)
440 { 441 {
441 fprintf(stderr, "Unknown option. See %s --usage.\n", argv[0]); 442 if (cch)
443 fprintf(stderr, "Unknown option '%c'. See %s --usage.\n", argv[i][cch - 1], argv[0]);
444 else
445 fprintf(stderr, "Unknown option '%s'. See %s --usage.\n", argv[i - 1], argv[0]);
442 exit(1); 446 exit(1);
443 } 447 }
444 if (parser -> options[j].arg) 448 if (parser -> options[j].arg)
445 { 449 {
446 if (tstr && cch && argv[i][cch] == 0) 450 if (tstr && cch && argv[i][cch] == 0)
447 nextarg++; 451 nextarg++;
448 452
449 if (!*tstr) 453 if (!*tstr)
450 tstr = NULL; 454 tstr = NULL;
451 455
456 /* move on to next argument if we have an arg specified */
457 if (tstr && cch && argv[i][cch] != 0)
458 i++;
459
452 if (!tstr && (parser -> options[j].flags & lw_cmdline_opt_optional) == 0) 460 if (!tstr && (parser -> options[j].flags & lw_cmdline_opt_optional) == 0)
453 { 461 {
454 fprintf(stderr, "Option %s requires argument.\n", parser -> options[j].name); 462 fprintf(stderr, "Option %s requires argument.\n", parser -> options[j].name);
455 } 463 }
456 } 464 }