Skip to content

Commit 2a5adc6

Browse files
committed
Allow Optional search to be disabled by user
1 parent a6e92f5 commit 2a5adc6

File tree

4 files changed

+23
-23
lines changed

4 files changed

+23
-23
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ An option name must start with a alphabetic character or underscore. For long op
167167

168168
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.
169169

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.
171171

172172
### Example
173173

include/CLI/Optional.hpp

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,37 +12,37 @@
1212

1313
#if defined(CLI11_CPP17) && __has_include(<optional>) && \
1414
defined(__cpp_lib_optional) && !defined(CLI11_STD_OPTIONAL)
15-
#define CLI11_STD_OPTIONAL
15+
#define CLI11_STD_OPTIONAL 1
1616
#endif
1717

1818
#if defined(CLI11_CPP14) && __has_include(<experimental/optional>) && \
1919
!defined(CLI11_EXPERIMENTAL_OPTIONAL)
20-
#define CLI11_EXPERIMENTAL_OPTIONAL
20+
#define CLI11_EXPERIMENTAL_OPTIONAL 1
2121
#endif
2222

2323
#if __has_include(<boost/optional.hpp>) && !defined(CLI11_BOOST_OPTIONAL)
2424
#include <boost/version.hpp>
2525
#if BOOST_VERSION >= 105800
26-
#define CLI11_BOOST_OPTIONAL
26+
#define CLI11_BOOST_OPTIONAL 1
2727
#endif
2828
#endif
2929

3030
#endif
3131

32-
#ifdef CLI11_STD_OPTIONAL
32+
#if CLI11_STD_OPTIONAL
3333
#include <optional>
3434
#endif
35-
#ifdef CLI11_EXPERIMENTAL_OPTIONAL
35+
#if CLI11_EXPERIMENTAL_OPTIONAL
3636
#include <experimental/optional>
3737
#endif
38-
#ifdef CLI11_BOOST_OPTIONAL
38+
#if CLI11_BOOST_OPTIONAL
3939
#include <boost/optional.hpp>
4040
#endif
4141
// [CLI11:verbatim]
4242

4343
namespace CLI {
4444

45-
#ifdef CLI11_STD_OPTIONAL
45+
#if CLI11_STD_OPTIONAL
4646
template <typename T> std::istream &operator>>(std::istream &in, std::optional<T> &val) {
4747
T v;
4848
in >> v;
@@ -51,7 +51,7 @@ template <typename T> std::istream &operator>>(std::istream &in, std::optional<T
5151
}
5252
#endif
5353

54-
#ifdef CLI11_EXPERIMENTAL_OPTIONAL
54+
#if CLI11_EXPERIMENTAL_OPTIONAL
5555
template <typename T> std::istream &operator>>(std::istream &in, std::experimental::optional<T> &val) {
5656
T v;
5757
in >> v;
@@ -60,7 +60,7 @@ template <typename T> std::istream &operator>>(std::istream &in, std::experiment
6060
}
6161
#endif
6262

63-
#ifdef CLI11_BOOST_OPTIONAL
63+
#if CLI11_BOOST_OPTIONAL
6464
template <typename T> std::istream &operator>>(std::istream &in, boost::optional<T> &val) {
6565
T v;
6666
in >> v;
@@ -70,17 +70,17 @@ template <typename T> std::istream &operator>>(std::istream &in, boost::optional
7070
#endif
7171

7272
// Export the best optional to the CLI namespace
73-
#if defined(CLI11_STD_OPTIONAL)
73+
#if CLI11_STD_OPTIONAL
7474
using std::optional;
75-
#elif defined(CLI11_EXPERIMENTAL_OPTIONAL)
75+
#elif CLI11_EXPERIMENTAL_OPTIONAL
7676
using std::experimental::optional;
77-
#elif defined(CLI11_BOOST_OPTIONAL)
77+
#elif CLI11_BOOST_OPTIONAL
7878
using boost::optional;
7979
#endif
8080

8181
// 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
8484
#endif
8585

8686
} // namespace CLI

tests/OptionalTest.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
#include "app_helper.hpp"
55

6-
#ifdef CLI11_STD_OPTIONAL
6+
#if CLI11_STD_OPTIONAL
77

88
TEST_F(TApp, StdOptionalTest) {
99
std::optional<int> opt;
@@ -25,7 +25,7 @@ TEST_F(TApp, StdOptionalTest) {
2525
}
2626

2727
#endif
28-
#ifdef CLI11_EXPERIMENTAL_OPTIONAL
28+
#if CLI11_EXPERIMENTAL_OPTIONAL
2929

3030
TEST_F(TApp, ExperimentalOptionalTest) {
3131
std::experimental::optional<int> opt;
@@ -47,7 +47,7 @@ TEST_F(TApp, ExperimentalOptionalTest) {
4747
}
4848

4949
#endif
50-
#ifdef CLI11_BOOST_OPTIONAL
50+
#if CLI11_BOOST_OPTIONAL
5151

5252
TEST_F(TApp, BoostOptionalTest) {
5353
boost::optional<int> opt;
@@ -70,6 +70,6 @@ TEST_F(TApp, BoostOptionalTest) {
7070

7171
#endif
7272

73-
#ifndef CLI11_OPTIONAL
73+
#if !CLI11_OPTIONAL
7474
TEST_F(TApp, DISABLED_OptionalTest) {}
7575
#endif

tests/informational.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,21 @@ int main() {
2828
std::cout << "no\n";
2929
#endif
3030

31-
#ifdef CLI11_OPTIONAL
31+
#if CLI11_OPTIONAL
3232
std::cout << " [Available as CLI::optional]";
3333
#else
3434
std::cout << " No optional library found\n";
3535
#endif
3636

37-
#ifdef CLI11_STD_OPTIONAL
37+
#if CLI11_STD_OPTIONAL
3838
std::cout << " std::optional support active\n";
3939
#endif
4040

41-
#ifdef CLI11_EXPERIMENTAL_OPTIONAL
41+
#if CLI11_EXPERIMENTAL_OPTIONAL
4242
std::cout << " std::experimental::optional support active\n";
4343
#endif
4444

45-
#ifdef CLI11_BOOST_OPTIONAL
45+
#if CLI11_BOOST_OPTIONAL
4646
std::cout << " boost::optional support active\n";
4747
#endif
4848

0 commit comments

Comments
 (0)