From 819272485a0c06abc8d7d62b188a6f54174881cb Mon Sep 17 00:00:00 2001 From: Gennadiy Rozental Date: Tue, 21 Mar 2023 01:04:30 -0700 Subject: Change ParseAbseilFlagsOnly function to include handling of usage flags and exit on syntax errors. PiperOrigin-RevId: 518198160 Change-Id: Ib4fe53e0b0b371472b6b9473aeb84779953b6a38 --- absl/flags/parse_test.cc | 86 +++++++++++++++++++++++++++++++++--------------- 1 file changed, 59 insertions(+), 27 deletions(-) (limited to 'absl/flags/parse_test.cc') diff --git a/absl/flags/parse_test.cc b/absl/flags/parse_test.cc index c46bb46e..13606d14 100644 --- a/absl/flags/parse_test.cc +++ b/absl/flags/parse_test.cc @@ -250,19 +250,31 @@ class ParseTest : public testing::Test { // -------------------------------------------------------------------- template -std::vector InvokeParse(const char* (&in_argv)[N]) { - return absl::ParseCommandLine(N, const_cast(in_argv)); +flags::HelpMode InvokeParseAbslOnlyImpl(const char* (&in_argv)[N]) { + std::vector positional_args; + std::vector unrecognized_flags; + + return flags::ParseAbseilFlagsOnlyImpl(N, const_cast(in_argv), + positional_args, unrecognized_flags, + flags::UsageFlagsAction::kHandleUsage); } // -------------------------------------------------------------------- template -bool InvokeParseAbslOnly(const char* (&in_argv)[N]) { +void InvokeParseAbslOnly(const char* (&in_argv)[N]) { std::vector positional_args; std::vector unrecognized_flags; - return absl::ParseAbseilFlagsOnly(N, const_cast(in_argv), - positional_args, unrecognized_flags); + absl::ParseAbseilFlagsOnly(2, const_cast(in_argv), positional_args, + unrecognized_flags); +} + +// -------------------------------------------------------------------- + +template +std::vector InvokeParse(const char* (&in_argv)[N]) { + return absl::ParseCommandLine(N, const_cast(in_argv)); } // -------------------------------------------------------------------- @@ -871,6 +883,7 @@ TEST_F(ParseDeathTest, TestSimpleHelpFlagHandling) { "--help", }; + EXPECT_EQ(InvokeParseAbslOnlyImpl(in_args1), flags::HelpMode::kImportant); EXPECT_EXIT(InvokeParse(in_args1), testing::ExitedWithCode(1), ""); const char* in_args2[] = { @@ -879,34 +892,55 @@ TEST_F(ParseDeathTest, TestSimpleHelpFlagHandling) { "--int_flag=3", }; - InvokeParseAbslOnly(in_args2); - - EXPECT_EQ(flags::GetFlagsHelpMode(), flags::HelpMode::kImportant); + EXPECT_EQ(InvokeParseAbslOnlyImpl(in_args2), flags::HelpMode::kImportant); EXPECT_EQ(absl::GetFlag(FLAGS_int_flag), 3); const char* in_args3[] = {"testbin", "--help", "some_positional_arg"}; - InvokeParseAbslOnly(in_args3); - - EXPECT_EQ(flags::GetFlagsHelpMode(), flags::HelpMode::kImportant); + EXPECT_EQ(InvokeParseAbslOnlyImpl(in_args3), flags::HelpMode::kImportant); } // -------------------------------------------------------------------- -TEST_F(ParseDeathTest, TestSubstringHelpFlagHandling) { +TEST_F(ParseTest, TestSubstringHelpFlagHandling) { const char* in_args1[] = { "testbin", "--help=abcd", }; - InvokeParseAbslOnly(in_args1); - - EXPECT_EQ(flags::GetFlagsHelpMode(), flags::HelpMode::kMatch); + EXPECT_EQ(InvokeParseAbslOnlyImpl(in_args1), flags::HelpMode::kMatch); EXPECT_EQ(flags::GetFlagsHelpMatchSubstr(), "abcd"); } // -------------------------------------------------------------------- +TEST_F(ParseDeathTest, TestVersionHandling) { + const char* in_args1[] = { + "testbin", + "--version", + }; + + EXPECT_EQ(InvokeParseAbslOnlyImpl(in_args1), flags::HelpMode::kVersion); +} + +// -------------------------------------------------------------------- + +TEST_F(ParseTest, TestCheckArgsHandling) { + const char* in_args1[] = {"testbin", "--only_check_args", "--int_flag=211"}; + + EXPECT_EQ(InvokeParseAbslOnlyImpl(in_args1), flags::HelpMode::kOnlyCheckArgs); + EXPECT_EXIT(InvokeParseAbslOnly(in_args1), testing::ExitedWithCode(0), ""); + EXPECT_EXIT(InvokeParse(in_args1), testing::ExitedWithCode(0), ""); + + const char* in_args2[] = {"testbin", "--only_check_args", "--unknown_flag=a"}; + + EXPECT_EQ(InvokeParseAbslOnlyImpl(in_args2), flags::HelpMode::kOnlyCheckArgs); + EXPECT_EXIT(InvokeParseAbslOnly(in_args2), testing::ExitedWithCode(0), ""); + EXPECT_EXIT(InvokeParse(in_args2), testing::ExitedWithCode(1), ""); +} + +// -------------------------------------------------------------------- + TEST_F(ParseTest, WasPresentOnCommandLine) { const char* in_args1[] = { "testbin", "arg1", "--bool_flag", @@ -947,8 +981,8 @@ TEST_F(ParseTest, ParseAbseilFlagsOnlySuccess) { std::vector positional_args; std::vector unrecognized_flags; - EXPECT_TRUE(absl::ParseAbseilFlagsOnly(13, const_cast(in_args), - positional_args, unrecognized_flags)); + absl::ParseAbseilFlagsOnly(13, const_cast(in_args), positional_args, + unrecognized_flags); EXPECT_THAT(positional_args, ElementsAreArray( {absl::string_view("testbin"), absl::string_view("arg1"), @@ -964,17 +998,15 @@ TEST_F(ParseTest, ParseAbseilFlagsOnlySuccess) { // -------------------------------------------------------------------- -TEST_F(ParseTest, ParseAbseilFlagsOnlyFailure) { +TEST_F(ParseDeathTest, ParseAbseilFlagsOnlyFailure) { const char* in_args[] = { "testbin", "--int_flag=21.1", }; - std::vector positional_args; - std::vector unrecognized_flags; - - EXPECT_FALSE(absl::ParseAbseilFlagsOnly(2, const_cast(in_args), - positional_args, unrecognized_flags)); + EXPECT_DEATH_IF_SUPPORTED( + InvokeParseAbslOnly(in_args), + "Illegal value '21.1' specified for flag 'int_flag'"); } // -------------------------------------------------------------------- @@ -989,8 +1021,8 @@ TEST_F(ParseTest, UndefOkFlagsAreIgnored) { std::vector positional_args; std::vector unrecognized_flags; - EXPECT_TRUE(absl::ParseAbseilFlagsOnly(6, const_cast(in_args), - positional_args, unrecognized_flags)); + absl::ParseAbseilFlagsOnly(6, const_cast(in_args), positional_args, + unrecognized_flags); EXPECT_THAT(positional_args, ElementsAreArray({absl::string_view("testbin"), absl::string_view("value")})); EXPECT_THAT(unrecognized_flags, @@ -1018,8 +1050,8 @@ TEST_F(ParseTest, AllUndefOkFlagsAreIgnored) { std::vector positional_args; std::vector unrecognized_flags; - EXPECT_TRUE(absl::ParseAbseilFlagsOnly(8, const_cast(in_args), - positional_args, unrecognized_flags)); + absl::ParseAbseilFlagsOnly(8, const_cast(in_args), positional_args, + unrecognized_flags); EXPECT_THAT(positional_args, ElementsAreArray({absl::string_view("testbin"), absl::string_view("value"), -- cgit v1.2.3