From 6db185d8e2ba93adf32d689fd702ee0465b2e5ec Mon Sep 17 00:00:00 2001 From: Gennadiy Rozental Date: Mon, 13 Mar 2023 21:22:11 -0700 Subject: Correct semantic and documentation for the ReportUnrecognizedFlags interface PiperOrigin-RevId: 516411395 Change-Id: I442fc9435bd7a829802c325e2e4106aa6775c263 --- absl/flags/parse.cc | 67 ++++++++++++++++++++++++++++++----------------------- 1 file changed, 38 insertions(+), 29 deletions(-) (limited to 'absl/flags/parse.cc') diff --git a/absl/flags/parse.cc b/absl/flags/parse.cc index fcd6a52d..236d31b7 100644 --- a/absl/flags/parse.cc +++ b/absl/flags/parse.cc @@ -22,7 +22,6 @@ #include #include #include -#include #include #include #include @@ -600,6 +599,34 @@ bool CanIgnoreUndefinedFlag(absl::string_view flag_name) { return false; } +// -------------------------------------------------------------------- + +void ReportUnrecognizedFlags( + const std::vector& unrecognized_flags, + bool report_as_fatal_error) { + for (const auto& unrecognized : unrecognized_flags) { + // Verify if flag_name has the "no" already removed + std::vector misspelling_hints; + if (unrecognized.source == UnrecognizedFlag::kFromArgv) { + misspelling_hints = + flags_internal::GetMisspellingHints(unrecognized.flag_name); + } + + if (misspelling_hints.empty()) { + flags_internal::ReportUsageError( + absl::StrCat("Unknown command line flag '", unrecognized.flag_name, + "'"), + report_as_fatal_error); + } else { + flags_internal::ReportUsageError( + absl::StrCat("Unknown command line flag '", unrecognized.flag_name, + "'. Did you mean: ", + absl::StrJoin(misspelling_hints, ", "), " ?"), + report_as_fatal_error); + } + } +} + } // namespace // -------------------------------------------------------------------- @@ -674,12 +701,15 @@ std::vector ParseCommandLineImpl(int argc, char* argv[], argc, argv, positional_args, unrecognized_flags); if (undef_flag_action != OnUndefinedFlag::kIgnoreUndefined) { - ReportUnrecognizedFlags( - unrecognized_flags, - undef_flag_action == - flags_internal::OnUndefinedFlag::kAbortIfUndefined); + if (parse_successful && + undef_flag_action == OnUndefinedFlag::kAbortIfUndefined) { + if (!unrecognized_flags.empty()) { parse_successful = false; } + } - if (!unrecognized_flags.empty()) { parse_successful = false; } + flags_internal::ReportUnrecognizedFlags( + unrecognized_flags, + !parse_successful && + (undef_flag_action == OnUndefinedFlag::kAbortIfUndefined)); } #if ABSL_FLAGS_STRIP_NAMES @@ -871,29 +901,8 @@ bool ParseAbseilFlagsOnly(int argc, char* argv[], // -------------------------------------------------------------------- void ReportUnrecognizedFlags( - const std::vector& unrecognized_flags, - bool report_fatal_error) { - for (const auto& unrecognized : unrecognized_flags) { - // Verify if flag_name has the "no" already removed - std::vector misspelling_hints; - if (unrecognized.source == UnrecognizedFlag::kFromArgv) { - misspelling_hints = - flags_internal::GetMisspellingHints(unrecognized.flag_name); - } - - if (misspelling_hints.empty()) { - flags_internal::ReportUsageError( - absl::StrCat("Unknown command line flag '", unrecognized.flag_name, - "'"), - report_fatal_error); - } else { - flags_internal::ReportUsageError( - absl::StrCat("Unknown command line flag '", unrecognized.flag_name, - "'. Did you mean: ", - absl::StrJoin(misspelling_hints, ", "), " ?"), - report_fatal_error); - } - } + const std::vector& unrecognized_flags) { + flags_internal::ReportUnrecognizedFlags(unrecognized_flags, true); } // -------------------------------------------------------------------- -- cgit v1.2.3