about summary refs log tree commit diff
path: root/absl/flags
diff options
context:
space:
mode:
Diffstat (limited to 'absl/flags')
-rw-r--r--absl/flags/internal/commandlineflag.cc13
-rw-r--r--absl/flags/internal/commandlineflag.h22
-rw-r--r--absl/flags/internal/commandlineflag_test.cc70
-rw-r--r--absl/flags/internal/type_erased.cc3
-rw-r--r--absl/flags/parse.cc3
5 files changed, 72 insertions, 39 deletions
diff --git a/absl/flags/internal/commandlineflag.cc b/absl/flags/internal/commandlineflag.cc
index f1e50d01ec8d..c919fa1be8a7 100644
--- a/absl/flags/internal/commandlineflag.cc
+++ b/absl/flags/internal/commandlineflag.cc
@@ -23,6 +23,11 @@ FlagStateInterface::~FlagStateInterface() {}
 
 bool CommandLineFlag::IsRetired() const { return false; }
 
+bool CommandLineFlag::ParseFrom(absl::string_view value, std::string* error) {
+  return ParseFrom(value, flags_internal::SET_FLAGS_VALUE,
+                   flags_internal::kProgrammaticChange, error);
+}
+
 FlagFastTypeId PrivateHandleInterface::TypeId(const CommandLineFlag& flag) {
   return flag.TypeId();
 }
@@ -42,6 +47,14 @@ void PrivateHandleInterface::CheckDefaultValueParsingRoundtrip(
   flag.CheckDefaultValueParsingRoundtrip();
 }
 
+bool PrivateHandleInterface::ParseFrom(CommandLineFlag* flag,
+                                       absl::string_view value,
+                                       flags_internal::FlagSettingMode set_mode,
+                                       flags_internal::ValueSource source,
+                                       std::string* error) {
+  return flag->ParseFrom(value, set_mode, source, error);
+}
+
 }  // namespace flags_internal
 ABSL_NAMESPACE_END
 }  // namespace absl
