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

Commit

Permalink
v1.1.1
Browse files Browse the repository at this point in the history
  • Loading branch information
philsquared committed Jan 22, 2018
1 parent b94d908 commit 5fa7b72
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 49 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Clara 1.1.0
# Clara v1.1.1
[![Build Status](https://travis-ci.org/catchorg/Clara.svg?branch=master)](https://travis-ci.org/catchorg/Clara)
[![Build status](https://ci.appveyor.com/api/projects/status/github/catchorg/Clara?brach=master&svg=true)](https://ci.appveyor.com/project/catchorg/clara)
[![codecov](https://codecov.io/gh/catchorg/Clara/branch/master/graph/badge.svg)](https://codecov.io/gh/catchorg/Clara)
Expand Down Expand Up @@ -94,7 +94,7 @@ To see which direction Clara is going in, please see [the roadmap](Roadmap.md)
## Old version

If you used the earlier, v0.x, version of Clara please note that this is a complete rewrite which assumes C++11 and has
a different interface (composability was a big step forward). Conversion between v0.x and v1.x is a fairly simple and mechanical task, but is a bit of manual
a different interface (composability was a big step forward). Conversion between v0.x and v1.x is a fairly simple and mechanical task, but is a bit of manual
work - so don't take this version until you're ready (and, of course, able to use C++11).

I hope you'll find the new interface an improvement - and this will be built on to offer new features moving forwards.
Expand Down
4 changes: 2 additions & 2 deletions include/clara.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
//
// See https://github.com/philsquared/Clara for more details

// Clara v1.1.0
// Clara v1.1.1

#ifndef CLARA_HPP_INCLUDED
#define CLARA_HPP_INCLUDED
Expand Down Expand Up @@ -462,7 +462,7 @@ namespace detail {
public:
template<typename T>
auto operator|( T const &other ) const -> Parser;

template<typename T>
auto operator+( T const &other ) const -> Parser;
};
Expand Down
79 changes: 34 additions & 45 deletions single_include/clara.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
//
// See https://github.com/philsquared/Clara for more details

// Clara v1.1.0
// Clara v1.1.1

#ifndef CLARA_HPP_INCLUDED
#define CLARA_HPP_INCLUDED
Expand Down Expand Up @@ -659,57 +659,41 @@ namespace detail {
return ParserResult::ok( ParseResultType::Matched );
}

struct BoundRefBase {
BoundRefBase() = default;
BoundRefBase( BoundRefBase const & ) = delete;
BoundRefBase( BoundRefBase && ) = delete;
BoundRefBase &operator=( BoundRefBase const & ) = delete;
BoundRefBase &operator=( BoundRefBase && ) = delete;

virtual ~BoundRefBase() = default;
struct NonCopyable {
NonCopyable() = default;
NonCopyable( NonCopyable const & ) = delete;
NonCopyable( NonCopyable && ) = delete;
NonCopyable &operator=( NonCopyable const & ) = delete;
NonCopyable &operator=( NonCopyable && ) = delete;
};

virtual auto isFlag() const -> bool = 0;
struct BoundRef : NonCopyable {
virtual ~BoundRef() = default;
virtual auto isContainer() const -> bool { return false; }
virtual auto setValue( std::string const &arg ) -> ParserResult = 0;
virtual auto setFlag( bool flag ) -> ParserResult = 0;
};

struct BoundValueRefBase : BoundRefBase {
auto isFlag() const -> bool override { return false; }

auto setFlag( bool ) -> ParserResult override {
return ParserResult::logicError( "Flags can only be set on boolean fields" );
}
struct BoundValueRefBase : BoundRef {
virtual auto setValue( std::string const &arg ) -> ParserResult = 0;
};

struct BoundFlagRefBase : BoundRefBase {
auto isFlag() const -> bool override { return true; }

auto setValue( std::string const &arg ) -> ParserResult override {
bool flag = false;
auto result = convertInto( arg, flag );
if( result )
setFlag( flag );
return result;
}
struct BoundFlagRefBase : BoundRef {
virtual auto setFlag( bool flag ) -> ParserResult = 0;
};

template<typename T>
struct BoundRef : BoundValueRefBase {
struct BoundValueRef : BoundValueRefBase {
T &m_ref;

explicit BoundRef( T &ref ) : m_ref( ref ) {}
explicit BoundValueRef( T &ref ) : m_ref( ref ) {}

auto setValue( std::string const &arg ) -> ParserResult override {
return convertInto( arg, m_ref );
}
};

template<typename T>
struct BoundRef<std::vector<T>> : BoundValueRefBase {
struct BoundValueRef<std::vector<T>> : BoundValueRefBase {
std::vector<T> &m_ref;

explicit BoundRef( std::vector<T> &ref ) : m_ref( ref ) {}
explicit BoundValueRef( std::vector<T> &ref ) : m_ref( ref ) {}

auto isContainer() const -> bool override { return true; }

Expand Down Expand Up @@ -754,7 +738,7 @@ namespace detail {

template<typename ArgType, typename L>
inline auto invokeLambda( L const &lambda, std::string const &arg ) -> ParserResult {
ArgType temp;
ArgType temp{};
auto result = convertInto( arg, temp );
return !result
? result
Expand Down Expand Up @@ -819,16 +803,16 @@ namespace detail {
class ParserRefImpl : public ComposableParserImpl<DerivedT> {
protected:
Optionality m_optionality = Optionality::Optional;
std::shared_ptr<BoundRefBase> m_ref;
std::shared_ptr<BoundRef> m_ref;
std::string m_hint;
std::string m_description;

explicit ParserRefImpl( std::shared_ptr<BoundRefBase> const &ref ) : m_ref( ref ) {}
explicit ParserRefImpl( std::shared_ptr<BoundRef> const &ref ) : m_ref( ref ) {}

public:
template<typename T>
ParserRefImpl( T &ref, std::string const &hint )
: m_ref( std::make_shared<BoundRef<T>>( ref ) ),
: m_ref( std::make_shared<BoundValueRef<T>>( ref ) ),
m_hint( hint )
{}

Expand Down Expand Up @@ -869,18 +853,18 @@ namespace detail {

class ExeName : public ComposableParserImpl<ExeName> {
std::shared_ptr<std::string> m_name;
std::shared_ptr<BoundRefBase> m_ref;
std::shared_ptr<BoundValueRefBase> m_ref;

template<typename LambdaT>
static auto makeRef(LambdaT const &lambda) -> std::shared_ptr<BoundRefBase> {
static auto makeRef(LambdaT const &lambda) -> std::shared_ptr<BoundValueRefBase> {
return std::make_shared<BoundLambda<LambdaT>>( lambda) ;
}

public:
ExeName() : m_name( std::make_shared<std::string>( "<executable>" ) ) {}

explicit ExeName( std::string &ref ) : ExeName() {
m_ref = std::make_shared<BoundRef<std::string>>( ref );
m_ref = std::make_shared<BoundValueRef<std::string>>( ref );
}

template<typename LambdaT>
Expand Down Expand Up @@ -923,7 +907,10 @@ namespace detail {
if( token.type != TokenType::Argument )
return InternalParseResult::ok( ParseState( ParseResultType::NoMatch, remainingTokens ) );

auto result = m_ref->setValue( remainingTokens->token );
assert( dynamic_cast<detail::BoundValueRefBase*>( m_ref.get() ) );
auto valueRef = static_cast<detail::BoundValueRefBase*>( m_ref.get() );

auto result = valueRef->setValue( remainingTokens->token );
if( !result )
return InternalParseResult( result );
else
Expand Down Expand Up @@ -996,20 +983,22 @@ namespace detail {
if( remainingTokens && remainingTokens->type == TokenType::Option ) {
auto const &token = *remainingTokens;
if( isMatch(token.token ) ) {
if( m_ref->isFlag() ) {
auto result = m_ref->setFlag( true );
if( auto flagRef = dynamic_cast<detail::BoundFlagRefBase*>( m_ref.get() ) ) {
auto result = flagRef->setFlag( true );
if( !result )
return InternalParseResult( result );
if( result.value() == ParseResultType::ShortCircuitAll )
return InternalParseResult::ok( ParseState( result.value(), remainingTokens ) );
} else {
assert( dynamic_cast<detail::BoundValueRefBase*>( m_ref.get() ) );
auto valueRef = static_cast<detail::BoundValueRefBase*>( m_ref.get() );
++remainingTokens;
if( !remainingTokens )
return InternalParseResult::runtimeError( "Expected argument following " + token.token );
auto const &argToken = *remainingTokens;
if( argToken.type != TokenType::Argument )
return InternalParseResult::runtimeError( "Expected argument following " + token.token );
auto result = m_ref->setValue( argToken.token );
auto result = valueRef->setValue( argToken.token );
if( !result )
return InternalParseResult( result );
if( result.value() == ParseResultType::ShortCircuitAll )
Expand Down

0 comments on commit 5fa7b72

Please sign in to comment.