1
0
mirror of https://github.com/CLIUtils/CLI11.git synced 2025-04-29 20:23:55 +00:00

Allow Optional search to be disabled by user

This commit is contained in:
Henry Fredrick Schreiner 2018-06-01 08:43:26 +02:00 committed by Henry Schreiner
parent dab61c0107
commit 87494a2270
4 changed files with 23 additions and 23 deletions

View File

@ -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

View File

@ -12,37 +12,37 @@
#if defined(CLI11_CPP17) && __has_include(<optional>) && \
defined(__cpp_lib_optional) && !defined(CLI11_STD_OPTIONAL)
#define CLI11_STD_OPTIONAL
#define CLI11_STD_OPTIONAL 1
#endif
#if defined(CLI11_CPP14) && __has_include(<experimental/optional>) && \
!defined(CLI11_EXPERIMENTAL_OPTIONAL)
#define CLI11_EXPERIMENTAL_OPTIONAL
#define CLI11_EXPERIMENTAL_OPTIONAL 1
#endif
#if __has_include(<boost/optional.hpp>) && !defined(CLI11_BOOST_OPTIONAL)
#include <boost/version.hpp>
#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 <optional>
#endif
#ifdef CLI11_EXPERIMENTAL_OPTIONAL
#if CLI11_EXPERIMENTAL_OPTIONAL
#include <experimental/optional>
#endif
#ifdef CLI11_BOOST_OPTIONAL
#if CLI11_BOOST_OPTIONAL
#include <boost/optional.hpp>
#endif
// [CLI11:verbatim]
namespace CLI {
#ifdef CLI11_STD_OPTIONAL
#if CLI11_STD_OPTIONAL
template <typename T> std::istream &operator>>(std::istream &in, std::optional<T> &val) {
T v;
in >> v;
@ -51,7 +51,7 @@ template <typename T> std::istream &operator>>(std::istream &in, std::optional<T
}
#endif
#ifdef CLI11_EXPERIMENTAL_OPTIONAL
#if CLI11_EXPERIMENTAL_OPTIONAL
template <typename T> std::istream &operator>>(std::istream &in, std::experimental::optional<T> &val) {
T v;
in >> v;
@ -60,7 +60,7 @@ template <typename T> std::istream &operator>>(std::istream &in, std::experiment
}
#endif
#ifdef CLI11_BOOST_OPTIONAL
#if CLI11_BOOST_OPTIONAL
template <typename T> std::istream &operator>>(std::istream &in, boost::optional<T> &val) {
T v;
in >> v;
@ -70,17 +70,17 @@ template <typename T> 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

View File

@ -3,7 +3,7 @@
#include "app_helper.hpp"
#ifdef CLI11_STD_OPTIONAL
#if CLI11_STD_OPTIONAL
TEST_F(TApp, StdOptionalTest) {
std::optional<int> 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<int> opt;
@ -47,7 +47,7 @@ TEST_F(TApp, ExperimentalOptionalTest) {
}
#endif
#ifdef CLI11_BOOST_OPTIONAL
#if CLI11_BOOST_OPTIONAL
TEST_F(TApp, BoostOptionalTest) {
boost::optional<int> opt;
@ -70,6 +70,6 @@ TEST_F(TApp, BoostOptionalTest) {
#endif
#ifndef CLI11_OPTIONAL
#if !CLI11_OPTIONAL
TEST_F(TApp, DISABLED_OptionalTest) {}
#endif

View File

@ -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