diff options
author | 2023-03-24 02:24:00 -0700 | |
---|---|---|
committer | 2023-03-24 02:24:56 -0700 | |
commit | 1bd60dca52b120a7b8e15b39ce86d19d87494a11 (patch) | |
tree | 1dac0120d5d165e1a6f1a7453f307cfe92c26349 | |
parent | b6de7b80325514018d38de2c4dee1254258c4b31 (diff) |
Add a call to handle usage flags in case of unrecognized flags
PiperOrigin-RevId: 519090305
Change-Id: Ic97a36da33e1e0681765a913f3c54c3d818eebdc
-rw-r--r-- | absl/flags/parse.cc | 7 | ||||
-rw-r--r-- | absl/flags/parse_test.cc | 15 |
2 files changed, 18 insertions, 4 deletions
diff --git a/absl/flags/parse.cc b/absl/flags/parse.cc index 1f639066..d3609c13 100644 --- a/absl/flags/parse.cc +++ b/absl/flags/parse.cc @@ -706,7 +706,10 @@ std::vector<char*> ParseCommandLineImpl(int argc, char* argv[], (undef_flag_action == OnUndefinedFlag::kAbortIfUndefined)); if (undef_flag_action == OnUndefinedFlag::kAbortIfUndefined) { - if (!unrecognized_flags.empty()) { std::exit(1); } + if (!unrecognized_flags.empty()) { + flags_internal::HandleUsageFlags(std::cerr, ProgramUsageMessage()); + std::exit(1); + } } } @@ -891,7 +894,7 @@ HelpMode ParseAbseilFlagsOnlyImpl( flags_internal::ReportUsageError( "NOTE: command line flags are disabled in this build", true); #else - flags_internal::HandleUsageFlags(std::cout, ProgramUsageMessage()); + flags_internal::HandleUsageFlags(std::cerr, ProgramUsageMessage()); #endif return HelpMode::kFull; // We just need to make sure the exit with // code 1. diff --git a/absl/flags/parse_test.cc b/absl/flags/parse_test.cc index 13606d14..f3987bb7 100644 --- a/absl/flags/parse_test.cc +++ b/absl/flags/parse_test.cc @@ -17,10 +17,8 @@ #include <stdlib.h> -#include <cstddef> #include <fstream> #include <string> -#include <utility> #include <vector> #include "gmock/gmock.h" @@ -1061,4 +1059,17 @@ TEST_F(ParseTest, AllUndefOkFlagsAreIgnored) { // -------------------------------------------------------------------- +TEST_F(ParseDeathTest, ExitOnUnrecognizedFlagPrintsHelp) { + const char* in_args[] = { + "testbin", + "--undef_flag1", + "--help=int_flag", + }; + + EXPECT_DEATH_IF_SUPPORTED(InvokeParse(in_args), + "Try --helpfull to get a list of all flags"); +} + +// -------------------------------------------------------------------- + } // namespace |