MCPcopy Create free account
hub / github.com/ChaiScript/ChaiScript / Opt

Class Opt

unittests/catch.hpp:6152–6251  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

6150 }
6151
6152 class Opt : public ParserRefImpl<Opt> {
6153 protected:
6154 std::vector<std::string> m_optNames;
6155
6156 public:
6157 template<typename LambdaT>
6158 explicit Opt( LambdaT const &ref ) : ParserRefImpl( std::make_shared<BoundFlagLambda<LambdaT>>( ref ) ) {}
6159
6160 explicit Opt( bool &ref ) : ParserRefImpl( std::make_shared<BoundFlagRef>( ref ) ) {}
6161
6162 template<typename LambdaT>
6163 Opt( LambdaT const &ref, std::string const &hint ) : ParserRefImpl( ref, hint ) {}
6164
6165 template<typename T>
6166 Opt( T &ref, std::string const &hint ) : ParserRefImpl( ref, hint ) {}
6167
6168 auto operator[]( std::string const &optName ) -> Opt & {
6169 m_optNames.push_back( optName );
6170 return *this;
6171 }
6172
6173 auto getHelpColumns() const -> std::vector<HelpColumns> {
6174 std::ostringstream oss;
6175 bool first = true;
6176 for( auto const &opt : m_optNames ) {
6177 if (first)
6178 first = false;
6179 else
6180 oss << ", ";
6181 oss << opt;
6182 }
6183 if( !m_hint.empty() )
6184 oss << " <" << m_hint << ">";
6185 return { { oss.str(), m_description } };
6186 }
6187
6188 auto isMatch( std::string const &optToken ) const -> bool {
6189 auto normalisedToken = normaliseOpt( optToken );
6190 for( auto const &name : m_optNames ) {
6191 if( normaliseOpt( name ) == normalisedToken )
6192 return true;
6193 }
6194 return false;
6195 }
6196
6197 using ParserBase::parse;
6198
6199 auto parse( std::string const&, TokenStream const &tokens ) const -> InternalParseResult override {
6200 auto validationResult = validate();
6201 if( !validationResult )
6202 return InternalParseResult( validationResult );
6203
6204 auto remainingTokens = tokens;
6205 if( remainingTokens && remainingTokens->type == TokenType::Option ) {
6206 auto const &token = *remainingTokens;
6207 if( isMatch(token.token ) ) {
6208 if( m_ref->isFlag() ) {
6209 auto flagRef = static_cast<detail::BoundFlagRefBase*>( m_ref.get() );

Callers 1

makeCommandLineParserFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected