Skip to content
This repository has been archived by the owner on Oct 28, 2021. It is now read-only.

Add support for std::optional if macro '__cpp_if_constexpr' returns 1 #45

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions include/clara.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,18 @@ namespace detail {
return ParserResult::ok( ParseResultType::Matched );
}

#if __cpp_if_constexpr // is a C++17 feature. If this is there, then std::optional<T> too
// __has_include is not rliable: gcc with -std=c++11 returns true here
#define CLARA_OPTIONAL_VALUES
template<typename T>
inline auto convertInto( std::string const &source, std::optional<T>& target_opt ) -> ParserResult {
T target;
auto result = convertInto(source,target);
if (result)
target_opt = target;
return result;
}
#endif
struct BoundRefBase {
BoundRefBase() = default;
BoundRefBase( BoundRefBase const & ) = delete;
Expand Down
27 changes: 26 additions & 1 deletion src/ClaraTests.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#ifdef CLARA_OPTIONAL_VALUES
#include <optional>
#endif
#include "clara.hpp"

#include "catch.hpp"

#include <iostream>

using namespace clara;

namespace Catch {
Expand Down Expand Up @@ -38,6 +40,29 @@ struct StringMaker<clara::detail::InternalParseResult> {
// other dependencies/ hierarchical parsers
// Exclusive() parser for choices

#ifdef CLARA_OPTIONAL_VALUES
TEST_CASE( "optional value is not set" ) {
std::optional<std::string> name;
auto p = Opt(name, "name")
["-n"]["--name"]
("the name to use");
REQUIRE( !name.has_value() );

}

TEST_CASE( "optional value is set" ) {
std::optional<std::string> name;
auto parser = Opt(name, "name")
["-n"]["--name"]
("the name to use");
auto result = parser.parse( Args{ "TestApp", "-n", "Bill", "-d:123.45", "-f", "test1", "test2" } );
REQUIRE( name.has_value() );
CHECK( result );
CHECK( result.value().type() == ParseResultType::Matched );
REQUIRE( name.value() == "Bill" );

}
#endif
TEST_CASE( "single parsers" ) {

std::string name;
Expand Down
9 changes: 7 additions & 2 deletions third_party/catch.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7408,13 +7408,18 @@ namespace Catch {
m_info.message = builder.m_stream.str();
getResultCapture().pushScopedMessage( m_info );
}

#if defined(_MSC_VER)
#pragma warning(push)
#pragma warning(disable:4996) // std::uncaught_exception is deprecated in C++17
#endif
ScopedMessage::~ScopedMessage() {
if ( !std::uncaught_exception() ){
getResultCapture().popScopedMessage(m_info);
}
}

#if defined(_MSC_VER)
#pragma warning(pop)
#endif
} // end namespace Catch
// end catch_message.cpp
// start catch_notimplemented_exception.cpp
Expand Down