diff options
Diffstat (limited to 'absl/flags/marshalling.cc')
-rw-r--r-- | absl/flags/marshalling.cc | 53 |
1 files changed, 51 insertions, 2 deletions
diff --git a/absl/flags/marshalling.cc b/absl/flags/marshalling.cc index 71b01d77..6f2ddda8 100644 --- a/absl/flags/marshalling.cc +++ b/absl/flags/marshalling.cc @@ -15,18 +15,28 @@ #include "absl/flags/marshalling.h" +#include <stddef.h> + +#include <cmath> #include <limits> +#include <string> +#include <type_traits> +#include <vector> +#include "absl/base/config.h" +#include "absl/base/log_severity.h" #include "absl/base/macros.h" +#include "absl/strings/ascii.h" #include "absl/strings/match.h" #include "absl/strings/numbers.h" #include "absl/strings/str_cat.h" #include "absl/strings/str_format.h" #include "absl/strings/str_join.h" #include "absl/strings/str_split.h" +#include "absl/strings/string_view.h" namespace absl { -inline namespace lts_2019_08_08 { +ABSL_NAMESPACE_BEGIN namespace flags_internal { // -------------------------------------------------------------------- @@ -187,5 +197,44 @@ std::string AbslUnparseFlag(const std::vector<std::string>& v) { } } // namespace flags_internal -} // inline namespace lts_2019_08_08 + +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)); +} + +ABSL_NAMESPACE_END } // namespace absl |