diff --git a/absl/flags/internal/commandlineflag.h b/absl/flags/internal/commandlineflag.h
index f60204dd6256..af5e05d59984 100644
--- a/absl/flags/internal/commandlineflag.h
+++ b/absl/flags/internal/commandlineflag.h
@@ -140,6 +140,17 @@ class CommandLineFlag {
 
   // Sets the value of the flag based on specified string `value`. If the flag
   // was successfully set to new value, it returns true. Otherwise, sets `error`
+  // to indicate the error, leaves the flag unchanged, and returns false.
+  bool ParseFrom(absl::string_view value, std::string* error);
+
+ protected:
+  ~CommandLineFlag() = default;
+
+ private:
+  friend class PrivateHandleInterface;
+
+  // Sets the value of the flag based on specified string `value`. If the flag
+  // was successfully set to new value, it returns true. Otherwise, sets `error`
   // to indicate the error, leaves the flag unchanged, and returns false. There
   // are three ways to set the flag's value:
   //  * Update the current flag value
@@ -151,12 +162,6 @@ class CommandLineFlag {
                          flags_internal::ValueSource source,
                          std::string* error) = 0;
 
- protected:
-  ~CommandLineFlag() = default;
-
- private:
-  friend class PrivateHandleInterface;
-
   // Returns id of the flag's value type.
   virtual FlagFastTypeId TypeId() const = 0;
 
@@ -168,7 +173,6 @@ class CommandLineFlag {
   // the dst based on the current flag's value.
   virtual void Read(void* dst) const = 0;
 
-  // Interfaces to operate on validators.
   // Validates supplied value usign validator or parseflag routine
   virtual bool ValidateInputValue(absl::string_view value) const = 0;
 
@@ -194,6 +198,10 @@ class PrivateHandleInterface {
 
   // Access to CommandLineFlag::CheckDefaultValueParsingRoundtrip.
   static void CheckDefaultValueParsingRoundtrip(const CommandLineFlag& flag);
+
+  static bool ParseFrom(CommandLineFlag* flag, absl::string_view value,
+                        flags_internal::FlagSettingMode set_mode,
+                        flags_internal::ValueSource source, std::string* error);
 };
 
 // This macro is the "source of truth" for the list of supported flag built-in
diff --git a/absl/flags/internal/commandlineflag_test.cc b/absl/flags/internal/commandlineflag_test.cc
index c31679f95e7a..4aeb3bf4dd3c 100644
--- a/absl/flags/internal/commandlineflag_test.cc
+++ b/absl/flags/internal/commandlineflag_test.cc
@@ -122,49 +122,53 @@ TEST_F(CommandLineFlagTest, TestParseFromCurrentValue) {
   auto* flag_01 = flags::FindCommandLineFlag("int_flag");
   EXPECT_FALSE(flag_01->IsSpecifiedOnCommandLine());
 
-  EXPECT_TRUE(flag_01->ParseFrom("11", flags::SET_FLAGS_VALUE,
-                                 flags::kProgrammaticChange, &err));
+  EXPECT_TRUE(flags::PrivateHandleInterface::ParseFrom(
+      flag_01, "11", flags::SET_FLAGS_VALUE, flags::kProgrammaticChange, &err));
   EXPECT_EQ(absl::GetFlag(FLAGS_int_flag), 11);
   EXPECT_FALSE(flag_01->IsSpecifiedOnCommandLine());
 
-  EXPECT_TRUE(flag_01->ParseFrom("-123", flags::SET_FLAGS_VALUE,
-                                 flags::kProgrammaticChange, &err));
+  EXPECT_TRUE(flags::PrivateHandleInterface::ParseFrom(
+      flag_01, "-123", flags::SET_FLAGS_VALUE, flags::kProgrammaticChange,
+      &err));
   EXPECT_EQ(absl::GetFlag(FLAGS_int_flag), -123);
   EXPECT_FALSE(flag_01->IsSpecifiedOnCommandLine());
 
-  EXPECT_TRUE(!flag_01->ParseFrom("xyz", flags::SET_FLAGS_VALUE,
-                                  flags::kProgrammaticChange, &err));
+  EXPECT_TRUE(!flags::PrivateHandleInterface::ParseFrom(
+      flag_01, "xyz", flags::SET_FLAGS_VALUE, flags::kProgrammaticChange,
+      &err));
   EXPECT_EQ(absl::GetFlag(FLAGS_int_flag), -123);
   EXPECT_EQ(err, "Illegal value 'xyz' specified for flag 'int_flag'");
   EXPECT_FALSE(flag_01->IsSpecifiedOnCommandLine());
 
-  EXPECT_TRUE(!flag_01->ParseFrom("A1", flags::SET_FLAGS_VALUE,
-                                  flags::kProgrammaticChange, &err));
+  EXPECT_TRUE(!flags::PrivateHandleInterface::ParseFrom(
+      flag_01, "A1", flags::SET_FLAGS_VALUE, flags::kProgrammaticChange, &err));
   EXPECT_EQ(absl::GetFlag(FLAGS_int_flag), -123);
   EXPECT_EQ(err, "Illegal value 'A1' specified for flag 'int_flag'");
   EXPECT_FALSE(flag_01->IsSpecifiedOnCommandLine());
 
-  EXPECT_TRUE(flag_01->ParseFrom("0x10", flags::SET_FLAGS_VALUE,
-                                 flags::kProgrammaticChange, &err));
+  EXPECT_TRUE(flags::PrivateHandleInterface::ParseFrom(
+      flag_01, "0x10", flags::SET_FLAGS_VALUE, flags::kProgrammaticChange,
+      &err));
   EXPECT_EQ(absl::GetFlag(FLAGS_int_flag), 16);
   EXPECT_FALSE(flag_01->IsSpecifiedOnCommandLine());
 
-  EXPECT_TRUE(flag_01->ParseFrom("011", flags::SET_FLAGS_VALUE,
-                                 flags::kCommandLine, &err));
+  EXPECT_TRUE(flags::PrivateHandleInterface::ParseFrom(
+      flag_01, "011", flags::SET_FLAGS_VALUE, flags::kCommandLine, &err));
   EXPECT_EQ(absl::GetFlag(FLAGS_int_flag), 11);
   EXPECT_TRUE(flag_01->IsSpecifiedOnCommandLine());
 
-  EXPECT_TRUE(!flag_01->ParseFrom("", flags::SET_FLAGS_VALUE,
-                                  flags::kProgrammaticChange, &err));
+  EXPECT_TRUE(!flags::PrivateHandleInterface::ParseFrom(
+      flag_01, "", flags::SET_FLAGS_VALUE, flags::kProgrammaticChange, &err));
   EXPECT_EQ(err, "Illegal value '' specified for flag 'int_flag'");
 
   auto* flag_02 = flags::FindCommandLineFlag("string_flag");
-  EXPECT_TRUE(flag_02->ParseFrom("xyz", flags::SET_FLAGS_VALUE,
-                                 flags::kProgrammaticChange, &err));
+  EXPECT_TRUE(flags::PrivateHandleInterface::ParseFrom(
+      flag_02, "xyz", flags::SET_FLAGS_VALUE, flags::kProgrammaticChange,
+      &err));
   EXPECT_EQ(absl::GetFlag(FLAGS_string_flag), "xyz");
 
-  EXPECT_TRUE(flag_02->ParseFrom("", flags::SET_FLAGS_VALUE,
-                                 flags::kProgrammaticChange, &err));
+  EXPECT_TRUE(flags::PrivateHandleInterface::ParseFrom(
+      flag_02, "", flags::SET_FLAGS_VALUE, flags::kProgrammaticChange, &err));
   EXPECT_EQ(absl::GetFlag(FLAGS_string_flag), "");
 }
 
@@ -175,14 +179,16 @@ TEST_F(CommandLineFlagTest, TestParseFromDefaultValue) {
 
   auto* flag_01 = flags::FindCommandLineFlag("int_flag");
 
-  EXPECT_TRUE(flag_01->ParseFrom("111", flags::SET_FLAGS_DEFAULT,
-                                 flags::kProgrammaticChange, &err));
+  EXPECT_TRUE(flags::PrivateHandleInterface::ParseFrom(
+      flag_01, "111", flags::SET_FLAGS_DEFAULT, flags::kProgrammaticChange,
+      &err));
   EXPECT_EQ(flag_01->DefaultValue(), "111");
 
   auto* flag_02 = flags::FindCommandLineFlag("string_flag");
 
-  EXPECT_TRUE(flag_02->ParseFrom("abc", flags::SET_FLAGS_DEFAULT,
-                                 flags::kProgrammaticChange, &err));
+  EXPECT_TRUE(flags::PrivateHandleInterface::ParseFrom(
+      flag_02, "abc", flags::SET_FLAGS_DEFAULT, flags::kProgrammaticChange,
+      &err));
   EXPECT_EQ(flag_02->DefaultValue(), "abc");
 }
 
@@ -193,22 +199,26 @@ TEST_F(CommandLineFlagTest, TestParseFromIfDefault) {
 
   auto* flag_01 = flags::FindCommandLineFlag("int_flag");
 
-  EXPECT_TRUE(flag_01->ParseFrom("22", flags::SET_FLAG_IF_DEFAULT,
-                                 flags::kProgrammaticChange, &err))
+  EXPECT_TRUE(flags::PrivateHandleInterface::ParseFrom(
+      flag_01, "22", flags::SET_FLAG_IF_DEFAULT, flags::kProgrammaticChange,
+      &err))
       << err;
   EXPECT_EQ(absl::GetFlag(FLAGS_int_flag), 22);
 
-  EXPECT_TRUE(flag_01->ParseFrom("33", flags::SET_FLAG_IF_DEFAULT,
-                                 flags::kProgrammaticChange, &err));
+  EXPECT_TRUE(flags::PrivateHandleInterface::ParseFrom(
+      flag_01, "33", flags::SET_FLAG_IF_DEFAULT, flags::kProgrammaticChange,
+      &err));
   EXPECT_EQ(absl::GetFlag(FLAGS_int_flag), 22);
   // EXPECT_EQ(err, "ERROR: int_flag is already set to 22");
 
   // Reset back to default value
-  EXPECT_TRUE(flag_01->ParseFrom("201", flags::SET_FLAGS_VALUE,
-                                 flags::kProgrammaticChange, &err));
+  EXPECT_TRUE(flags::PrivateHandleInterface::ParseFrom(
+      flag_01, "201", flags::SET_FLAGS_VALUE, flags::kProgrammaticChange,
+      &err));
 
-  EXPECT_TRUE(flag_01->ParseFrom("33", flags::SET_FLAG_IF_DEFAULT,
-                                 flags::kProgrammaticChange, &err));
+  EXPECT_TRUE(flags::PrivateHandleInterface::ParseFrom(
+      flag_01, "33", flags::SET_FLAG_IF_DEFAULT, flags::kProgrammaticChange,
+      &err));
   EXPECT_EQ(absl::GetFlag(FLAGS_int_flag), 201);
   // EXPECT_EQ(err, "ERROR: int_flag is already set to 201");
 }
diff --git a/absl/flags/internal/type_erased.cc b/absl/flags/internal/type_erased.cc
index 5038625be761..adeb7a1541ab 100644
--- a/absl/flags/internal/type_erased.cc
+++ b/absl/flags/internal/type_erased.cc
@@ -56,7 +56,8 @@ bool SetCommandLineOptionWithMode(absl::string_view name,
   if (!flag || flag->IsRetired()) return false;
 
   std::string error;
-  if (!flag->ParseFrom(value, set_mode, kProgrammaticChange, &error)) {
+  if (!flags_internal::PrivateHandleInterface::ParseFrom(
+          flag, value, set_mode, kProgrammaticChange, &error)) {
     // Errors here are all of the form: the provided name was a recognized
     // flag, but the value was invalid (bad type, or validation failed).
     flags_internal::ReportUsageError(error, false);
diff --git a/absl/flags/parse.cc b/absl/flags/parse.cc
index b76edc62d9b0..cc134909877f 100644
--- a/absl/flags/parse.cc
+++ b/absl/flags/parse.cc
@@ -697,7 +697,8 @@ std::vector<char*> ParseCommandLineImpl(int argc, char* argv[],
     if (flag->IsRetired()) continue;
 
     std::string error;
-    if (!flag->ParseFrom(value, SET_FLAGS_VALUE, kCommandLine, &error)) {
+    if (!flags_internal::PrivateHandleInterface::ParseFrom(
+            flag, value, SET_FLAGS_VALUE, kCommandLine, &error)) {
       flags_internal::ReportUsageError(error, true);
       success = false;
     }