mirror of
https://github.com/CLIUtils/CLI11.git
synced 2025-05-01 13:13:53 +00:00
Fix std::string_view support in transforming validators (#300)
* Fix std::string_view support in transforming validators * Fix single header * Fix formatting * Be more careful * fix string_view test
This commit is contained in:
parent
68c8b1b789
commit
f0c0794aa7
@ -10,6 +10,17 @@
|
|||||||
#include <type_traits>
|
#include <type_traits>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
// [CLI11:verbatim]
|
||||||
|
#if defined(CLI11_CPP17)
|
||||||
|
#if defined(__has_include)
|
||||||
|
#if __has_include(<string_view>)
|
||||||
|
#include <string_view>
|
||||||
|
#define CLI11_HAS_STRING_VIEW
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
// [CLI11:verbatim]
|
||||||
|
|
||||||
namespace CLI {
|
namespace CLI {
|
||||||
|
|
||||||
// Type tools
|
// Type tools
|
||||||
@ -72,6 +83,10 @@ template <typename T> struct IsMemberType { using type = T; };
|
|||||||
/// The main custom type needed here is const char * should be a string.
|
/// The main custom type needed here is const char * should be a string.
|
||||||
template <> struct IsMemberType<const char *> { using type = std::string; };
|
template <> struct IsMemberType<const char *> { using type = std::string; };
|
||||||
|
|
||||||
|
#ifdef CLI11_HAS_STRING_VIEW
|
||||||
|
template <> struct IsMemberType<std::string_view> { using type = std::string; };
|
||||||
|
#endif
|
||||||
|
|
||||||
namespace detail {
|
namespace detail {
|
||||||
|
|
||||||
// These are utilities for IsMember
|
// These are utilities for IsMember
|
||||||
|
@ -503,7 +503,7 @@ auto search(const T &set, const V &val, const std::function<V(V)> &filter_functi
|
|||||||
// if we haven't found it do the longer linear search with all the element translations
|
// if we haven't found it do the longer linear search with all the element translations
|
||||||
auto &setref = detail::smart_deref(set);
|
auto &setref = detail::smart_deref(set);
|
||||||
auto it = std::find_if(std::begin(setref), std::end(setref), [&](decltype(*std::begin(setref)) v) {
|
auto it = std::find_if(std::begin(setref), std::end(setref), [&](decltype(*std::begin(setref)) v) {
|
||||||
V a = detail::pair_adaptor<element_t>::first(v);
|
V a{detail::pair_adaptor<element_t>::first(v)};
|
||||||
a = filter_function(a);
|
a = filter_function(a);
|
||||||
return (a == val);
|
return (a == val);
|
||||||
});
|
});
|
||||||
|
@ -102,6 +102,18 @@ TEST_F(TApp, SimpleTransformFn) {
|
|||||||
EXPECT_EQ(value, 1);
|
EXPECT_EQ(value, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if defined(CLI11_HAS_STRING_VIEW)
|
||||||
|
TEST_F(TApp, StringViewTransformFn) {
|
||||||
|
std::string value;
|
||||||
|
std::map<std::string_view, std::string_view> map = {// key length > std::string().capacity() [SSO length]
|
||||||
|
{"a-rather-long-argument", "mapped"}};
|
||||||
|
app.add_option("-s", value)->transform(CLI::CheckedTransformer(map));
|
||||||
|
args = {"-s", "a-rather-long-argument"};
|
||||||
|
run();
|
||||||
|
EXPECT_EQ(value, "mapped");
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
TEST_F(TApp, SimpleNumericalTransformFn) {
|
TEST_F(TApp, SimpleNumericalTransformFn) {
|
||||||
int value;
|
int value;
|
||||||
auto opt =
|
auto opt =
|
||||||
|
Loading…
x
Reference in New Issue
Block a user