Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
clara::detail::Parser Struct Reference

#include <clara.hpp>

Inheritance diagram for clara::detail::Parser:
Collaboration diagram for clara::detail::Parser:

Public Member Functions

auto operator|= (ExeName const &exeName) -> Parser &
 
auto operator|= (Arg const &arg) -> Parser &
 
auto operator|= (Opt const &opt) -> Parser &
 
auto operator|= (Parser const &other) -> Parser &
 
template<typename T >
auto operator| (T const &other) const -> Parser
 
template<typename T >
auto operator+= (T const &other) -> Parser &
 
template<typename T >
auto operator+ (T const &other) const -> Parser
 
auto getHelpColumns () const -> std::vector< HelpColumns >
 
void writeToStream (std::ostream &os) const
 
auto validate () const -> Result override
 
auto parse (std::string const &exeName, TokenStream const &tokens) const -> InternalParseResult override
 
auto parse (Args const &args) const -> InternalParseResult
 
- Public Member Functions inherited from clara::detail::ParserBase
virtual ~ParserBase ()=default
 
virtual auto cardinality () const -> size_t
 
auto parse (Args const &args) const -> InternalParseResult
 

Public Attributes

ExeName m_exeName
 
std::vector< Optm_options
 
std::vector< Argm_args
 

Friends

auto operator<< (std::ostream &os, Parser const &parser) -> std::ostream &
 

Detailed Description

Definition at line 1076 of file clara.hpp.

Member Function Documentation

◆ getHelpColumns()

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

Definition at line 1114 of file clara.hpp.

1114 {
1115 std::vector<HelpColumns> cols;
1116 for (auto const &o : m_options) {
1117 auto childCols = o.getHelpColumns();
1118 cols.insert( cols.end(), childCols.begin(), childCols.end() );
1119 }
1120 return cols;
1121 }
std::vector< Opt > m_options
Definition clara.hpp:1079
Here is the caller graph for this function:

◆ operator+()

template<typename T >
auto clara::detail::Parser::operator+ ( T const & other) const -> Parser
inline

Definition at line 1112 of file clara.hpp.

1112{ return operator|( other ); }
auto operator|(T const &other) const -> Parser
Definition clara.hpp:1104
Here is the call graph for this function:

◆ operator+=()

template<typename T >
auto clara::detail::Parser::operator+= ( T const & other) -> Parser &
inline

Definition at line 1110 of file clara.hpp.

1110{ return operator|=( other ); }
auto operator|=(ExeName const &exeName) -> Parser &
Definition clara.hpp:1082
Here is the call graph for this function:

◆ operator|()

template<typename T >
auto clara::detail::Parser::operator| ( T const & other) const -> Parser
inline

Definition at line 1104 of file clara.hpp.

1104 {
1105 return Parser( *this ) |= other;
1106 }
Here is the caller graph for this function:

◆ operator|=() [1/4]

auto clara::detail::Parser::operator|= ( Arg const & arg) -> Parser &
inline

Definition at line 1087 of file clara.hpp.

1087 {
1088 m_args.push_back(arg);
1089 return *this;
1090 }
std::vector< Arg > m_args
Definition clara.hpp:1080

◆ operator|=() [2/4]

auto clara::detail::Parser::operator|= ( ExeName const & exeName) -> Parser &
inline

Definition at line 1082 of file clara.hpp.

1082 {
1083 m_exeName = exeName;
1084 return *this;
1085 }
Here is the caller graph for this function:

◆ operator|=() [3/4]

auto clara::detail::Parser::operator|= ( Opt const & opt) -> Parser &
inline

Definition at line 1092 of file clara.hpp.

1092 {
1093 m_options.push_back(opt);
1094 return *this;
1095 }

◆ operator|=() [4/4]

auto clara::detail::Parser::operator|= ( Parser const & other) -> Parser &
inline

Definition at line 1097 of file clara.hpp.

1097 {
1098 m_options.insert(m_options.end(), other.m_options.begin(), other.m_options.end());
1099 m_args.insert(m_args.end(), other.m_args.begin(), other.m_args.end());
1100 return *this;
1101 }

◆ 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 &exeName, TokenStream const &tokens) const -> InternalParseResult override
Definition clara.hpp:1185

◆ parse() [2/2]

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

Implements clara::detail::ParserBase.

Definition at line 1185 of file clara.hpp.

1185 {
1186
1187 struct ParserInfo {
1188 ParserBase const* parser = nullptr;
1189 size_t count = 0;
1190 };
1191 const size_t totalParsers = m_options.size() + m_args.size();
1192 assert( totalParsers < 512 );
1193 // ParserInfo parseInfos[totalParsers]; // <-- this is what we really want to do
1194 ParserInfo parseInfos[512];
1195
1196 {
1197 size_t i = 0;
1198 for (auto const &opt : m_options) parseInfos[i++].parser = &opt;
1199 for (auto const &arg : m_args) parseInfos[i++].parser = &arg;
1200 }
1201
1202 m_exeName.set( exeName );
1203
1204 auto result = InternalParseResult::ok( ParseState( ParseResultType::NoMatch, tokens ) );
1205 while( result.value().remainingTokens() ) {
1206 bool tokenParsed = false;
1207
1208 for( size_t i = 0; i < totalParsers; ++i ) {
1209 auto& parseInfo = parseInfos[i];
1210 if( parseInfo.parser->cardinality() == 0 || parseInfo.count < parseInfo.parser->cardinality() ) {
1211 result = parseInfo.parser->parse(exeName, result.value().remainingTokens());
1212 if (!result)
1213 return result;
1214 if (result.value().type() != ParseResultType::NoMatch) {
1215 tokenParsed = true;
1216 ++parseInfo.count;
1217 break;
1218 }
1219 }
1220 }
1221
1222 if( result.value().type() == ParseResultType::ShortCircuitAll )
1223 return result;
1224 if( !tokenParsed )
1225 return InternalParseResult::runtimeError( "Unrecognised token: " + result.value().remainingTokens()->token );
1226 }
1227 // !TBD Check missing required options
1228 return result;
1229 }
static auto runtimeError(std::string const &message) -> BasicResult
Definition clara.hpp:595
static auto ok() -> BasicResult
Definition clara.hpp:593
auto set(std::string const &newName) -> ParserResult
Definition clara.hpp:911
int * count
Here is the call graph for this function:

◆ validate()

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

Reimplemented from clara::detail::ParserBase.

Definition at line 1169 of file clara.hpp.

1169 {
1170 for( auto const &opt : m_options ) {
1171 auto result = opt.validate();
1172 if( !result )
1173 return result;
1174 }
1175 for( auto const &arg : m_args ) {
1176 auto result = arg.validate();
1177 if( !result )
1178 return result;
1179 }
1180 return Result::ok();
1181 }
Here is the call graph for this function:

◆ writeToStream()

void clara::detail::Parser::writeToStream ( std::ostream & os) const
inline

Definition at line 1123 of file clara.hpp.

1123 {
1124 if (!m_exeName.name().empty()) {
1125 os << "usage:\n" << " " << m_exeName.name() << " ";
1126 bool required = true, first = true;
1127 for( auto const &arg : m_args ) {
1128 if (first)
1129 first = false;
1130 else
1131 os << " ";
1132 if( arg.isOptional() && required ) {
1133 os << "[";
1134 required = false;
1135 }
1136 os << "<" << arg.hint() << ">";
1137 if( arg.cardinality() == 0 )
1138 os << " ... ";
1139 }
1140 if( !required )
1141 os << "]";
1142 if( !m_options.empty() )
1143 os << " options";
1144 os << "\n\nwhere options are:" << std::endl;
1145 }
1146
1147 auto rows = getHelpColumns();
1148 size_t consoleWidth = CLARA_CONFIG_CONSOLE_WIDTH;
1149 size_t optWidth = 0;
1150 for( auto const &cols : rows )
1151 optWidth = (std::max)(optWidth, cols.left.size() + 2);
1152
1153 optWidth = (std::min)(optWidth, consoleWidth/2);
1154
1155 for( auto const &cols : rows ) {
1156 auto row =
1157 TextFlow::Column( cols.left ).width( optWidth ).indent( 2 ) +
1158 TextFlow::Spacer(4) +
1159 TextFlow::Column( cols.right ).width( consoleWidth - 7 - optWidth );
1160 os << row << std::endl;
1161 }
1162 }
auto name() const -> std::string
Definition clara.hpp:910
os_t os
auto getHelpColumns() const -> std::vector< HelpColumns >
Definition clara.hpp:1114
#define CLARA_CONFIG_CONSOLE_WIDTH
Definition clara.hpp:14
Here is the call graph for this function:

Friends And Related Symbol Documentation

◆ operator<<

auto operator<< ( std::ostream & os,
Parser const & parser ) -> std::ostream&
friend

Definition at line 1164 of file clara.hpp.

1164 {
1165 parser.writeToStream( os );
1166 return os;
1167 }

Member Data Documentation

◆ m_args

std::vector<Arg> clara::detail::Parser::m_args

Definition at line 1080 of file clara.hpp.

◆ m_exeName

ExeName clara::detail::Parser::m_exeName
mutable

Definition at line 1078 of file clara.hpp.

◆ m_options

std::vector<Opt> clara::detail::Parser::m_options

Definition at line 1079 of file clara.hpp.


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