mirror of
https://github.com/CLIUtils/CLI11.git
synced 2025-04-29 12:13:52 +00:00
Allow Optional search to be disabled by user
This commit is contained in:
parent
dab61c0107
commit
87494a2270
@ -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 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
|
### Example
|
||||||
|
|
||||||
|
@ -12,37 +12,37 @@
|
|||||||
|
|
||||||
#if defined(CLI11_CPP17) && __has_include(<optional>) && \
|
#if defined(CLI11_CPP17) && __has_include(<optional>) && \
|
||||||
defined(__cpp_lib_optional) && !defined(CLI11_STD_OPTIONAL)
|
defined(__cpp_lib_optional) && !defined(CLI11_STD_OPTIONAL)
|
||||||
#define CLI11_STD_OPTIONAL
|
#define CLI11_STD_OPTIONAL 1
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if defined(CLI11_CPP14) && __has_include(<experimental/optional>) && \
|
#if defined(CLI11_CPP14) && __has_include(<experimental/optional>) && \
|
||||||
!defined(CLI11_EXPERIMENTAL_OPTIONAL)
|
!defined(CLI11_EXPERIMENTAL_OPTIONAL)
|
||||||
#define CLI11_EXPERIMENTAL_OPTIONAL
|
#define CLI11_EXPERIMENTAL_OPTIONAL 1
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if __has_include(<boost/optional.hpp>) && !defined(CLI11_BOOST_OPTIONAL)
|
#if __has_include(<boost/optional.hpp>) && !defined(CLI11_BOOST_OPTIONAL)
|
||||||
#include <boost/version.hpp>
|
#include <boost/version.hpp>
|
||||||
#if BOOST_VERSION >= 105800
|
#if BOOST_VERSION >= 105800
|
||||||
#define CLI11_BOOST_OPTIONAL
|
#define CLI11_BOOST_OPTIONAL 1
|
||||||
#endif
|
#endif
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef CLI11_STD_OPTIONAL
|
#if CLI11_STD_OPTIONAL
|
||||||
#include <optional>
|
#include <optional>
|
||||||
#endif
|
#endif
|
||||||
#ifdef CLI11_EXPERIMENTAL_OPTIONAL
|
#if CLI11_EXPERIMENTAL_OPTIONAL
|
||||||
#include <experimental/optional>
|
#include <experimental/optional>
|
||||||
#endif
|
#endif
|
||||||
#ifdef CLI11_BOOST_OPTIONAL
|
#if CLI11_BOOST_OPTIONAL
|
||||||
#include <boost/optional.hpp>
|
#include <boost/optional.hpp>
|
||||||
#endif
|
#endif
|
||||||
// [CLI11:verbatim]
|
// [CLI11:verbatim]
|
||||||
|
|
||||||
namespace CLI {
|
namespace CLI {
|
||||||
|
|
||||||
#ifdef CLI11_STD_OPTIONAL
|
#if CLI11_STD_OPTIONAL
|
||||||
template <typename T> std::istream &operator>>(std::istream &in, std::optional<T> &val) {
|
template <typename T> std::istream &operator>>(std::istream &in, std::optional<T> &val) {
|
||||||
T v;
|
T v;
|
||||||
in >> v;
|
in >> v;
|
||||||
@ -51,7 +51,7 @@ template <typename T> std::istream &operator>>(std::istream &in, std::optional<T
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef CLI11_EXPERIMENTAL_OPTIONAL
|
#if CLI11_EXPERIMENTAL_OPTIONAL
|
||||||
template <typename T> std::istream &operator>>(std::istream &in, std::experimental::optional<T> &val) {
|
template <typename T> std::istream &operator>>(std::istream &in, std::experimental::optional<T> &val) {
|
||||||
T v;
|
T v;
|
||||||
in >> v;
|
in >> v;
|
||||||
@ -60,7 +60,7 @@ template <typename T> std::istream &operator>>(std::istream &in, std::experiment
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef CLI11_BOOST_OPTIONAL
|
#if CLI11_BOOST_OPTIONAL
|
||||||
template <typename T> std::istream &operator>>(std::istream &in, boost::optional<T> &val) {
|
template <typename T> std::istream &operator>>(std::istream &in, boost::optional<T> &val) {
|
||||||
T v;
|
T v;
|
||||||
in >> v;
|
in >> v;
|
||||||
@ -70,17 +70,17 @@ template <typename T> std::istream &operator>>(std::istream &in, boost::optional
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
// Export the best optional to the CLI namespace
|
// Export the best optional to the CLI namespace
|
||||||
#if defined(CLI11_STD_OPTIONAL)
|
#if CLI11_STD_OPTIONAL
|
||||||
using std::optional;
|
using std::optional;
|
||||||
#elif defined(CLI11_EXPERIMENTAL_OPTIONAL)
|
#elif CLI11_EXPERIMENTAL_OPTIONAL
|
||||||
using std::experimental::optional;
|
using std::experimental::optional;
|
||||||
#elif defined(CLI11_BOOST_OPTIONAL)
|
#elif CLI11_BOOST_OPTIONAL
|
||||||
using boost::optional;
|
using boost::optional;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// This is true if any optional is found
|
// This is true if any optional is found
|
||||||
#if defined(CLI11_STD_OPTIONAL) || defined(CLI11_EXPERIMENTAL_OPTIONAL) || defined(CLI11_BOOST_OPTIONAL)
|
#if CLI11_STD_OPTIONAL || CLI11_EXPERIMENTAL_OPTIONAL || CLI11_BOOST_OPTIONAL
|
||||||
#define CLI11_OPTIONAL
|
#define CLI11_OPTIONAL 1
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
} // namespace CLI
|
} // namespace CLI
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
|
|
||||||
#include "app_helper.hpp"
|
#include "app_helper.hpp"
|
||||||
|
|
||||||
#ifdef CLI11_STD_OPTIONAL
|
#if CLI11_STD_OPTIONAL
|
||||||
|
|
||||||
TEST_F(TApp, StdOptionalTest) {
|
TEST_F(TApp, StdOptionalTest) {
|
||||||
std::optional<int> opt;
|
std::optional<int> opt;
|
||||||
@ -25,7 +25,7 @@ TEST_F(TApp, StdOptionalTest) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
#ifdef CLI11_EXPERIMENTAL_OPTIONAL
|
#if CLI11_EXPERIMENTAL_OPTIONAL
|
||||||
|
|
||||||
TEST_F(TApp, ExperimentalOptionalTest) {
|
TEST_F(TApp, ExperimentalOptionalTest) {
|
||||||
std::experimental::optional<int> opt;
|
std::experimental::optional<int> opt;
|
||||||
@ -47,7 +47,7 @@ TEST_F(TApp, ExperimentalOptionalTest) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
#ifdef CLI11_BOOST_OPTIONAL
|
#if CLI11_BOOST_OPTIONAL
|
||||||
|
|
||||||
TEST_F(TApp, BoostOptionalTest) {
|
TEST_F(TApp, BoostOptionalTest) {
|
||||||
boost::optional<int> opt;
|
boost::optional<int> opt;
|
||||||
@ -70,6 +70,6 @@ TEST_F(TApp, BoostOptionalTest) {
|
|||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifndef CLI11_OPTIONAL
|
#if !CLI11_OPTIONAL
|
||||||
TEST_F(TApp, DISABLED_OptionalTest) {}
|
TEST_F(TApp, DISABLED_OptionalTest) {}
|
||||||
#endif
|
#endif
|
||||||
|
@ -28,21 +28,21 @@ int main() {
|
|||||||
std::cout << "no\n";
|
std::cout << "no\n";
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef CLI11_OPTIONAL
|
#if CLI11_OPTIONAL
|
||||||
std::cout << " [Available as CLI::optional]";
|
std::cout << " [Available as CLI::optional]";
|
||||||
#else
|
#else
|
||||||
std::cout << " No optional library found\n";
|
std::cout << " No optional library found\n";
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef CLI11_STD_OPTIONAL
|
#if CLI11_STD_OPTIONAL
|
||||||
std::cout << " std::optional support active\n";
|
std::cout << " std::optional support active\n";
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef CLI11_EXPERIMENTAL_OPTIONAL
|
#if CLI11_EXPERIMENTAL_OPTIONAL
|
||||||
std::cout << " std::experimental::optional support active\n";
|
std::cout << " std::experimental::optional support active\n";
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef CLI11_BOOST_OPTIONAL
|
#if CLI11_BOOST_OPTIONAL
|
||||||
std::cout << " boost::optional support active\n";
|
std::cout << " boost::optional support active\n";
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user