Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
clara::detail::Opt Class Reference

#include <clara.hpp>

Inheritance diagram for clara::detail::Opt:
Collaboration diagram for clara::detail::Opt:

Public Member Functions

template<typename LambdaT >
 Opt (LambdaT const &ref)
 
 Opt (bool &ref)
 
template<typename LambdaT >
 Opt (LambdaT const &ref, std::string const &hint)
 
template<typename T >
 Opt (T &ref, std::string const &hint)
 
auto operator[] (std::string const &optName) -> Opt &
 
auto getHelpColumns () const -> std::vector< HelpColumns >
 
auto isMatch (std::string const &optToken) const -> bool
 
auto parse (std::string const &, TokenStream const &tokens) const -> InternalParseResult override
 
auto validate () const -> Result override
 
auto parse (Args const &args) const -> InternalParseResult
 
- Public Member Functions inherited from clara::detail::ParserRefImpl< Opt >
 ParserRefImpl (T &ref, std::string const &hint)
 
 ParserRefImpl (LambdaT const &ref, std::string const &hint)
 
auto operator() (std::string const &description) -> Opt &
 
auto optional () -> Opt &
 
auto required () -> Opt &
 
auto isOptional () const -> bool
 
auto cardinality () const -> size_t override
 
auto hint () const -> std::string
 
- Public Member Functions inherited from clara::detail::ComposableParserImpl< Opt >
auto operator| (T const &other) const -> Parser
 
auto operator+ (T const &other) const -> Parser
 
- Public Member Functions inherited from clara::detail::ParserBase
virtual ~ParserBase ()=default
 
auto parse (Args const &args) const -> InternalParseResult
 

Protected Attributes

std::vector< std::string > m_optNames
 
- Protected Attributes inherited from clara::detail::ParserRefImpl< Opt >
Optionality m_optionality
 
std::shared_ptr< BoundRefm_ref
 
std::string m_hint
 
std::string m_description
 

Additional Inherited Members

- Protected Member Functions inherited from clara::detail::ParserRefImpl< Opt >
 ParserRefImpl (std::shared_ptr< BoundRef > const &ref)
 

Detailed Description

Definition at line 960 of file clara.hpp.

Constructor & Destructor Documentation

◆ Opt() [1/4]

template<typename LambdaT >
clara::detail::Opt::Opt ( LambdaT const & ref)
inlineexplicit

Definition at line 966 of file clara.hpp.

966: ParserRefImpl( std::make_shared<BoundFlagLambda<LambdaT>>( ref ) ) {}
ParserRefImpl(std::shared_ptr< BoundRef > const &ref)
Definition clara.hpp:840

◆ Opt() [2/4]

clara::detail::Opt::Opt ( bool & ref)
inlineexplicit

Definition at line 968 of file clara.hpp.

968: ParserRefImpl( std::make_shared<BoundFlagRef>( ref ) ) {}

◆ Opt() [3/4]

template<typename LambdaT >
clara::detail::Opt::Opt ( LambdaT const & ref,
std::string const & hint )
inline

Definition at line 971 of file clara.hpp.

971: ParserRefImpl( ref, hint ) {}
auto hint() const -> std::string
Definition clara.hpp:881

◆ Opt() [4/4]

template<typename T >
clara::detail::Opt::Opt ( T & ref,
std::string const & hint )
inline

Definition at line 974 of file clara.hpp.

974: ParserRefImpl( ref, hint ) {}

Member Function Documentation

◆ getHelpColumns()

auto clara::detail::Opt::getHelpColumns ( ) const -> std::vector<HelpColumns>
inline

Definition at line 981 of file clara.hpp.

981 {
982 std::ostringstream oss;
983 bool first = true;
984 for( auto const &opt : m_optNames ) {
985 if (first)
986 first = false;
987 else
988 oss << ", ";
989 oss << opt;
990 }
991 if( !m_hint.empty() )
992 oss << " <" << m_hint << ">";
993 return { { oss.str(), m_description } };
994 }
std::vector< std::string > m_optNames
Definition clara.hpp:962

◆ isMatch()

auto clara::detail::Opt::isMatch ( std::string const & optToken) const -> bool
inline

Definition at line 996 of file clara.hpp.

996 {
997 auto normalisedToken = normaliseOpt( optToken );
998 for( auto const &name : m_optNames ) {
999 if( normaliseOpt( name ) == normalisedToken )
1000 return true;
1001 }
1002 return false;
1003 }
std::string name
auto normaliseOpt(std::string const &optName) -> std::string
Definition clara.hpp:951
Here is the call graph for this function:
Here is the caller graph for this function:

◆ operator[]()

auto clara::detail::Opt::operator[] ( std::string const & optName) -> Opt &
inline

Definition at line 976 of file clara.hpp.

976 {
977 m_optNames.push_back( optName );
978 return *this;
979 }

◆ parse() [1/2]

auto clara::detail::ParserBase::parse ( Args const & args) const -> InternalParseResult
inline

Definition at line 816 of file clara.hpp.

816 {
817 return parse( args.exeName(), TokenStream( args ) );
818 }
auto parse(std::string const &, TokenStream const &tokens) const -> InternalParseResult override
Definition clara.hpp:1007

◆ parse() [2/2]

auto clara::detail::Opt::parse ( std::string const & ,
TokenStream const & tokens ) const -> InternalParseResult
inlineoverridevirtual

Implements clara::detail::ParserBase.

Definition at line 1007 of file clara.hpp.

1007 {
1008 auto validationResult = validate();
1009 if( !validationResult )
1010 return InternalParseResult( validationResult );
1011
1012 auto remainingTokens = tokens;
1013 if( remainingTokens && remainingTokens->type == TokenType::Option ) {
1014 auto const &token = *remainingTokens;
1015 if( isMatch(token.token ) ) {
1016 if( m_ref->isFlag() ) {
1017 auto flagRef = static_cast<detail::BoundFlagRefBase*>( m_ref.get() );
1018 auto result = flagRef->setFlag( true );
1019 if( !result )
1020 return InternalParseResult( result );
1021 if( result.value() == ParseResultType::ShortCircuitAll )
1022 return InternalParseResult::ok( ParseState( result.value(), remainingTokens ) );
1023 } else {
1024 auto valueRef = static_cast<detail::BoundValueRefBase*>( m_ref.get() );
1025 ++remainingTokens;
1026 if( !remainingTokens )
1027 return InternalParseResult::runtimeError( "Expected argument following " + token.token );
1028 auto const &argToken = *remainingTokens;
1029 if( argToken.type != TokenType::Argument )
1030 return InternalParseResult::runtimeError( "Expected argument following " + token.token );
1031 auto result = valueRef->setValue( argToken.token );
1032 if( !result )
1033 return InternalParseResult( result );
1034 if( result.value() == ParseResultType::ShortCircuitAll )
1035 return InternalParseResult::ok( ParseState( result.value(), remainingTokens ) );
1036 }
1037 return InternalParseResult::ok( ParseState( ParseResultType::Matched, ++remainingTokens ) );
1038 }
1039 }
1040 return InternalParseResult::ok( ParseState( ParseResultType::NoMatch, remainingTokens ) );
1041 }
static auto runtimeError(std::string const &message) -> BasicResult
Definition clara.hpp:595
static auto ok() -> BasicResult
Definition clara.hpp:593
auto validate() const -> Result override
Definition clara.hpp:1043
auto isMatch(std::string const &optToken) const -> bool
Definition clara.hpp:996
std::shared_ptr< BoundRef > m_ref
Definition clara.hpp:836
BasicResult< ParseState > InternalParseResult
Definition clara.hpp:647
Here is the call graph for this function:

◆ validate()

auto clara::detail::Opt::validate ( ) const -> Result
inlineoverridevirtual

Reimplemented from clara::detail::ParserBase.

Definition at line 1043 of file clara.hpp.

1043 {
1044 if( m_optNames.empty() )
1045 return Result::logicError( "No options supplied to Opt" );
1046 for( auto const &name : m_optNames ) {
1047 if( name.empty() )
1048 return Result::logicError( "Option name cannot be empty" );
1049#ifdef CLARA_PLATFORM_WINDOWS
1050 if( name[0] != '-' && name[0] != '/' )
1051 return Result::logicError( "Option name must begin with '-' or '/'" );
1052#else
1053 if( name[0] != '-' )
1054 return Result::logicError( "Option name must begin with '-'" );
1055#endif
1056 }
1057 return ParserRefImpl::validate();
1058 }
static auto logicError(std::string const &message) -> BasicResult
Definition clara.hpp:594
virtual auto validate() const -> Result
Definition clara.hpp:812
Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ m_optNames

std::vector<std::string> clara::detail::Opt::m_optNames
protected

Definition at line 962 of file clara.hpp.


The documentation for this class was generated from the following file: