Skip to content

Commit 6db185d

Browse files
rogeeffcopybara-github
authored andcommitted
Correct semantic and documentation for the ReportUnrecognizedFlags interface
PiperOrigin-RevId: 516411395 Change-Id: I442fc9435bd7a829802c325e2e4106aa6775c263
1 parent 52578ed commit 6db185d

File tree

2 files changed

+41
-39
lines changed

2 files changed

+41
-39
lines changed

absl/flags/parse.cc

Lines changed: 38 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
#include <cstdlib>
2323
#include <fstream>
2424
#include <iostream>
25-
#include <iterator>
2625
#include <string>
2726
#include <tuple>
2827
#include <utility>
@@ -600,6 +599,34 @@ bool CanIgnoreUndefinedFlag(absl::string_view flag_name) {
600599
return false;
601600
}
602601

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+
603630
} // namespace
604631

605632
// --------------------------------------------------------------------
@@ -674,12 +701,15 @@ std::vector<char*> ParseCommandLineImpl(int argc, char* argv[],
674701
argc, argv, positional_args, unrecognized_flags);
675702

676703
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+
}
681708

682-
if (!unrecognized_flags.empty()) { parse_successful = false; }
709+
flags_internal::ReportUnrecognizedFlags(
710+
unrecognized_flags,
711+
!parse_successful &&
712+
(undef_flag_action == OnUndefinedFlag::kAbortIfUndefined));
683713
}
684714

685715
#if ABSL_FLAGS_STRIP_NAMES
@@ -871,29 +901,8 @@ bool ParseAbseilFlagsOnly(int argc, char* argv[],
871901
// --------------------------------------------------------------------
872902

873903
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);
897906
}
898907

899908
// --------------------------------------------------------------------

absl/flags/parse.h

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -90,17 +90,10 @@ bool ParseAbseilFlagsOnly(int argc, char* argv[],
9090

9191
// ReportUnrecognizedFlags()
9292
//
93-
// Reports an error for all non-ignored unrecognized flags in the provided
94-
// `unrecognized_flags` list.
95-
//
96-
// If `report_fatal_error` is true, the fatal error is reported and program is
97-
// aborted. Otherwise non-fatal error is reported for all flags.
98-
//
99-
// This function returns true if any non-ignored unrecognized flags were
100-
// located in the list and false otherwise.
93+
// Reports an error to `stderr` for all non-ignored unrecognized flags in
94+
// the provided `unrecognized_flags` list.
10195
void ReportUnrecognizedFlags(
102-
const std::vector<UnrecognizedFlag>& unrecognized_flags,
103-
bool report_fatal_error);
96+
const std::vector<UnrecognizedFlag>& unrecognized_flags);
10497

10598
// ParseCommandLine()
10699
//

0 commit comments

Comments
 (0)