Commit 6db185d8 by Gennadiy Rozental Committed by Copybara-Service

Correct semantic and documentation for the ReportUnrecognizedFlags interface

PiperOrigin-RevId: 516411395
Change-Id: I442fc9435bd7a829802c325e2e4106aa6775c263
parent 52578edd
...@@ -22,7 +22,6 @@ ...@@ -22,7 +22,6 @@
#include <cstdlib> #include <cstdlib>
#include <fstream> #include <fstream>
#include <iostream> #include <iostream>
#include <iterator>
#include <string> #include <string>
#include <tuple> #include <tuple>
#include <utility> #include <utility>
...@@ -600,6 +599,34 @@ bool CanIgnoreUndefinedFlag(absl::string_view flag_name) { ...@@ -600,6 +599,34 @@ bool CanIgnoreUndefinedFlag(absl::string_view flag_name) {
return false; return false;
} }
// --------------------------------------------------------------------
void ReportUnrecognizedFlags(
const std::vector<UnrecognizedFlag>& unrecognized_flags,
bool report_as_fatal_error) {
for (const auto& unrecognized : unrecognized_flags) {
// Verify if flag_name has the "no" already removed
std::vector<std::string> 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 } // namespace
// -------------------------------------------------------------------- // --------------------------------------------------------------------
...@@ -674,12 +701,15 @@ std::vector<char*> ParseCommandLineImpl(int argc, char* argv[], ...@@ -674,12 +701,15 @@ std::vector<char*> ParseCommandLineImpl(int argc, char* argv[],
argc, argv, positional_args, unrecognized_flags); argc, argv, positional_args, unrecognized_flags);
if (undef_flag_action != OnUndefinedFlag::kIgnoreUndefined) { if (undef_flag_action != OnUndefinedFlag::kIgnoreUndefined) {
ReportUnrecognizedFlags( if (parse_successful &&
unrecognized_flags, undef_flag_action == OnUndefinedFlag::kAbortIfUndefined) {
undef_flag_action == if (!unrecognized_flags.empty()) { parse_successful = false; }
flags_internal::OnUndefinedFlag::kAbortIfUndefined); }
if (!unrecognized_flags.empty()) { parse_successful = false; } flags_internal::ReportUnrecognizedFlags(
unrecognized_flags,
!parse_successful &&
(undef_flag_action == OnUndefinedFlag::kAbortIfUndefined));
} }
#if ABSL_FLAGS_STRIP_NAMES #if ABSL_FLAGS_STRIP_NAMES
...@@ -871,29 +901,8 @@ bool ParseAbseilFlagsOnly(int argc, char* argv[], ...@@ -871,29 +901,8 @@ bool ParseAbseilFlagsOnly(int argc, char* argv[],
// -------------------------------------------------------------------- // --------------------------------------------------------------------
void ReportUnrecognizedFlags( void ReportUnrecognizedFlags(
const std::vector<UnrecognizedFlag>& unrecognized_flags, const std::vector<UnrecognizedFlag>& unrecognized_flags) {
bool report_fatal_error) { flags_internal::ReportUnrecognizedFlags(unrecognized_flags, true);
for (const auto& unrecognized : unrecognized_flags) {
// Verify if flag_name has the "no" already removed
std::vector<std::string> 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);
}
}
} }
// -------------------------------------------------------------------- // --------------------------------------------------------------------
......
...@@ -90,17 +90,10 @@ bool ParseAbseilFlagsOnly(int argc, char* argv[], ...@@ -90,17 +90,10 @@ bool ParseAbseilFlagsOnly(int argc, char* argv[],
// ReportUnrecognizedFlags() // ReportUnrecognizedFlags()
// //
// Reports an error for all non-ignored unrecognized flags in the provided // Reports an error to `stderr` for all non-ignored unrecognized flags in
// `unrecognized_flags` list. // the provided `unrecognized_flags` list.
//
// If `report_fatal_error` is true, the fatal error is reported and program is
// aborted. Otherwise non-fatal error is reported for all flags.
//
// This function returns true if any non-ignored unrecognized flags were
// located in the list and false otherwise.
void ReportUnrecognizedFlags( void ReportUnrecognizedFlags(
const std::vector<UnrecognizedFlag>& unrecognized_flags, const std::vector<UnrecognizedFlag>& unrecognized_flags);
bool report_fatal_error);
// ParseCommandLine() // ParseCommandLine()
// //
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment