summaryrefslogtreecommitdiff
path: root/absl/flags/parse_test.cc
diff options
context:
space:
mode:
authorGravatar Gennadiy Rozental <rogeeff@google.com>2023-03-21 01:04:30 -0700
committerGravatar Copybara-Service <copybara-worker@google.com>2023-03-21 01:05:15 -0700
commit819272485a0c06abc8d7d62b188a6f54174881cb (patch)
tree736d215cde8a2598026632c51f3453b4b56036c1 /absl/flags/parse_test.cc
parent396e9764cded739f595ba880b95b061051c49128 (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/parse_test.cc')
-rw-r--r--absl/flags/parse_test.cc86
1 files changed, 59 insertions, 27 deletions
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 <int N>
-std::vector<char*> InvokeParse(const char* (&in_argv)[N]) {
- return absl::ParseCommandLine(N, const_cast<char**>(in_argv));
+flags::HelpMode InvokeParseAbslOnlyImpl(const char* (&in_argv)[N]) {
+ std::vector<char*> positional_args;
+ std::vector<absl::UnrecognizedFlag> unrecognized_flags;
+
+ return flags::ParseAbseilFlagsOnlyImpl(N, const_cast<char**>(in_argv),
+ positional_args, unrecognized_flags,
+ flags::UsageFlagsAction::kHandleUsage);
}
// --------------------------------------------------------------------
template <int N>
-bool InvokeParseAbslOnly(const char* (&in_argv)[N]) {
+void InvokeParseAbslOnly(const char* (&in_argv)[N]) {
std::vector<char*> positional_args;
std::vector<absl::UnrecognizedFlag> unrecognized_flags;
- return absl::ParseAbseilFlagsOnly(N, const_cast<char**>(in_argv),
- positional_args, unrecognized_flags);
+ absl::ParseAbseilFlagsOnly(2, const_cast<char**>(in_argv), positional_args,
+ unrecognized_flags);
+}
+
+// --------------------------------------------------------------------
+
+template <int N>
+std::vector<char*> InvokeParse(const char* (&in_argv)[N]) {
+ return absl::ParseCommandLine(N, const_cast<char**>(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<char*> positional_args;
std::vector<absl::UnrecognizedFlag> unrecognized_flags;
- EXPECT_TRUE(absl::ParseAbseilFlagsOnly(13, const_cast<char**>(in_args),
- positional_args, unrecognized_flags));
+ absl::ParseAbseilFlagsOnly(13, const_cast<char**>(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<char*> positional_args;
- std::vector<absl::UnrecognizedFlag> unrecognized_flags;
-
- EXPECT_FALSE(absl::ParseAbseilFlagsOnly(2, const_cast<char**>(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<char*> positional_args;
std::vector<absl::UnrecognizedFlag> unrecognized_flags;
- EXPECT_TRUE(absl::ParseAbseilFlagsOnly(6, const_cast<char**>(in_args),
- positional_args, unrecognized_flags));
+ absl::ParseAbseilFlagsOnly(6, const_cast<char**>(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<char*> positional_args;
std::vector<absl::UnrecognizedFlag> unrecognized_flags;
- EXPECT_TRUE(absl::ParseAbseilFlagsOnly(8, const_cast<char**>(in_args),
- positional_args, unrecognized_flags));
+ absl::ParseAbseilFlagsOnly(8, const_cast<char**>(in_args), positional_args,
+ unrecognized_flags);
EXPECT_THAT(positional_args,
ElementsAreArray({absl::string_view("testbin"),
absl::string_view("value"),