From 41f9b84c31c872e0e48b5047ba45ac22d1aaaf36 Mon Sep 17 00:00:00 2001 From: "Korner, Daniel" Date: Tue, 22 Sep 2020 11:21:02 +0200 Subject: [PATCH 1/2] Add noexcept operator to docopt::value::isBool, isString, isLong, isStringList --- docopt_value.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/docopt_value.h b/docopt_value.h index e1c87e6..a42876e 100644 --- a/docopt_value.h +++ b/docopt_value.h @@ -51,10 +51,10 @@ namespace docopt { explicit operator bool() const { return kind_ != Kind::Empty; } // Test the type contained by this value object - bool isBool() const { return kind_==Kind::Bool; } - bool isString() const { return kind_==Kind::String; } - bool isLong() const { return kind_==Kind::Long; } - bool isStringList() const { return kind_==Kind::StringList; } + bool isBool() const noexcept { return kind_==Kind::Bool; } + bool isString() const noexcept { return kind_==Kind::String; } + bool isLong() const noexcept { return kind_==Kind::Long; } + bool isStringList() const noexcept { return kind_==Kind::StringList; } // Throws std::invalid_argument if the type does not match bool asBool() const; From c9fe80943cd6884b0aaf709c025a3b851ffba95d Mon Sep 17 00:00:00 2001 From: "Korner, Daniel" Date: Tue, 22 Sep 2020 11:21:49 +0200 Subject: [PATCH 2/2] Add docopt::value::asBoolOr(bool) function Add docopt::value::asStringOr(string) function Add docopt::value::asLongOr(long) function Add docopt::value::asStringList(StringList) function --- docopt_value.h | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/docopt_value.h b/docopt_value.h index a42876e..d57f49a 100644 --- a/docopt_value.h +++ b/docopt_value.h @@ -14,6 +14,8 @@ #include // std::hash #include #include +#include // std::strtol +#include // errno, ERANGE namespace docopt { @@ -58,9 +60,13 @@ namespace docopt { // Throws std::invalid_argument if the type does not match bool asBool() const; + bool asBoolOr(const bool value) const noexcept; long asLong() const; + long asLongOr(const long value) const noexcept; std::string const& asString() const; + std::string const& asStringOr(std::string const& value) const noexcept; std::vector const& asStringList() const; + std::vector const& asStringListOr(std::vector const& value) const noexcept; size_t hash() const noexcept; @@ -274,6 +280,16 @@ namespace docopt { return variant_.boolValue; } + inline + bool value::asBoolOr(const bool value) const noexcept + { + if(!isBool()) + { + return value; + } + return variant_.boolValue; + } + inline long value::asLong() const { @@ -292,6 +308,33 @@ namespace docopt { return variant_.longValue; } + inline + long value::asLongOr(const long value) const noexcept + { + // Attempt to convert a string to a long + if (isString()) { + const std::string& str = variant_.strValue; + char * str_end; + const long ret = std::strtol(str.c_str(), &str_end, 10); + // Case 1: No Conversion was possible, return given default value + if(str_end == str.c_str() && ret == 0) + { + return value; + } + // Case 2: Parsed value is out of range, return given default value + if (errno == ERANGE) { + return value; + } + // Case 3: Everything is fine, return parsed value + return ret; + } + if (!isLong()) + { + return value; + } + return variant_.longValue; + } + inline std::string const& value::asString() const { @@ -299,6 +342,17 @@ namespace docopt { return variant_.strValue; } + inline + std::string const& value::asStringOr(std::string const& value) const noexcept + { + if(!isString()) + { + return value; + } + return variant_.strValue; + } + + inline std::vector const& value::asStringList() const { @@ -306,6 +360,16 @@ namespace docopt { return variant_.strList; } + inline + std::vector const& value::asStringListOr(std::vector const& value) const noexcept + { + if(!isStringList()) + { + return value; + } + return variant_.strList; + } + inline bool operator==(value const& v1, value const& v2) {