diff options
author | Gennadiy Rozental <rogeeff@google.com> | 2023-03-21 01:04:30 -0700 |
---|---|---|
committer | Copybara-Service <copybara-worker@google.com> | 2023-03-21 01:05:15 -0700 |
commit | 819272485a0c06abc8d7d62b188a6f54174881cb (patch) | |
tree | 736d215cde8a2598026632c51f3453b4b56036c1 /absl/flags/internal | |
parent | 396e9764cded739f595ba880b95b061051c49128 (diff) |
Change ParseAbseilFlagsOnly function to include handling of usage flags and exit on syntax errors.
PiperOrigin-RevId: 518198160
Change-Id: Ib4fe53e0b0b371472b6b9473aeb84779953b6a38
Diffstat (limited to 'absl/flags/internal')
-rw-r--r-- | absl/flags/internal/parse.h | 5 | ||||
-rw-r--r-- | absl/flags/internal/usage.cc | 39 | ||||
-rw-r--r-- | absl/flags/internal/usage.h | 42 | ||||
-rw-r--r-- | absl/flags/internal/usage_test.cc | 31 |
4 files changed, 72 insertions, 45 deletions
diff --git a/absl/flags/internal/parse.h b/absl/flags/internal/parse.h index 145e591d..e4352049 100644 --- a/absl/flags/internal/parse.h +++ b/absl/flags/internal/parse.h @@ -21,6 +21,7 @@ #include "absl/base/config.h" #include "absl/flags/declare.h" +#include "absl/flags/internal/usage.h" #include "absl/strings/string_view.h" ABSL_DECLARE_FLAG(std::vector<std::string>, flagfile); @@ -40,8 +41,8 @@ enum class OnUndefinedFlag { }; std::vector<char*> ParseCommandLineImpl(int argc, char* argv[], - UsageFlagsAction usage_flag_act, - OnUndefinedFlag on_undef_flag); + UsageFlagsAction usage_flag_action, + OnUndefinedFlag undef_flag_action); // -------------------------------------------------------------------- // Inspect original command line diff --git a/absl/flags/internal/usage.cc b/absl/flags/internal/usage.cc index e9481488..6a56fce9 100644 --- a/absl/flags/internal/usage.cc +++ b/absl/flags/internal/usage.cc @@ -18,6 +18,7 @@ #include <stdint.h> #include <algorithm> +#include <cstdlib> #include <functional> #include <iterator> #include <map> @@ -354,8 +355,8 @@ void FlagsHelp(std::ostream& out, absl::string_view filter, HelpFormat format, // -------------------------------------------------------------------- // Checks all the 'usage' command line flags to see if any have been set. // If so, handles them appropriately. -int HandleUsageFlags(std::ostream& out, - absl::string_view program_usage_message) { +HelpMode HandleUsageFlags(std::ostream& out, + absl::string_view program_usage_message) { switch (GetFlagsHelpMode()) { case HelpMode::kNone: break; @@ -363,25 +364,24 @@ int HandleUsageFlags(std::ostream& out, flags_internal::FlagsHelpImpl( out, flags_internal::GetUsageConfig().contains_help_flags, GetFlagsHelpFormat(), program_usage_message); - return 1; + break; case HelpMode::kShort: flags_internal::FlagsHelpImpl( out, flags_internal::GetUsageConfig().contains_helpshort_flags, GetFlagsHelpFormat(), program_usage_message); - return 1; + break; case HelpMode::kFull: flags_internal::FlagsHelp(out, "", GetFlagsHelpFormat(), program_usage_message); - return 1; + break; case HelpMode::kPackage: flags_internal::FlagsHelpImpl( out, flags_internal::GetUsageConfig().contains_helppackage_flags, GetFlagsHelpFormat(), program_usage_message); - - return 1; + break; case HelpMode::kMatch: { std::string substr = GetFlagsHelpMatchSubstr(); @@ -400,20 +400,19 @@ int HandleUsageFlags(std::ostream& out, flags_internal::FlagsHelpImpl( out, filter_cb, HelpFormat::kHumanReadable, program_usage_message); } - - return 1; + break; } case HelpMode::kVersion: if (flags_internal::GetUsageConfig().version_string) out << flags_internal::GetUsageConfig().version_string(); // Unlike help, we may be asking for version in a script, so return 0 - return 0; + break; case HelpMode::kOnlyCheckArgs: - return 0; + break; } - return -1; + return GetFlagsHelpMode(); } // -------------------------------------------------------------------- @@ -521,6 +520,22 @@ bool DeduceUsageFlags(absl::string_view name, absl::string_view value) { return false; } +// -------------------------------------------------------------------- + +void MaybeExit(HelpMode mode) { + switch (mode) { + case flags_internal::HelpMode::kNone: + return; + case flags_internal::HelpMode::kOnlyCheckArgs: + case flags_internal::HelpMode::kVersion: + std::exit(0); + default: // For all the other modes we exit with 1 + std::exit(1); + } +} + +// -------------------------------------------------------------------- + } // namespace flags_internal ABSL_NAMESPACE_END } // namespace absl diff --git a/absl/flags/internal/usage.h b/absl/flags/internal/usage.h index c0bcac57..a96cbf38 100644 --- a/absl/flags/internal/usage.h +++ b/absl/flags/internal/usage.h @@ -17,11 +17,11 @@ #define ABSL_FLAGS_INTERNAL_USAGE_H_ #include <iosfwd> +#include <ostream> #include <string> #include "absl/base/config.h" #include "absl/flags/commandlineflag.h" -#include "absl/flags/declare.h" #include "absl/strings/string_view.h" // -------------------------------------------------------------------- @@ -36,6 +36,18 @@ enum class HelpFormat { kHumanReadable, }; +// The kind of usage help requested. +enum class HelpMode { + kNone, + kImportant, + kShort, + kFull, + kPackage, + kMatch, + kVersion, + kOnlyCheckArgs +}; + // Streams the help message describing `flag` to `out`. // The default value for `flag` is included in the output. void FlagHelp(std::ostream& out, const CommandLineFlag& flag, @@ -57,28 +69,18 @@ void FlagsHelp(std::ostream& out, absl::string_view filter, // If any of the 'usage' related command line flags (listed on the bottom of // this file) has been set this routine produces corresponding help message in -// the specified output stream and returns: -// 0 - if "version" or "only_check_flags" flags were set and handled. -// 1 - if some other 'usage' related flag was set and handled. -// -1 - if no usage flags were set on a commmand line. -// Non negative return values are expected to be used as an exit code for a -// binary. -int HandleUsageFlags(std::ostream& out, - absl::string_view program_usage_message); +// the specified output stream and returns HelpMode that was handled. Otherwise +// it returns HelpMode::kNone. +HelpMode HandleUsageFlags(std::ostream& out, + absl::string_view program_usage_message); // -------------------------------------------------------------------- -// Globals representing usage reporting flags +// Encapsulates the logic of exiting the binary depending on handled help mode. -enum class HelpMode { - kNone, - kImportant, - kShort, - kFull, - kPackage, - kMatch, - kVersion, - kOnlyCheckArgs -}; +void MaybeExit(HelpMode mode); + +// -------------------------------------------------------------------- +// Globals representing usage reporting flags // Returns substring to filter help output (--help=substr argument) std::string GetFlagsHelpMatchSubstr(); diff --git a/absl/flags/internal/usage_test.cc b/absl/flags/internal/usage_test.cc index 209a7be9..c3ab4a42 100644 --- a/absl/flags/internal/usage_test.cc +++ b/absl/flags/internal/usage_test.cc @@ -24,7 +24,6 @@ #include "gtest/gtest.h" #include "absl/flags/flag.h" #include "absl/flags/internal/parse.h" -#include "absl/flags/internal/path_util.h" #include "absl/flags/internal/program_name.h" #include "absl/flags/reflection.h" #include "absl/flags/usage.h" @@ -256,7 +255,8 @@ path. TEST_F(UsageReportingTest, TestNoUsageFlags) { std::stringstream test_buf; - EXPECT_EQ(flags::HandleUsageFlags(test_buf, kTestUsageMessage), -1); + EXPECT_EQ(flags::HandleUsageFlags(test_buf, kTestUsageMessage), + flags::HelpMode::kNone); } // -------------------------------------------------------------------- @@ -265,7 +265,8 @@ TEST_F(UsageReportingTest, TestUsageFlag_helpshort) { flags::SetFlagsHelpMode(flags::HelpMode::kShort); std::stringstream test_buf; - EXPECT_EQ(flags::HandleUsageFlags(test_buf, kTestUsageMessage), 1); + EXPECT_EQ(flags::HandleUsageFlags(test_buf, kTestUsageMessage), + flags::HelpMode::kShort); EXPECT_EQ(test_buf.str(), R"(usage_test: Custom usage message @@ -298,7 +299,8 @@ TEST_F(UsageReportingTest, TestUsageFlag_help_simple) { flags::SetFlagsHelpMode(flags::HelpMode::kImportant); std::stringstream test_buf; - EXPECT_EQ(flags::HandleUsageFlags(test_buf, kTestUsageMessage), 1); + EXPECT_EQ(flags::HandleUsageFlags(test_buf, kTestUsageMessage), + flags::HelpMode::kImportant); EXPECT_EQ(test_buf.str(), R"(usage_test: Custom usage message @@ -332,7 +334,8 @@ TEST_F(UsageReportingTest, TestUsageFlag_help_one_flag) { flags::SetFlagsHelpMatchSubstr("usage_reporting_test_flag_06"); std::stringstream test_buf; - EXPECT_EQ(flags::HandleUsageFlags(test_buf, kTestUsageMessage), 1); + EXPECT_EQ(flags::HandleUsageFlags(test_buf, kTestUsageMessage), + flags::HelpMode::kMatch); EXPECT_EQ(test_buf.str(), R"(usage_test: Custom usage message @@ -356,7 +359,8 @@ TEST_F(UsageReportingTest, TestUsageFlag_help_multiple_flag) { flags::SetFlagsHelpMatchSubstr("test_flag"); std::stringstream test_buf; - EXPECT_EQ(flags::HandleUsageFlags(test_buf, kTestUsageMessage), 1); + EXPECT_EQ(flags::HandleUsageFlags(test_buf, kTestUsageMessage), + flags::HelpMode::kMatch); EXPECT_EQ(test_buf.str(), R"(usage_test: Custom usage message @@ -389,7 +393,8 @@ TEST_F(UsageReportingTest, TestUsageFlag_helppackage) { flags::SetFlagsHelpMode(flags::HelpMode::kPackage); std::stringstream test_buf; - EXPECT_EQ(flags::HandleUsageFlags(test_buf, kTestUsageMessage), 1); + EXPECT_EQ(flags::HandleUsageFlags(test_buf, kTestUsageMessage), + flags::HelpMode::kPackage); EXPECT_EQ(test_buf.str(), R"(usage_test: Custom usage message @@ -422,7 +427,8 @@ TEST_F(UsageReportingTest, TestUsageFlag_version) { flags::SetFlagsHelpMode(flags::HelpMode::kVersion); std::stringstream test_buf; - EXPECT_EQ(flags::HandleUsageFlags(test_buf, kTestUsageMessage), 0); + EXPECT_EQ(flags::HandleUsageFlags(test_buf, kTestUsageMessage), + flags::HelpMode::kVersion); #ifndef NDEBUG EXPECT_EQ(test_buf.str(), "usage_test\nDebug build (NDEBUG not #defined)\n"); #else @@ -436,7 +442,8 @@ TEST_F(UsageReportingTest, TestUsageFlag_only_check_args) { flags::SetFlagsHelpMode(flags::HelpMode::kOnlyCheckArgs); std::stringstream test_buf; - EXPECT_EQ(flags::HandleUsageFlags(test_buf, kTestUsageMessage), 0); + EXPECT_EQ(flags::HandleUsageFlags(test_buf, kTestUsageMessage), + flags::HelpMode::kOnlyCheckArgs); EXPECT_EQ(test_buf.str(), ""); } @@ -447,7 +454,8 @@ TEST_F(UsageReportingTest, TestUsageFlag_helpon) { flags::SetFlagsHelpMatchSubstr("/bla-bla."); std::stringstream test_buf_01; - EXPECT_EQ(flags::HandleUsageFlags(test_buf_01, kTestUsageMessage), 1); + EXPECT_EQ(flags::HandleUsageFlags(test_buf_01, kTestUsageMessage), + flags::HelpMode::kMatch); EXPECT_EQ(test_buf_01.str(), R"(usage_test: Custom usage message @@ -461,7 +469,8 @@ path. flags::SetFlagsHelpMatchSubstr("/usage_test."); std::stringstream test_buf_02; - EXPECT_EQ(flags::HandleUsageFlags(test_buf_02, kTestUsageMessage), 1); + EXPECT_EQ(flags::HandleUsageFlags(test_buf_02, kTestUsageMessage), + flags::HelpMode::kMatch); EXPECT_EQ(test_buf_02.str(), R"(usage_test: Custom usage message |