|
22 | 22 | #include <cstdlib>
|
23 | 23 | #include <fstream>
|
24 | 24 | #include <iostream>
|
25 |
| -#include <iterator> |
26 | 25 | #include <string>
|
27 | 26 | #include <tuple>
|
28 | 27 | #include <utility>
|
@@ -600,6 +599,34 @@ bool CanIgnoreUndefinedFlag(absl::string_view flag_name) {
|
600 | 599 | return false;
|
601 | 600 | }
|
602 | 601 |
|
| 602 | +// -------------------------------------------------------------------- |
| 603 | + |
| 604 | +void ReportUnrecognizedFlags( |
| 605 | + const std::vector<UnrecognizedFlag>& unrecognized_flags, |
| 606 | + bool report_as_fatal_error) { |
| 607 | + for (const auto& unrecognized : unrecognized_flags) { |
| 608 | + // Verify if flag_name has the "no" already removed |
| 609 | + std::vector<std::string> misspelling_hints; |
| 610 | + if (unrecognized.source == UnrecognizedFlag::kFromArgv) { |
| 611 | + misspelling_hints = |
| 612 | + flags_internal::GetMisspellingHints(unrecognized.flag_name); |
| 613 | + } |
| 614 | + |
| 615 | + if (misspelling_hints.empty()) { |
| 616 | + flags_internal::ReportUsageError( |
| 617 | + absl::StrCat("Unknown command line flag '", unrecognized.flag_name, |
| 618 | + "'"), |
| 619 | + report_as_fatal_error); |
| 620 | + } else { |
| 621 | + flags_internal::ReportUsageError( |
| 622 | + absl::StrCat("Unknown command line flag '", unrecognized.flag_name, |
| 623 | + "'. Did you mean: ", |
| 624 | + absl::StrJoin(misspelling_hints, ", "), " ?"), |
| 625 | + report_as_fatal_error); |
| 626 | + } |
| 627 | + } |
| 628 | +} |
| 629 | + |
603 | 630 | } // namespace
|
604 | 631 |
|
605 | 632 | // --------------------------------------------------------------------
|
@@ -674,12 +701,15 @@ std::vector<char*> ParseCommandLineImpl(int argc, char* argv[],
|
674 | 701 | argc, argv, positional_args, unrecognized_flags);
|
675 | 702 |
|
676 | 703 | if (undef_flag_action != OnUndefinedFlag::kIgnoreUndefined) {
|
677 |
| - ReportUnrecognizedFlags( |
678 |
| - unrecognized_flags, |
679 |
| - undef_flag_action == |
680 |
| - flags_internal::OnUndefinedFlag::kAbortIfUndefined); |
| 704 | + if (parse_successful && |
| 705 | + undef_flag_action == OnUndefinedFlag::kAbortIfUndefined) { |
| 706 | + if (!unrecognized_flags.empty()) { parse_successful = false; } |
| 707 | + } |
681 | 708 |
|
682 |
| - if (!unrecognized_flags.empty()) { parse_successful = false; } |
| 709 | + flags_internal::ReportUnrecognizedFlags( |
| 710 | + unrecognized_flags, |
| 711 | + !parse_successful && |
| 712 | + (undef_flag_action == OnUndefinedFlag::kAbortIfUndefined)); |
683 | 713 | }
|
684 | 714 |
|
685 | 715 | #if ABSL_FLAGS_STRIP_NAMES
|
@@ -871,29 +901,8 @@ bool ParseAbseilFlagsOnly(int argc, char* argv[],
|
871 | 901 | // --------------------------------------------------------------------
|
872 | 902 |
|
873 | 903 | void ReportUnrecognizedFlags(
|
874 |
| - const std::vector<UnrecognizedFlag>& unrecognized_flags, |
875 |
| - bool report_fatal_error) { |
876 |
| - for (const auto& unrecognized : unrecognized_flags) { |
877 |
| - // Verify if flag_name has the "no" already removed |
878 |
| - std::vector<std::string> misspelling_hints; |
879 |
| - if (unrecognized.source == UnrecognizedFlag::kFromArgv) { |
880 |
| - misspelling_hints = |
881 |
| - flags_internal::GetMisspellingHints(unrecognized.flag_name); |
882 |
| - } |
883 |
| - |
884 |
| - if (misspelling_hints.empty()) { |
885 |
| - flags_internal::ReportUsageError( |
886 |
| - absl::StrCat("Unknown command line flag '", unrecognized.flag_name, |
887 |
| - "'"), |
888 |
| - report_fatal_error); |
889 |
| - } else { |
890 |
| - flags_internal::ReportUsageError( |
891 |
| - absl::StrCat("Unknown command line flag '", unrecognized.flag_name, |
892 |
| - "'. Did you mean: ", |
893 |
| - absl::StrJoin(misspelling_hints, ", "), " ?"), |
894 |
| - report_fatal_error); |
895 |
| - } |
896 |
| - } |
| 904 | + const std::vector<UnrecognizedFlag>& unrecognized_flags) { |
| 905 | + flags_internal::ReportUnrecognizedFlags(unrecognized_flags, true); |
897 | 906 | }
|
898 | 907 |
|
899 | 908 | // --------------------------------------------------------------------
|
|
0 commit comments