about summary refs log tree commit diff
path: root/absl/flags/marshalling.cc
diff options
context:
space:
mode:
Diffstat (limited to 'absl/flags/marshalling.cc')
-rw-r--r--absl/flags/marshalling.cc40
1 files changed, 40 insertions, 0 deletions
diff --git a/absl/flags/marshalling.cc b/absl/flags/marshalling.cc
index f025ae7e86c4..f4ebe0e34330 100644
--- a/absl/flags/marshalling.cc
+++ b/absl/flags/marshalling.cc
@@ -16,6 +16,7 @@
 #include "absl/flags/marshalling.h"
 
 #include <limits>
+#include <type_traits>
 
 #include "absl/base/macros.h"
 #include "absl/strings/match.h"
@@ -186,4 +187,43 @@ std::string AbslUnparseFlag(const std::vector<std::string>& v) {
 }
 
 }  // namespace flags_internal
+
+bool AbslParseFlag(absl::string_view text, absl::LogSeverity* dst,
+                   std::string* err) {
+  text = absl::StripAsciiWhitespace(text);
+  if (text.empty()) {
+    *err = "no value provided";
+    return false;
+  }
+  if (text.front() == 'k' || text.front() == 'K') text.remove_prefix(1);
+  if (absl::EqualsIgnoreCase(text, "info")) {
+    *dst = absl::LogSeverity::kInfo;
+    return true;
+  }
+  if (absl::EqualsIgnoreCase(text, "warning")) {
+    *dst = absl::LogSeverity::kWarning;
+    return true;
+  }
+  if (absl::EqualsIgnoreCase(text, "error")) {
+    *dst = absl::LogSeverity::kError;
+    return true;
+  }
+  if (absl::EqualsIgnoreCase(text, "fatal")) {
+    *dst = absl::LogSeverity::kFatal;
+    return true;
+  }
+  std::underlying_type<absl::LogSeverity>::type numeric_value;
+  if (absl::ParseFlag(text, &numeric_value, err)) {
+    *dst = static_cast<absl::LogSeverity>(numeric_value);
+    return true;
+  }
+  *err = "only integers and absl::LogSeverity enumerators are accepted";
+  return false;
+}
+
+std::string AbslUnparseFlag(absl::LogSeverity v) {
+  if (v == absl::NormalizeLogSeverity(v)) return absl::LogSeverityName(v);
+  return absl::UnparseFlag(static_cast<int>(v));
+}
+
 }  // namespace absl