Commit 87494a2270823bae49aa7f9dba8fc865ccb700e0
Committed by
Henry Schreiner
1 parent
dab61c01
Allow Optional search to be disabled by user
Showing
4 changed files
with
23 additions
and
23 deletions
README.md
| @@ -167,7 +167,7 @@ An option name must start with a alphabetic character or underscore. For long op | @@ -167,7 +167,7 @@ An option name must start with a alphabetic character or underscore. For long op | ||
| 167 | 167 | ||
| 168 | 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. | 168 | 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. |
| 169 | 169 | ||
| 170 | -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. | 170 | +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. |
| 171 | 171 | ||
| 172 | ### Example | 172 | ### Example |
| 173 | 173 |
include/CLI/Optional.hpp
| @@ -12,37 +12,37 @@ | @@ -12,37 +12,37 @@ | ||
| 12 | 12 | ||
| 13 | #if defined(CLI11_CPP17) && __has_include(<optional>) && \ | 13 | #if defined(CLI11_CPP17) && __has_include(<optional>) && \ |
| 14 | defined(__cpp_lib_optional) && !defined(CLI11_STD_OPTIONAL) | 14 | defined(__cpp_lib_optional) && !defined(CLI11_STD_OPTIONAL) |
| 15 | -#define CLI11_STD_OPTIONAL | 15 | +#define CLI11_STD_OPTIONAL 1 |
| 16 | #endif | 16 | #endif |
| 17 | 17 | ||
| 18 | #if defined(CLI11_CPP14) && __has_include(<experimental/optional>) && \ | 18 | #if defined(CLI11_CPP14) && __has_include(<experimental/optional>) && \ |
| 19 | !defined(CLI11_EXPERIMENTAL_OPTIONAL) | 19 | !defined(CLI11_EXPERIMENTAL_OPTIONAL) |
| 20 | -#define CLI11_EXPERIMENTAL_OPTIONAL | 20 | +#define CLI11_EXPERIMENTAL_OPTIONAL 1 |
| 21 | #endif | 21 | #endif |
| 22 | 22 | ||
| 23 | #if __has_include(<boost/optional.hpp>) && !defined(CLI11_BOOST_OPTIONAL) | 23 | #if __has_include(<boost/optional.hpp>) && !defined(CLI11_BOOST_OPTIONAL) |
| 24 | #include <boost/version.hpp> | 24 | #include <boost/version.hpp> |
| 25 | #if BOOST_VERSION >= 105800 | 25 | #if BOOST_VERSION >= 105800 |
| 26 | -#define CLI11_BOOST_OPTIONAL | 26 | +#define CLI11_BOOST_OPTIONAL 1 |
| 27 | #endif | 27 | #endif |
| 28 | #endif | 28 | #endif |
| 29 | 29 | ||
| 30 | #endif | 30 | #endif |
| 31 | 31 | ||
| 32 | -#ifdef CLI11_STD_OPTIONAL | 32 | +#if CLI11_STD_OPTIONAL |
| 33 | #include <optional> | 33 | #include <optional> |
| 34 | #endif | 34 | #endif |
| 35 | -#ifdef CLI11_EXPERIMENTAL_OPTIONAL | 35 | +#if CLI11_EXPERIMENTAL_OPTIONAL |
| 36 | #include <experimental/optional> | 36 | #include <experimental/optional> |
| 37 | #endif | 37 | #endif |
| 38 | -#ifdef CLI11_BOOST_OPTIONAL | 38 | +#if CLI11_BOOST_OPTIONAL |
| 39 | #include <boost/optional.hpp> | 39 | #include <boost/optional.hpp> |
| 40 | #endif | 40 | #endif |
| 41 | // [CLI11:verbatim] | 41 | // [CLI11:verbatim] |
| 42 | 42 | ||
| 43 | namespace CLI { | 43 | namespace CLI { |
| 44 | 44 | ||
| 45 | -#ifdef CLI11_STD_OPTIONAL | 45 | +#if CLI11_STD_OPTIONAL |
| 46 | template <typename T> std::istream &operator>>(std::istream &in, std::optional<T> &val) { | 46 | template <typename T> std::istream &operator>>(std::istream &in, std::optional<T> &val) { |
| 47 | T v; | 47 | T v; |
| 48 | in >> v; | 48 | in >> v; |
| @@ -51,7 +51,7 @@ template <typename T> std::istream &operator>>(std::istream &in, std::optional<T | @@ -51,7 +51,7 @@ template <typename T> std::istream &operator>>(std::istream &in, std::optional<T | ||
| 51 | } | 51 | } |
| 52 | #endif | 52 | #endif |
| 53 | 53 | ||
| 54 | -#ifdef CLI11_EXPERIMENTAL_OPTIONAL | 54 | +#if CLI11_EXPERIMENTAL_OPTIONAL |
| 55 | template <typename T> std::istream &operator>>(std::istream &in, std::experimental::optional<T> &val) { | 55 | template <typename T> std::istream &operator>>(std::istream &in, std::experimental::optional<T> &val) { |
| 56 | T v; | 56 | T v; |
| 57 | in >> v; | 57 | in >> v; |
| @@ -60,7 +60,7 @@ template <typename T> std::istream &operator>>(std::istream &in, std::experiment | @@ -60,7 +60,7 @@ template <typename T> std::istream &operator>>(std::istream &in, std::experiment | ||
| 60 | } | 60 | } |
| 61 | #endif | 61 | #endif |
| 62 | 62 | ||
| 63 | -#ifdef CLI11_BOOST_OPTIONAL | 63 | +#if CLI11_BOOST_OPTIONAL |
| 64 | template <typename T> std::istream &operator>>(std::istream &in, boost::optional<T> &val) { | 64 | template <typename T> std::istream &operator>>(std::istream &in, boost::optional<T> &val) { |
| 65 | T v; | 65 | T v; |
| 66 | in >> v; | 66 | in >> v; |
| @@ -70,17 +70,17 @@ template <typename T> std::istream &operator>>(std::istream &in, boost::optional | @@ -70,17 +70,17 @@ template <typename T> std::istream &operator>>(std::istream &in, boost::optional | ||
| 70 | #endif | 70 | #endif |
| 71 | 71 | ||
| 72 | // Export the best optional to the CLI namespace | 72 | // Export the best optional to the CLI namespace |
| 73 | -#if defined(CLI11_STD_OPTIONAL) | 73 | +#if CLI11_STD_OPTIONAL |
| 74 | using std::optional; | 74 | using std::optional; |
| 75 | -#elif defined(CLI11_EXPERIMENTAL_OPTIONAL) | 75 | +#elif CLI11_EXPERIMENTAL_OPTIONAL |
| 76 | using std::experimental::optional; | 76 | using std::experimental::optional; |
| 77 | -#elif defined(CLI11_BOOST_OPTIONAL) | 77 | +#elif CLI11_BOOST_OPTIONAL |
| 78 | using boost::optional; | 78 | using boost::optional; |
| 79 | #endif | 79 | #endif |
| 80 | 80 | ||
| 81 | // This is true if any optional is found | 81 | // This is true if any optional is found |
| 82 | -#if defined(CLI11_STD_OPTIONAL) || defined(CLI11_EXPERIMENTAL_OPTIONAL) || defined(CLI11_BOOST_OPTIONAL) | ||
| 83 | -#define CLI11_OPTIONAL | 82 | +#if CLI11_STD_OPTIONAL || CLI11_EXPERIMENTAL_OPTIONAL || CLI11_BOOST_OPTIONAL |
| 83 | +#define CLI11_OPTIONAL 1 | ||
| 84 | #endif | 84 | #endif |
| 85 | 85 | ||
| 86 | } // namespace CLI | 86 | } // namespace CLI |
tests/OptionalTest.cpp
| @@ -3,7 +3,7 @@ | @@ -3,7 +3,7 @@ | ||
| 3 | 3 | ||
| 4 | #include "app_helper.hpp" | 4 | #include "app_helper.hpp" |
| 5 | 5 | ||
| 6 | -#ifdef CLI11_STD_OPTIONAL | 6 | +#if CLI11_STD_OPTIONAL |
| 7 | 7 | ||
| 8 | TEST_F(TApp, StdOptionalTest) { | 8 | TEST_F(TApp, StdOptionalTest) { |
| 9 | std::optional<int> opt; | 9 | std::optional<int> opt; |
| @@ -25,7 +25,7 @@ TEST_F(TApp, StdOptionalTest) { | @@ -25,7 +25,7 @@ TEST_F(TApp, StdOptionalTest) { | ||
| 25 | } | 25 | } |
| 26 | 26 | ||
| 27 | #endif | 27 | #endif |
| 28 | -#ifdef CLI11_EXPERIMENTAL_OPTIONAL | 28 | +#if CLI11_EXPERIMENTAL_OPTIONAL |
| 29 | 29 | ||
| 30 | TEST_F(TApp, ExperimentalOptionalTest) { | 30 | TEST_F(TApp, ExperimentalOptionalTest) { |
| 31 | std::experimental::optional<int> opt; | 31 | std::experimental::optional<int> opt; |
| @@ -47,7 +47,7 @@ TEST_F(TApp, ExperimentalOptionalTest) { | @@ -47,7 +47,7 @@ TEST_F(TApp, ExperimentalOptionalTest) { | ||
| 47 | } | 47 | } |
| 48 | 48 | ||
| 49 | #endif | 49 | #endif |
| 50 | -#ifdef CLI11_BOOST_OPTIONAL | 50 | +#if CLI11_BOOST_OPTIONAL |
| 51 | 51 | ||
| 52 | TEST_F(TApp, BoostOptionalTest) { | 52 | TEST_F(TApp, BoostOptionalTest) { |
| 53 | boost::optional<int> opt; | 53 | boost::optional<int> opt; |
| @@ -70,6 +70,6 @@ TEST_F(TApp, BoostOptionalTest) { | @@ -70,6 +70,6 @@ TEST_F(TApp, BoostOptionalTest) { | ||
| 70 | 70 | ||
| 71 | #endif | 71 | #endif |
| 72 | 72 | ||
| 73 | -#ifndef CLI11_OPTIONAL | 73 | +#if !CLI11_OPTIONAL |
| 74 | TEST_F(TApp, DISABLED_OptionalTest) {} | 74 | TEST_F(TApp, DISABLED_OptionalTest) {} |
| 75 | #endif | 75 | #endif |
tests/informational.cpp
| @@ -28,21 +28,21 @@ int main() { | @@ -28,21 +28,21 @@ int main() { | ||
| 28 | std::cout << "no\n"; | 28 | std::cout << "no\n"; |
| 29 | #endif | 29 | #endif |
| 30 | 30 | ||
| 31 | -#ifdef CLI11_OPTIONAL | 31 | +#if CLI11_OPTIONAL |
| 32 | std::cout << " [Available as CLI::optional]"; | 32 | std::cout << " [Available as CLI::optional]"; |
| 33 | #else | 33 | #else |
| 34 | std::cout << " No optional library found\n"; | 34 | std::cout << " No optional library found\n"; |
| 35 | #endif | 35 | #endif |
| 36 | 36 | ||
| 37 | -#ifdef CLI11_STD_OPTIONAL | 37 | +#if CLI11_STD_OPTIONAL |
| 38 | std::cout << " std::optional support active\n"; | 38 | std::cout << " std::optional support active\n"; |
| 39 | #endif | 39 | #endif |
| 40 | 40 | ||
| 41 | -#ifdef CLI11_EXPERIMENTAL_OPTIONAL | 41 | +#if CLI11_EXPERIMENTAL_OPTIONAL |
| 42 | std::cout << " std::experimental::optional support active\n"; | 42 | std::cout << " std::experimental::optional support active\n"; |
| 43 | #endif | 43 | #endif |
| 44 | 44 | ||
| 45 | -#ifdef CLI11_BOOST_OPTIONAL | 45 | +#if CLI11_BOOST_OPTIONAL |
| 46 | std::cout << " boost::optional support active\n"; | 46 | std::cout << " boost::optional support active\n"; |
| 47 | #endif | 47 | #endif |
| 48 | 48 |