diff --git a/README.md b/README.md index 34974ade..10c50049 100644 --- a/README.md +++ b/README.md @@ -167,7 +167,7 @@ An option name must start with a alphabetic character or underscore. For long op On a C++14 compiler, you can pass a callback function directly to `.add_flag`, while in C++11 mode you'll need to use `.add_flag_function` if you want a callback function. The function will be given the number of times the flag was passed. You can throw a relevant `CLI::ParseError` to signal a failure. -On a compiler that supports C++17's `__has_include`, you can also use `std::optional`, `std::experimental::optional`, and `boost::optional` directly in an `add_option` call. If you don't have `__has_include`, you can define `CLI11_BOOST_OPTIONAL` before including CLI11 to manually add support for `boost::optional`. See [CLI11 Internals] for information on how this was done and how you can add your own converters. +On a compiler that supports C++17's `__has_include`, you can also use `std::optional`, `std::experimental::optional`, and `boost::optional` directly in an `add_option` call. If you don't have `__has_include`, you can define `CLI11_BOOST_OPTIONAL 1` before including CLI11 to manually add support (or 0 to remove) for `boost::optional`. See [CLI11 Internals] for information on how this was done and how you can add your own converters. ### Example diff --git a/include/CLI/Optional.hpp b/include/CLI/Optional.hpp index 88044e9b..a4a03920 100644 --- a/include/CLI/Optional.hpp +++ b/include/CLI/Optional.hpp @@ -12,37 +12,37 @@ #if defined(CLI11_CPP17) && __has_include() && \ defined(__cpp_lib_optional) && !defined(CLI11_STD_OPTIONAL) -#define CLI11_STD_OPTIONAL +#define CLI11_STD_OPTIONAL 1 #endif #if defined(CLI11_CPP14) && __has_include() && \ !defined(CLI11_EXPERIMENTAL_OPTIONAL) -#define CLI11_EXPERIMENTAL_OPTIONAL +#define CLI11_EXPERIMENTAL_OPTIONAL 1 #endif #if __has_include() && !defined(CLI11_BOOST_OPTIONAL) #include #if BOOST_VERSION >= 105800 -#define CLI11_BOOST_OPTIONAL +#define CLI11_BOOST_OPTIONAL 1 #endif #endif #endif -#ifdef CLI11_STD_OPTIONAL +#if CLI11_STD_OPTIONAL #include #endif -#ifdef CLI11_EXPERIMENTAL_OPTIONAL +#if CLI11_EXPERIMENTAL_OPTIONAL #include #endif -#ifdef CLI11_BOOST_OPTIONAL +#if CLI11_BOOST_OPTIONAL #include #endif // [CLI11:verbatim] namespace CLI { -#ifdef CLI11_STD_OPTIONAL +#if CLI11_STD_OPTIONAL template std::istream &operator>>(std::istream &in, std::optional &val) { T v; in >> v; @@ -51,7 +51,7 @@ template std::istream &operator>>(std::istream &in, std::optional std::istream &operator>>(std::istream &in, std::experimental::optional &val) { T v; in >> v; @@ -60,7 +60,7 @@ template std::istream &operator>>(std::istream &in, std::experiment } #endif -#ifdef CLI11_BOOST_OPTIONAL +#if CLI11_BOOST_OPTIONAL template std::istream &operator>>(std::istream &in, boost::optional &val) { T v; in >> v; @@ -70,17 +70,17 @@ template std::istream &operator>>(std::istream &in, boost::optional #endif // Export the best optional to the CLI namespace -#if defined(CLI11_STD_OPTIONAL) +#if CLI11_STD_OPTIONAL using std::optional; -#elif defined(CLI11_EXPERIMENTAL_OPTIONAL) +#elif CLI11_EXPERIMENTAL_OPTIONAL using std::experimental::optional; -#elif defined(CLI11_BOOST_OPTIONAL) +#elif CLI11_BOOST_OPTIONAL using boost::optional; #endif // This is true if any optional is found -#if defined(CLI11_STD_OPTIONAL) || defined(CLI11_EXPERIMENTAL_OPTIONAL) || defined(CLI11_BOOST_OPTIONAL) -#define CLI11_OPTIONAL +#if CLI11_STD_OPTIONAL || CLI11_EXPERIMENTAL_OPTIONAL || CLI11_BOOST_OPTIONAL +#define CLI11_OPTIONAL 1 #endif } // namespace CLI diff --git a/tests/OptionalTest.cpp b/tests/OptionalTest.cpp index b67e4ade..6c519e1f 100644 --- a/tests/OptionalTest.cpp +++ b/tests/OptionalTest.cpp @@ -3,7 +3,7 @@ #include "app_helper.hpp" -#ifdef CLI11_STD_OPTIONAL +#if CLI11_STD_OPTIONAL TEST_F(TApp, StdOptionalTest) { std::optional opt; @@ -25,7 +25,7 @@ TEST_F(TApp, StdOptionalTest) { } #endif -#ifdef CLI11_EXPERIMENTAL_OPTIONAL +#if CLI11_EXPERIMENTAL_OPTIONAL TEST_F(TApp, ExperimentalOptionalTest) { std::experimental::optional opt; @@ -47,7 +47,7 @@ TEST_F(TApp, ExperimentalOptionalTest) { } #endif -#ifdef CLI11_BOOST_OPTIONAL +#if CLI11_BOOST_OPTIONAL TEST_F(TApp, BoostOptionalTest) { boost::optional opt; @@ -70,6 +70,6 @@ TEST_F(TApp, BoostOptionalTest) { #endif -#ifndef CLI11_OPTIONAL +#if !CLI11_OPTIONAL TEST_F(TApp, DISABLED_OptionalTest) {} #endif diff --git a/tests/informational.cpp b/tests/informational.cpp index 89c73ff9..b5a0098c 100644 --- a/tests/informational.cpp +++ b/tests/informational.cpp @@ -28,21 +28,21 @@ int main() { std::cout << "no\n"; #endif -#ifdef CLI11_OPTIONAL +#if CLI11_OPTIONAL std::cout << " [Available as CLI::optional]"; #else std::cout << " No optional library found\n"; #endif -#ifdef CLI11_STD_OPTIONAL +#if CLI11_STD_OPTIONAL std::cout << " std::optional support active\n"; #endif -#ifdef CLI11_EXPERIMENTAL_OPTIONAL +#if CLI11_EXPERIMENTAL_OPTIONAL std::cout << " std::experimental::optional support active\n"; #endif -#ifdef CLI11_BOOST_OPTIONAL +#if CLI11_BOOST_OPTIONAL std::cout << " boost::optional support active\n"; #endif