--------------------------------------------------------------------------- ^FUNCTION: OptionSpec::Format - format an option-spec for a usage message ^SYNOPSIS: unsigned OptionSpec::Format(buf, optctrls) const ^PARAMETERS: char * buf -- where to print the formatted option unsigned optctrls -- option-parsing configuration flags ^DESCRIPTION: Self-explanatory. ^REQUIREMENTS: - buf must be large
| 521 | // Follow along in the source - it's not hard but it is tedious! |
| 522 | // ^^------------------------------------------------------------------------- |
| 523 | unsigned OptionSpec::Format(char *buf, unsigned optctrls) const |
| 524 | { |
| 525 | #ifdef NO_USAGE |
| 526 | return (*buf = '\0'); |
| 527 | #else |
| 528 | static char default_value[] = "<value>"; |
| 529 | if (isHiddenOpt()) |
| 530 | return (unsigned)(*buf = '\0'); |
| 531 | char optchar = OptChar(); |
| 532 | const char *longopt = LongOpt(); |
| 533 | char *p = buf; |
| 534 | |
| 535 | const char *value = NULLSTR; |
| 536 | int longopt_len = 0; |
| 537 | int value_len = 0; |
| 538 | |
| 539 | if (longopt) |
| 540 | { |
| 541 | value = ::strchr(longopt, ' '); |
| 542 | longopt_len = (value) ? (int)(value - longopt) : (int)::strlen(longopt); |
| 543 | } |
| 544 | else |
| 545 | { |
| 546 | value = ::strchr(spec + 1, ' '); |
| 547 | } |
| 548 | while (value && (*value == ' ')) |
| 549 | ++value; |
| 550 | if (value && *value) |
| 551 | { |
| 552 | value_len = (int)::strlen(value); |
| 553 | } |
| 554 | else |
| 555 | { |
| 556 | value = default_value; |
| 557 | value_len = sizeof(default_value) - 1; |
| 558 | } |
| 559 | |
| 560 | if ((optctrls & static_cast<signed>(Options::OptCtrl::SHORT_ONLY)) && |
| 561 | ((!isNullOpt(optchar)) || (optctrls & static_cast<signed>(Options::OptCtrl::NOGUESSING)))) |
| 562 | { |
| 563 | longopt = NULLSTR; |
| 564 | } |
| 565 | if ((optctrls & static_cast<signed>(Options::OptCtrl::LONG_ONLY)) && |
| 566 | (longopt || (optctrls & static_cast<signed>(Options::OptCtrl::NOGUESSING)))) |
| 567 | { |
| 568 | optchar = '\0'; |
| 569 | } |
| 570 | if (isNullOpt(optchar) && (longopt == NULL)) |
| 571 | { |
| 572 | *buf = '\0'; |
| 573 | return 0; |
| 574 | } |
| 575 | |
| 576 | *(p++) = '['; |
| 577 | |
| 578 | // print the single character option |
| 579 | if (!isNullOpt(optchar)) |
| 580 | { |