diff options
Diffstat (limited to 'absl/flags')
-rw-r--r-- | absl/flags/BUILD.bazel | 2 | ||||
-rw-r--r-- | absl/flags/marshalling.cc | 40 | ||||
-rw-r--r-- | absl/flags/marshalling.h | 12 |
3 files changed, 51 insertions, 3 deletions
diff --git a/absl/flags/BUILD.bazel b/absl/flags/BUILD.bazel index fe59ec848e84..2bf562f898c7 100644 --- a/absl/flags/BUILD.bazel +++ b/absl/flags/BUILD.bazel @@ -251,7 +251,7 @@ cc_library( ) ############################################################################ -# Unit tests in alpahabetical order. +# Unit tests in alphabetical order. cc_test( name = "commandlineflag_test", 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 diff --git a/absl/flags/marshalling.h b/absl/flags/marshalling.h index f9e2959437a1..6d3918043704 100644 --- a/absl/flags/marshalling.h +++ b/absl/flags/marshalling.h @@ -33,6 +33,7 @@ // * `double` // * `std::string` // * `std::vector<std::string>` +// * `absl::LogSeverity` (provided here due to dependency ordering) // // Note that support for integral types is implemented using overloads for // variable-width fundamental types (`short`, `int`, `long`, etc.). However, @@ -178,8 +179,8 @@ bool AbslParseFlag(absl::string_view, unsigned int*, std::string*); // NOLINT bool AbslParseFlag(absl::string_view, long*, std::string*); // NOLINT bool AbslParseFlag(absl::string_view, unsigned long*, std::string*); // NOLINT bool AbslParseFlag(absl::string_view, long long*, std::string*); // NOLINT -bool AbslParseFlag(absl::string_view, unsigned long long*, - std::string*); // NOLINT +bool AbslParseFlag(absl::string_view, unsigned long long*, // NOLINT + std::string*); bool AbslParseFlag(absl::string_view, float*, std::string*); bool AbslParseFlag(absl::string_view, double*, std::string*); bool AbslParseFlag(absl::string_view, std::string*, std::string*); @@ -248,6 +249,13 @@ inline std::string UnparseFlag(const T& v) { return flags_internal::Unparse(v); } +// Overloads for `absl::LogSeverity` can't (easily) appear alongside that type's +// definition because it is layered below flags. See proper documentation in +// base/log_severity.h. +enum class LogSeverity : int; +bool AbslParseFlag(absl::string_view, absl::LogSeverity*, std::string*); +std::string AbslUnparseFlag(absl::LogSeverity); + } // namespace absl #endif // ABSL_FLAGS_MARSHALLING_H_ |