From 1bd60dca52b120a7b8e15b39ce86d19d87494a11 Mon Sep 17 00:00:00 2001 From: Gennadiy Rozental Date: Fri, 24 Mar 2023 02:24:00 -0700 Subject: Add a call to handle usage flags in case of unrecognized flags PiperOrigin-RevId: 519090305 Change-Id: Ic97a36da33e1e0681765a913f3c54c3d818eebdc --- absl/flags/parse.cc | 7 +++++-- absl/flags/parse_test.cc | 15 +++++++++++++-- 2 files changed, 18 insertions(+), 4 deletions(-) (limited to 'absl/flags') 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 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 -#include #include #include -#include #include #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 -- cgit v1.2.3