2017-06-13 00:04:24 +02:00
|
|
|
// v1.0
|
|
|
|
// See https://github.com/philsquared/Clara
|
|
|
|
|
|
|
|
#ifndef CATCH_CLARA_HPP_INCLUDED
|
|
|
|
#define CATCH_CLARA_HPP_INCLUDED
|
|
|
|
|
2017-06-20 19:03:37 +02:00
|
|
|
#ifndef CATCH_CLARA_CONFIG_CONSOLE_WIDTH
|
|
|
|
#define CATCH_CLARA_CONFIG_CONSOLE_WIDTH 80
|
|
|
|
#endif
|
|
|
|
|
2017-09-26 00:12:00 +02:00
|
|
|
#ifndef CATCH_CLARA_TEXTFLOW_CONFIG_CONSOLE_WIDTH
|
|
|
|
#define CATCH_CLARA_TEXTFLOW_CONFIG_CONSOLE_WIDTH CATCH_CLARA_CONFIG_CONSOLE_WIDTH
|
|
|
|
#endif
|
2017-06-20 19:03:37 +02:00
|
|
|
|
|
|
|
// ----------- #included from clara_textflow.hpp -----------
|
|
|
|
|
|
|
|
// TextFlowCpp
|
|
|
|
//
|
|
|
|
// A single-header library for wrapping and laying out basic text, by Phil Nash
|
|
|
|
//
|
|
|
|
// This work is licensed under the BSD 2-Clause license.
|
|
|
|
// See the accompanying LICENSE file, or the one at https://opensource.org/licenses/BSD-2-Clause
|
|
|
|
//
|
|
|
|
// This project is hosted at https://github.com/philsquared/textflowcpp
|
|
|
|
|
|
|
|
#ifndef CATCH_CLARA_TEXTFLOW_HPP_INCLUDED
|
|
|
|
#define CATCH_CLARA_TEXTFLOW_HPP_INCLUDED
|
|
|
|
|
2017-06-13 00:04:24 +02:00
|
|
|
#include <cassert>
|
|
|
|
#include <ostream>
|
|
|
|
#include <sstream>
|
|
|
|
#include <vector>
|
|
|
|
|
2017-06-20 19:03:37 +02:00
|
|
|
#ifndef CATCH_CLARA_TEXTFLOW_CONFIG_CONSOLE_WIDTH
|
2017-06-22 17:07:05 +02:00
|
|
|
#define CATCH_CLARA_TEXTFLOW_CONFIG_CONSOLE_WIDTH 80
|
2017-06-13 00:04:24 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2017-07-20 20:42:06 +02:00
|
|
|
namespace Catch { namespace clara { namespace TextFlow {
|
2017-06-13 00:04:24 +02:00
|
|
|
|
|
|
|
inline auto isWhitespace( char c ) -> bool {
|
|
|
|
static std::string chars = " \t\n\r";
|
|
|
|
return chars.find( c ) != std::string::npos;
|
|
|
|
}
|
|
|
|
inline auto isBreakableBefore( char c ) -> bool {
|
|
|
|
static std::string chars = "[({<|";
|
|
|
|
return chars.find( c ) != std::string::npos;
|
|
|
|
}
|
|
|
|
inline auto isBreakableAfter( char c ) -> bool {
|
|
|
|
static std::string chars = "])}>.,:;*+-=&/\\";
|
|
|
|
return chars.find( c ) != std::string::npos;
|
|
|
|
}
|
|
|
|
|
|
|
|
class Columns;
|
|
|
|
|
|
|
|
class Column {
|
|
|
|
std::vector<std::string> m_strings;
|
2017-06-20 19:03:37 +02:00
|
|
|
size_t m_width = CATCH_CLARA_TEXTFLOW_CONFIG_CONSOLE_WIDTH;
|
2017-06-13 00:04:24 +02:00
|
|
|
size_t m_indent = 0;
|
|
|
|
size_t m_initialIndent = std::string::npos;
|
|
|
|
|
|
|
|
public:
|
|
|
|
class iterator {
|
|
|
|
friend Column;
|
|
|
|
|
|
|
|
Column const& m_column;
|
|
|
|
size_t m_stringIndex = 0;
|
|
|
|
size_t m_pos = 0;
|
|
|
|
|
2017-06-20 19:03:37 +02:00
|
|
|
size_t m_len = 0;
|
2017-07-20 20:42:06 +02:00
|
|
|
size_t m_end = 0;
|
2017-06-13 00:04:24 +02:00
|
|
|
bool m_suffix = false;
|
|
|
|
|
|
|
|
iterator( Column const& column, size_t stringIndex )
|
2017-06-20 19:03:37 +02:00
|
|
|
: m_column( column ),
|
|
|
|
m_stringIndex( stringIndex )
|
2017-06-13 00:04:24 +02:00
|
|
|
{}
|
|
|
|
|
|
|
|
auto line() const -> std::string const& { return m_column.m_strings[m_stringIndex]; }
|
|
|
|
|
|
|
|
auto isBoundary( size_t at ) const -> bool {
|
|
|
|
assert( at > 0 );
|
|
|
|
assert( at <= line().size() );
|
|
|
|
|
|
|
|
return at == line().size() ||
|
|
|
|
( isWhitespace( line()[at] ) && !isWhitespace( line()[at-1] ) ) ||
|
|
|
|
isBreakableBefore( line()[at] ) ||
|
|
|
|
isBreakableAfter( line()[at-1] );
|
|
|
|
}
|
|
|
|
|
|
|
|
void calcLength() {
|
|
|
|
assert( m_stringIndex < m_column.m_strings.size() );
|
|
|
|
|
|
|
|
m_suffix = false;
|
|
|
|
auto width = m_column.m_width-indent();
|
2017-07-20 20:42:06 +02:00
|
|
|
m_end = m_pos;
|
|
|
|
while( m_end < line().size() && line()[m_end] != '\n' )
|
|
|
|
++m_end;
|
|
|
|
|
|
|
|
if( m_end < m_pos + width ) {
|
|
|
|
m_len = m_end - m_pos;
|
2017-06-13 00:04:24 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
size_t len = width;
|
|
|
|
while (len > 0 && !isBoundary(m_pos + len))
|
|
|
|
--len;
|
|
|
|
while (len > 0 && isWhitespace( line()[m_pos + len - 1] ))
|
|
|
|
--len;
|
|
|
|
|
|
|
|
if (len > 0) {
|
|
|
|
m_len = len;
|
|
|
|
} else {
|
|
|
|
m_suffix = true;
|
|
|
|
m_len = width - 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
auto indent() const -> size_t {
|
|
|
|
auto initial = m_pos == 0 && m_stringIndex == 0 ? m_column.m_initialIndent : std::string::npos;
|
|
|
|
return initial == std::string::npos ? m_column.m_indent : initial;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto addIndentAndSuffix(std::string const &plain) const -> std::string {
|
|
|
|
return std::string( indent(), ' ' ) + (m_suffix ? plain + "-" : plain);
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit iterator( Column const& column ) : m_column( column ) {
|
|
|
|
assert( m_column.m_width > m_column.m_indent );
|
|
|
|
assert( m_column.m_initialIndent == std::string::npos || m_column.m_width > m_column.m_initialIndent );
|
|
|
|
calcLength();
|
|
|
|
if( m_len == 0 )
|
|
|
|
m_stringIndex++; // Empty string
|
|
|
|
}
|
|
|
|
|
|
|
|
auto operator *() const -> std::string {
|
|
|
|
assert( m_stringIndex < m_column.m_strings.size() );
|
2017-08-31 17:14:27 +02:00
|
|
|
assert( m_pos <= m_end );
|
2017-07-20 20:42:06 +02:00
|
|
|
if( m_pos + m_column.m_width < m_end )
|
2017-06-13 00:04:24 +02:00
|
|
|
return addIndentAndSuffix(line().substr(m_pos, m_len));
|
|
|
|
else
|
2017-07-20 20:42:06 +02:00
|
|
|
return addIndentAndSuffix(line().substr(m_pos, m_end - m_pos));
|
2017-06-13 00:04:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
auto operator ++() -> iterator& {
|
|
|
|
m_pos += m_len;
|
2017-07-20 20:42:06 +02:00
|
|
|
if( m_pos < line().size() && line()[m_pos] == '\n' )
|
|
|
|
m_pos += 1;
|
2017-07-20 21:07:28 +02:00
|
|
|
else
|
|
|
|
while( m_pos < line().size() && isWhitespace( line()[m_pos] ) )
|
|
|
|
++m_pos;
|
2017-06-13 00:04:24 +02:00
|
|
|
|
|
|
|
if( m_pos == line().size() ) {
|
|
|
|
m_pos = 0;
|
|
|
|
++m_stringIndex;
|
|
|
|
}
|
|
|
|
if( m_stringIndex < m_column.m_strings.size() )
|
|
|
|
calcLength();
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
auto operator ++(int) -> iterator {
|
|
|
|
iterator prev( *this );
|
|
|
|
operator++();
|
|
|
|
return prev;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto operator ==( iterator const& other ) const -> bool {
|
|
|
|
return
|
2017-06-20 19:03:37 +02:00
|
|
|
m_pos == other.m_pos &&
|
|
|
|
m_stringIndex == other.m_stringIndex &&
|
|
|
|
&m_column == &other.m_column;
|
2017-06-13 00:04:24 +02:00
|
|
|
}
|
|
|
|
auto operator !=( iterator const& other ) const -> bool {
|
|
|
|
return !operator==( other );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
using const_iterator = iterator;
|
|
|
|
|
|
|
|
explicit Column( std::string const& text ) { m_strings.push_back( text ); }
|
|
|
|
|
|
|
|
auto width( size_t newWidth ) -> Column& {
|
|
|
|
assert( newWidth > 0 );
|
|
|
|
m_width = newWidth;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
auto indent( size_t newIndent ) -> Column& {
|
|
|
|
m_indent = newIndent;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
auto initialIndent( size_t newIndent ) -> Column& {
|
|
|
|
m_initialIndent = newIndent;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto width() const -> size_t { return m_width; }
|
|
|
|
auto begin() const -> iterator { return iterator( *this ); }
|
2017-06-20 19:03:37 +02:00
|
|
|
auto end() const -> iterator { return { *this, m_strings.size() }; }
|
2017-06-13 00:04:24 +02:00
|
|
|
|
|
|
|
inline friend std::ostream& operator << ( std::ostream& os, Column const& col ) {
|
|
|
|
bool first = true;
|
|
|
|
for( auto line : col ) {
|
|
|
|
if( first )
|
|
|
|
first = false;
|
|
|
|
else
|
|
|
|
os << "\n";
|
|
|
|
os << line;
|
|
|
|
}
|
|
|
|
return os;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto operator + ( Column const& other ) -> Columns;
|
|
|
|
|
|
|
|
auto toString() const -> std::string {
|
|
|
|
std::ostringstream oss;
|
|
|
|
oss << *this;
|
|
|
|
return oss.str();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class Spacer : public Column {
|
|
|
|
|
|
|
|
public:
|
2017-06-20 19:03:37 +02:00
|
|
|
explicit Spacer( size_t spaceWidth ) : Column( "" ) {
|
2017-06-13 00:04:24 +02:00
|
|
|
width( spaceWidth );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class Columns {
|
|
|
|
std::vector<Column> m_columns;
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
class iterator {
|
|
|
|
friend Columns;
|
|
|
|
struct EndTag {};
|
|
|
|
|
|
|
|
std::vector<Column> const& m_columns;
|
|
|
|
std::vector<Column::iterator> m_iterators;
|
|
|
|
size_t m_activeIterators;
|
|
|
|
|
|
|
|
iterator( Columns const& columns, EndTag )
|
2017-06-20 19:03:37 +02:00
|
|
|
: m_columns( columns.m_columns ),
|
|
|
|
m_activeIterators( 0 )
|
2017-06-13 00:04:24 +02:00
|
|
|
{
|
|
|
|
m_iterators.reserve( m_columns.size() );
|
|
|
|
|
|
|
|
for( auto const& col : m_columns )
|
|
|
|
m_iterators.push_back( col.end() );
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
2017-06-20 19:03:37 +02:00
|
|
|
explicit iterator( Columns const& columns )
|
|
|
|
: m_columns( columns.m_columns ),
|
|
|
|
m_activeIterators( m_columns.size() )
|
2017-06-13 00:04:24 +02:00
|
|
|
{
|
|
|
|
m_iterators.reserve( m_columns.size() );
|
|
|
|
|
|
|
|
for( auto const& col : m_columns )
|
|
|
|
m_iterators.push_back( col.begin() );
|
|
|
|
}
|
|
|
|
|
|
|
|
auto operator ==( iterator const& other ) const -> bool {
|
|
|
|
return m_iterators == other.m_iterators;
|
|
|
|
}
|
|
|
|
auto operator !=( iterator const& other ) const -> bool {
|
|
|
|
return m_iterators != other.m_iterators;
|
|
|
|
}
|
|
|
|
auto operator *() const -> std::string {
|
|
|
|
std::string row, padding;
|
|
|
|
|
|
|
|
for( size_t i = 0; i < m_columns.size(); ++i ) {
|
|
|
|
auto width = m_columns[i].width();
|
|
|
|
if( m_iterators[i] != m_columns[i].end() ) {
|
|
|
|
std::string col = *m_iterators[i];
|
|
|
|
row += padding + col;
|
|
|
|
if( col.size() < width )
|
|
|
|
padding = std::string( width - col.size(), ' ' );
|
|
|
|
else
|
|
|
|
padding = "";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
padding += std::string( width, ' ' );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return row;
|
|
|
|
}
|
|
|
|
auto operator ++() -> iterator& {
|
|
|
|
for( size_t i = 0; i < m_columns.size(); ++i ) {
|
|
|
|
if (m_iterators[i] != m_columns[i].end())
|
|
|
|
++m_iterators[i];
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
auto operator ++(int) -> iterator {
|
|
|
|
iterator prev( *this );
|
|
|
|
operator++();
|
|
|
|
return prev;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
using const_iterator = iterator;
|
|
|
|
|
|
|
|
auto begin() const -> iterator { return iterator( *this ); }
|
2017-06-20 19:03:37 +02:00
|
|
|
auto end() const -> iterator { return { *this, iterator::EndTag() }; }
|
2017-06-13 00:04:24 +02:00
|
|
|
|
|
|
|
auto operator += ( Column const& col ) -> Columns& {
|
|
|
|
m_columns.push_back( col );
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
auto operator + ( Column const& col ) -> Columns {
|
|
|
|
Columns combined = *this;
|
|
|
|
combined += col;
|
|
|
|
return combined;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline friend std::ostream& operator << ( std::ostream& os, Columns const& cols ) {
|
|
|
|
|
|
|
|
bool first = true;
|
|
|
|
for( auto line : cols ) {
|
|
|
|
if( first )
|
|
|
|
first = false;
|
|
|
|
else
|
|
|
|
os << "\n";
|
|
|
|
os << line;
|
|
|
|
}
|
|
|
|
return os;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto toString() const -> std::string {
|
|
|
|
std::ostringstream oss;
|
|
|
|
oss << *this;
|
|
|
|
return oss.str();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
inline auto Column::operator + ( Column const& other ) -> Columns {
|
|
|
|
Columns cols;
|
|
|
|
cols += *this;
|
|
|
|
cols += other;
|
|
|
|
return cols;
|
|
|
|
}
|
2017-06-20 19:03:37 +02:00
|
|
|
}}} // namespace Catch::clara::TextFlow
|
|
|
|
|
|
|
|
#endif // CATCH_CLARA_TEXTFLOW_HPP_INCLUDED
|
|
|
|
|
|
|
|
// ----------- end of #include from clara_textflow.hpp -----------
|
|
|
|
// ........... back in clara.hpp
|
2017-06-13 00:04:24 +02:00
|
|
|
|
|
|
|
|
2017-06-20 19:03:37 +02:00
|
|
|
#include <memory>
|
|
|
|
#include <set>
|
|
|
|
#include <algorithm>
|
|
|
|
|
2017-09-26 00:12:00 +02:00
|
|
|
#if !defined(CATCH_PLATFORM_WINDOWS) && ( defined(WIN32) || defined(__WIN32__) || defined(_WIN32) || defined(_MSC_VER) )
|
|
|
|
#define CATCH_PLATFORM_WINDOWS
|
2017-06-23 10:08:44 +02:00
|
|
|
#endif
|
|
|
|
|
2017-06-20 19:03:37 +02:00
|
|
|
namespace Catch { namespace clara {
|
2017-06-13 00:04:24 +02:00
|
|
|
namespace detail {
|
|
|
|
|
|
|
|
// Traits for extracting arg and return type of lambdas (for single argument lambdas)
|
|
|
|
template<typename L>
|
2017-09-26 00:12:00 +02:00
|
|
|
struct UnaryLambdaTraits : UnaryLambdaTraits<decltype( &L::operator() )> {};
|
2017-06-13 00:04:24 +02:00
|
|
|
|
|
|
|
template<typename ClassT, typename ReturnT, typename... Args>
|
2017-09-26 00:12:00 +02:00
|
|
|
struct UnaryLambdaTraits<ReturnT( ClassT::* )( Args... ) const> {
|
2017-06-13 00:04:24 +02:00
|
|
|
static const bool isValid = false;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename ClassT, typename ReturnT, typename ArgT>
|
2017-09-26 00:12:00 +02:00
|
|
|
struct UnaryLambdaTraits<ReturnT( ClassT::* )( ArgT ) const> {
|
2017-06-13 00:04:24 +02:00
|
|
|
static const bool isValid = true;
|
|
|
|
using ArgType = typename std::remove_const<typename std::remove_reference<ArgT>::type>::type;;
|
|
|
|
using ReturnType = ReturnT;
|
|
|
|
};
|
|
|
|
|
|
|
|
class TokenStream;
|
|
|
|
|
|
|
|
// Transport for raw args (copied from main args, or supplied via init list for testing)
|
|
|
|
class Args {
|
|
|
|
friend TokenStream;
|
|
|
|
std::string m_exeName;
|
|
|
|
std::vector<std::string> m_args;
|
|
|
|
|
|
|
|
public:
|
2017-09-26 00:12:00 +02:00
|
|
|
Args( int argc, char *argv[] ) {
|
2017-06-13 00:04:24 +02:00
|
|
|
m_exeName = argv[0];
|
2017-09-26 00:12:00 +02:00
|
|
|
for( int i = 1; i < argc; ++i )
|
|
|
|
m_args.push_back( argv[i] );
|
2017-06-13 00:04:24 +02:00
|
|
|
}
|
|
|
|
|
2017-09-26 00:12:00 +02:00
|
|
|
Args( std::initializer_list<std::string> args )
|
2017-06-13 00:04:24 +02:00
|
|
|
: m_exeName( *args.begin() ),
|
|
|
|
m_args( args.begin()+1, args.end() )
|
|
|
|
{}
|
|
|
|
|
|
|
|
auto exeName() const -> std::string {
|
|
|
|
return m_exeName;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Wraps a token coming from a token stream. These may not directly correspond to strings as a single string
|
|
|
|
// may encode an option + its argument if the : or = form is used
|
|
|
|
enum class TokenType {
|
|
|
|
Option, Argument
|
|
|
|
};
|
|
|
|
struct Token {
|
|
|
|
TokenType type;
|
|
|
|
std::string token;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Abstracts iterators into args as a stream of tokens, with option arguments uniformly handled
|
|
|
|
class TokenStream {
|
|
|
|
using Iterator = std::vector<std::string>::const_iterator;
|
|
|
|
Iterator it;
|
|
|
|
Iterator itEnd;
|
|
|
|
std::vector<Token> m_tokenBuffer;
|
|
|
|
|
|
|
|
void loadBuffer() {
|
2017-09-26 00:12:00 +02:00
|
|
|
m_tokenBuffer.resize( 0 );
|
2017-06-13 00:04:24 +02:00
|
|
|
|
|
|
|
// Skip any empty strings
|
2017-09-26 00:12:00 +02:00
|
|
|
while( it != itEnd && it->empty() )
|
2017-06-13 00:04:24 +02:00
|
|
|
++it;
|
|
|
|
|
2017-09-26 00:12:00 +02:00
|
|
|
if( it != itEnd ) {
|
2017-06-13 00:04:24 +02:00
|
|
|
auto const &next = *it;
|
2017-09-26 00:12:00 +02:00
|
|
|
if( next[0] == '-' || next[0] == '/' ) {
|
|
|
|
auto delimiterPos = next.find_first_of( " :=" );
|
|
|
|
if( delimiterPos != std::string::npos ) {
|
|
|
|
m_tokenBuffer.push_back( { TokenType::Option, next.substr( 0, delimiterPos ) } );
|
|
|
|
m_tokenBuffer.push_back( { TokenType::Argument, next.substr( delimiterPos + 1 ) } );
|
2017-06-13 00:04:24 +02:00
|
|
|
} else {
|
2017-09-26 00:12:00 +02:00
|
|
|
if( next[1] != '-' && next.size() > 2 ) {
|
2017-06-13 00:04:24 +02:00
|
|
|
std::string opt = "- ";
|
2017-09-26 00:12:00 +02:00
|
|
|
for( size_t i = 1; i < next.size(); ++i ) {
|
2017-06-13 00:04:24 +02:00
|
|
|
opt[1] = next[i];
|
2017-09-26 00:12:00 +02:00
|
|
|
m_tokenBuffer.push_back( { TokenType::Option, opt } );
|
2017-06-13 00:04:24 +02:00
|
|
|
}
|
|
|
|
} else {
|
2017-09-26 00:12:00 +02:00
|
|
|
m_tokenBuffer.push_back( { TokenType::Option, next } );
|
2017-06-13 00:04:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2017-09-26 00:12:00 +02:00
|
|
|
m_tokenBuffer.push_back( { TokenType::Argument, next } );
|
2017-06-13 00:04:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
2017-09-26 00:12:00 +02:00
|
|
|
explicit TokenStream( Args const &args ) : TokenStream( args.m_args.begin(), args.m_args.end() ) {}
|
2017-06-13 00:04:24 +02:00
|
|
|
|
2017-09-26 00:12:00 +02:00
|
|
|
TokenStream( Iterator it, Iterator itEnd ) : it( it ), itEnd( itEnd ) {
|
2017-06-13 00:04:24 +02:00
|
|
|
loadBuffer();
|
|
|
|
}
|
|
|
|
|
|
|
|
explicit operator bool() const {
|
|
|
|
return !m_tokenBuffer.empty() || it != itEnd;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto count() const -> size_t { return m_tokenBuffer.size() + (itEnd - it); }
|
|
|
|
|
|
|
|
auto operator*() const -> Token {
|
2017-09-26 00:12:00 +02:00
|
|
|
assert( !m_tokenBuffer.empty() );
|
2017-06-13 00:04:24 +02:00
|
|
|
return m_tokenBuffer.front();
|
|
|
|
}
|
|
|
|
|
|
|
|
auto operator->() const -> Token const * {
|
2017-09-26 00:12:00 +02:00
|
|
|
assert( !m_tokenBuffer.empty() );
|
2017-06-13 00:04:24 +02:00
|
|
|
return &m_tokenBuffer.front();
|
|
|
|
}
|
|
|
|
|
|
|
|
auto operator++() -> TokenStream & {
|
2017-09-26 00:12:00 +02:00
|
|
|
if( m_tokenBuffer.size() >= 2 ) {
|
|
|
|
m_tokenBuffer.erase( m_tokenBuffer.begin() );
|
2017-06-13 00:04:24 +02:00
|
|
|
} else {
|
2017-09-26 00:12:00 +02:00
|
|
|
if( it != itEnd )
|
2017-06-13 00:04:24 +02:00
|
|
|
++it;
|
|
|
|
loadBuffer();
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class ResultBase {
|
|
|
|
public:
|
|
|
|
enum Type {
|
|
|
|
Ok, LogicError, RuntimeError
|
|
|
|
};
|
|
|
|
|
|
|
|
protected:
|
2017-09-26 00:12:00 +02:00
|
|
|
ResultBase( Type type ) : m_type( type ) {}
|
2017-06-20 23:35:35 +02:00
|
|
|
virtual ~ResultBase() = default;
|
2017-06-13 00:04:24 +02:00
|
|
|
|
|
|
|
virtual void enforceOk() const = 0;
|
|
|
|
|
|
|
|
Type m_type;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
class ResultValueBase : public ResultBase {
|
|
|
|
public:
|
|
|
|
auto value() const -> T const & {
|
|
|
|
enforceOk();
|
|
|
|
return m_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2017-09-26 00:12:00 +02:00
|
|
|
ResultValueBase( Type type ) : ResultBase( type ) {}
|
2017-06-13 00:04:24 +02:00
|
|
|
|
2017-09-26 00:12:00 +02:00
|
|
|
ResultValueBase( ResultValueBase const &other ) : ResultBase( other ) {
|
|
|
|
if( m_type == ResultBase::Ok )
|
|
|
|
new( &m_value ) T( other.m_value );
|
2017-06-13 00:04:24 +02:00
|
|
|
}
|
|
|
|
|
2017-09-26 00:12:00 +02:00
|
|
|
ResultValueBase( Type, T const &value ) : ResultBase( Ok ) {
|
|
|
|
new( &m_value ) T( value );
|
2017-06-13 00:04:24 +02:00
|
|
|
}
|
|
|
|
|
2017-09-26 00:12:00 +02:00
|
|
|
auto operator=( ResultValueBase const &other ) -> ResultValueBase & {
|
|
|
|
if( m_type == ResultBase::Ok )
|
2017-06-13 00:04:24 +02:00
|
|
|
m_value.~T();
|
|
|
|
ResultBase::operator=(other);
|
2017-09-26 00:12:00 +02:00
|
|
|
if( m_type == ResultBase::Ok )
|
|
|
|
new( &m_value ) T( other.m_value );
|
2017-06-13 00:04:24 +02:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
~ResultValueBase() {
|
2017-09-26 00:12:00 +02:00
|
|
|
if( m_type == Ok )
|
2017-06-13 00:04:24 +02:00
|
|
|
m_value.~T();
|
|
|
|
}
|
|
|
|
|
|
|
|
union {
|
|
|
|
T m_value;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
|
|
|
class ResultValueBase<void> : public ResultBase {
|
|
|
|
protected:
|
|
|
|
using ResultBase::ResultBase;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T = void>
|
|
|
|
class BasicResult : public ResultValueBase<T> {
|
|
|
|
public:
|
|
|
|
template<typename U>
|
2017-09-26 00:12:00 +02:00
|
|
|
explicit BasicResult( BasicResult<U> const &other )
|
|
|
|
: ResultValueBase<T>( other.type() ),
|
|
|
|
m_errorMessage( other.errorMessage() )
|
|
|
|
{
|
|
|
|
assert( type() != ResultBase::Ok );
|
2017-06-13 00:04:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename U>
|
2017-09-26 00:12:00 +02:00
|
|
|
static auto ok( U const &value ) -> BasicResult { return { ResultBase::Ok, value }; }
|
|
|
|
static auto ok() -> BasicResult { return { ResultBase::Ok }; }
|
|
|
|
static auto logicError( std::string const &message ) -> BasicResult { return { ResultBase::LogicError, message }; }
|
|
|
|
static auto runtimeError( std::string const &message ) -> BasicResult { return { ResultBase::RuntimeError, message }; }
|
2017-06-13 00:04:24 +02:00
|
|
|
|
|
|
|
explicit operator bool() const { return m_type == ResultBase::Ok; }
|
|
|
|
auto type() const -> ResultBase::Type { return m_type; }
|
|
|
|
auto errorMessage() const -> std::string { return m_errorMessage; }
|
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual void enforceOk() const {
|
|
|
|
// !TBD: If no exceptions, std::terminate here or something
|
2017-09-26 00:12:00 +02:00
|
|
|
switch( m_type ) {
|
2017-06-13 00:04:24 +02:00
|
|
|
case ResultBase::LogicError:
|
2017-09-26 00:12:00 +02:00
|
|
|
throw std::logic_error( m_errorMessage );
|
2017-06-13 00:04:24 +02:00
|
|
|
case ResultBase::RuntimeError:
|
2017-09-26 00:12:00 +02:00
|
|
|
throw std::runtime_error( m_errorMessage );
|
2017-06-13 00:04:24 +02:00
|
|
|
case ResultBase::Ok:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string m_errorMessage; // Only populated if resultType is an error
|
|
|
|
|
2017-09-26 00:12:00 +02:00
|
|
|
BasicResult( ResultBase::Type type, std::string const &message )
|
|
|
|
: ResultValueBase<T>(type),
|
|
|
|
m_errorMessage(message)
|
|
|
|
{
|
|
|
|
assert( m_type != ResultBase::Ok );
|
2017-06-13 00:04:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
using ResultValueBase<T>::ResultValueBase;
|
|
|
|
using ResultBase::m_type;
|
|
|
|
};
|
|
|
|
|
|
|
|
enum class ParseResultType {
|
|
|
|
Matched, NoMatch, ShortCircuitAll, ShortCircuitSame
|
|
|
|
};
|
|
|
|
|
|
|
|
class ParseState {
|
|
|
|
public:
|
|
|
|
|
2017-09-26 00:12:00 +02:00
|
|
|
ParseState( ParseResultType type, TokenStream const &remainingTokens )
|
|
|
|
: m_type(type),
|
|
|
|
m_remainingTokens( remainingTokens )
|
|
|
|
{}
|
2017-06-13 00:04:24 +02:00
|
|
|
|
|
|
|
auto type() const -> ParseResultType { return m_type; }
|
|
|
|
auto remainingTokens() const -> TokenStream { return m_remainingTokens; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
ParseResultType m_type;
|
|
|
|
TokenStream m_remainingTokens;
|
|
|
|
};
|
|
|
|
|
|
|
|
using Result = BasicResult<void>;
|
|
|
|
using ParserResult = BasicResult<ParseResultType>;
|
|
|
|
using InternalParseResult = BasicResult<ParseState>;
|
|
|
|
|
|
|
|
struct HelpColumns {
|
|
|
|
std::string left;
|
|
|
|
std::string right;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
2017-09-26 00:12:00 +02:00
|
|
|
inline auto convertInto( std::string const &source, T& target ) -> ParserResult {
|
2017-06-13 00:04:24 +02:00
|
|
|
std::stringstream ss;
|
|
|
|
ss << source;
|
|
|
|
ss >> target;
|
2017-09-26 00:12:00 +02:00
|
|
|
if( ss.fail() )
|
|
|
|
return ParserResult::runtimeError( "Unable to convert '" + source + "' to destination type" );
|
2017-06-13 00:04:24 +02:00
|
|
|
else
|
2017-09-26 00:12:00 +02:00
|
|
|
return ParserResult::ok( ParseResultType::Matched );
|
2017-06-13 00:04:24 +02:00
|
|
|
}
|
2017-09-26 00:12:00 +02:00
|
|
|
inline auto convertInto( std::string const &source, std::string& target ) -> ParserResult {
|
2017-06-13 00:04:24 +02:00
|
|
|
target = source;
|
2017-09-26 00:12:00 +02:00
|
|
|
return ParserResult::ok( ParseResultType::Matched );
|
2017-06-13 00:04:24 +02:00
|
|
|
}
|
2017-09-26 00:12:00 +02:00
|
|
|
inline auto convertInto( std::string const &source, bool &target ) -> ParserResult {
|
2017-06-13 00:04:24 +02:00
|
|
|
std::string srcLC = source;
|
2017-09-26 00:12:00 +02:00
|
|
|
std::transform( srcLC.begin(), srcLC.end(), srcLC.begin(), []( char c ) { return static_cast<char>( ::tolower(c) ); } );
|
2017-06-13 00:04:24 +02:00
|
|
|
if (srcLC == "y" || srcLC == "1" || srcLC == "true" || srcLC == "yes" || srcLC == "on")
|
|
|
|
target = true;
|
|
|
|
else if (srcLC == "n" || srcLC == "0" || srcLC == "false" || srcLC == "no" || srcLC == "off")
|
|
|
|
target = false;
|
|
|
|
else
|
2017-09-26 00:12:00 +02:00
|
|
|
return ParserResult::runtimeError( "Expected a boolean value but did not recognise: '" + source + "'" );
|
|
|
|
return ParserResult::ok( ParseResultType::Matched );
|
2017-06-13 00:04:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
struct BoundRefBase {
|
|
|
|
BoundRefBase() = default;
|
2017-09-26 00:12:00 +02:00
|
|
|
BoundRefBase( BoundRefBase const & ) = delete;
|
|
|
|
BoundRefBase( BoundRefBase && ) = delete;
|
|
|
|
BoundRefBase &operator=( BoundRefBase const & ) = delete;
|
|
|
|
BoundRefBase &operator=( BoundRefBase && ) = delete;
|
2017-06-13 00:04:24 +02:00
|
|
|
|
|
|
|
virtual ~BoundRefBase() = default;
|
|
|
|
|
|
|
|
virtual auto isFlag() const -> bool = 0;
|
|
|
|
virtual auto isContainer() const -> bool { return false; }
|
2017-09-26 00:12:00 +02:00
|
|
|
virtual auto setValue( std::string const &arg ) -> ParserResult = 0;
|
|
|
|
virtual auto setFlag( bool flag ) -> ParserResult = 0;
|
2017-06-13 00:04:24 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
struct BoundValueRefBase : BoundRefBase {
|
|
|
|
auto isFlag() const -> bool override { return false; }
|
|
|
|
|
2017-09-26 00:12:00 +02:00
|
|
|
auto setFlag( bool ) -> ParserResult override {
|
|
|
|
return ParserResult::logicError( "Flags can only be set on boolean fields" );
|
2017-06-13 00:04:24 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct BoundFlagRefBase : BoundRefBase {
|
|
|
|
auto isFlag() const -> bool override { return true; }
|
|
|
|
|
2017-09-26 00:12:00 +02:00
|
|
|
auto setValue( std::string const &arg ) -> ParserResult override {
|
2017-06-13 00:04:24 +02:00
|
|
|
bool flag;
|
2017-09-26 00:12:00 +02:00
|
|
|
auto result = convertInto( arg, flag );
|
|
|
|
if( result )
|
|
|
|
setFlag( flag );
|
2017-06-13 00:04:24 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
struct BoundRef : BoundValueRefBase {
|
|
|
|
T &m_ref;
|
|
|
|
|
2017-09-26 00:12:00 +02:00
|
|
|
explicit BoundRef( T &ref ) : m_ref( ref ) {}
|
2017-06-13 00:04:24 +02:00
|
|
|
|
2017-09-26 00:12:00 +02:00
|
|
|
auto setValue( std::string const &arg ) -> ParserResult override {
|
|
|
|
return convertInto( arg, m_ref );
|
2017-06-13 00:04:24 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
struct BoundRef<std::vector<T>> : BoundValueRefBase {
|
|
|
|
std::vector<T> &m_ref;
|
|
|
|
|
2017-09-26 00:12:00 +02:00
|
|
|
explicit BoundRef( std::vector<T> &ref ) : m_ref( ref ) {}
|
2017-06-13 00:04:24 +02:00
|
|
|
|
|
|
|
auto isContainer() const -> bool override { return true; }
|
|
|
|
|
2017-09-26 00:12:00 +02:00
|
|
|
auto setValue( std::string const &arg ) -> ParserResult override {
|
2017-06-13 00:04:24 +02:00
|
|
|
T temp;
|
2017-09-26 00:12:00 +02:00
|
|
|
auto result = convertInto( arg, temp );
|
|
|
|
if( result )
|
|
|
|
m_ref.push_back( temp );
|
2017-06-13 00:04:24 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct BoundFlagRef : BoundFlagRefBase {
|
|
|
|
bool &m_ref;
|
|
|
|
|
2017-09-26 00:12:00 +02:00
|
|
|
explicit BoundFlagRef( bool &ref ) : m_ref( ref ) {}
|
2017-06-13 00:04:24 +02:00
|
|
|
|
2017-09-26 00:12:00 +02:00
|
|
|
auto setFlag( bool flag ) -> ParserResult override {
|
2017-06-13 00:04:24 +02:00
|
|
|
m_ref = flag;
|
2017-09-26 00:12:00 +02:00
|
|
|
return ParserResult::ok( ParseResultType::Matched );
|
2017-06-13 00:04:24 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename ReturnType>
|
|
|
|
struct LambdaInvoker {
|
2017-09-26 00:12:00 +02:00
|
|
|
static_assert( std::is_same<ReturnType, ParserResult>::value, "Lambda must return void or clara::ParserResult" );
|
2017-06-13 00:04:24 +02:00
|
|
|
|
|
|
|
template<typename L, typename ArgType>
|
2017-09-26 00:12:00 +02:00
|
|
|
static auto invoke( L const &lambda, ArgType const &arg ) -> ParserResult {
|
|
|
|
return lambda( arg );
|
2017-06-13 00:04:24 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
|
|
|
struct LambdaInvoker<void> {
|
|
|
|
template<typename L, typename ArgType>
|
2017-09-26 00:12:00 +02:00
|
|
|
static auto invoke( L const &lambda, ArgType const &arg ) -> ParserResult {
|
|
|
|
lambda( arg );
|
|
|
|
return ParserResult::ok( ParseResultType::Matched );
|
2017-06-13 00:04:24 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename ArgType, typename L>
|
2017-09-26 00:12:00 +02:00
|
|
|
inline auto invokeLambda( L const &lambda, std::string const &arg ) -> ParserResult {
|
2017-06-13 00:04:24 +02:00
|
|
|
ArgType temp;
|
2017-09-26 00:12:00 +02:00
|
|
|
auto result = convertInto( arg, temp );
|
2017-06-13 00:04:24 +02:00
|
|
|
return !result
|
2017-09-26 00:12:00 +02:00
|
|
|
? result
|
|
|
|
: LambdaInvoker<typename UnaryLambdaTraits<L>::ReturnType>::invoke( lambda, temp );
|
2017-06-13 00:04:24 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
template<typename L>
|
|
|
|
struct BoundLambda : BoundValueRefBase {
|
|
|
|
L m_lambda;
|
|
|
|
|
|
|
|
static_assert( UnaryLambdaTraits<L>::isValid, "Supplied lambda must take exactly one argument" );
|
2017-09-26 00:12:00 +02:00
|
|
|
explicit BoundLambda( L const &lambda ) : m_lambda( lambda ) {}
|
2017-06-13 00:04:24 +02:00
|
|
|
|
2017-09-26 00:12:00 +02:00
|
|
|
auto setValue( std::string const &arg ) -> ParserResult override {
|
|
|
|
return invokeLambda<typename UnaryLambdaTraits<L>::ArgType>( m_lambda, arg );
|
2017-06-13 00:04:24 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename L>
|
|
|
|
struct BoundFlagLambda : BoundFlagRefBase {
|
|
|
|
L m_lambda;
|
|
|
|
|
|
|
|
static_assert( UnaryLambdaTraits<L>::isValid, "Supplied lambda must take exactly one argument" );
|
|
|
|
static_assert( std::is_same<typename UnaryLambdaTraits<L>::ArgType, bool>::value, "flags must be boolean" );
|
|
|
|
|
2017-09-26 00:12:00 +02:00
|
|
|
explicit BoundFlagLambda( L const &lambda ) : m_lambda( lambda ) {}
|
2017-06-13 00:04:24 +02:00
|
|
|
|
2017-09-26 00:12:00 +02:00
|
|
|
auto setFlag( bool flag ) -> ParserResult override {
|
|
|
|
return LambdaInvoker<typename UnaryLambdaTraits<L>::ReturnType>::invoke( m_lambda, flag );
|
2017-06-13 00:04:24 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-09-26 00:12:00 +02:00
|
|
|
enum class Optionality { Optional, Required };
|
2017-06-13 00:04:24 +02:00
|
|
|
|
|
|
|
struct Parser;
|
|
|
|
|
|
|
|
class ParserBase {
|
|
|
|
public:
|
2017-06-20 23:35:35 +02:00
|
|
|
virtual ~ParserBase() = default;
|
2017-06-13 00:04:24 +02:00
|
|
|
virtual auto validate() const -> Result { return Result::ok(); }
|
|
|
|
virtual auto parse( std::string const& exeName, TokenStream const &tokens) const -> InternalParseResult = 0;
|
|
|
|
virtual auto cardinality() const -> size_t { return 1; }
|
|
|
|
|
2017-09-26 00:12:00 +02:00
|
|
|
auto parse( Args const &args ) const -> InternalParseResult {
|
|
|
|
return parse( args.exeName(), TokenStream( args ) );
|
2017-06-13 00:04:24 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename DerivedT>
|
|
|
|
class ComposableParserImpl : public ParserBase {
|
|
|
|
public:
|
|
|
|
template<typename T>
|
2017-09-26 00:12:00 +02:00
|
|
|
auto operator|( T const &other ) const -> Parser;
|
2017-06-13 00:04:24 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// Common code and state for Args and Opts
|
|
|
|
template<typename DerivedT>
|
|
|
|
class ParserRefImpl : public ComposableParserImpl<DerivedT> {
|
|
|
|
protected:
|
|
|
|
Optionality m_optionality = Optionality::Optional;
|
|
|
|
std::shared_ptr<BoundRefBase> m_ref;
|
|
|
|
std::string m_hint;
|
|
|
|
std::string m_description;
|
|
|
|
|
2017-09-26 00:12:00 +02:00
|
|
|
explicit ParserRefImpl( std::shared_ptr<BoundRefBase> const &ref ) : m_ref( ref ) {}
|
2017-06-13 00:04:24 +02:00
|
|
|
|
|
|
|
public:
|
|
|
|
template<typename T>
|
2017-09-26 00:12:00 +02:00
|
|
|
ParserRefImpl( T &ref, std::string const &hint )
|
|
|
|
: m_ref( std::make_shared<BoundRef<T>>( ref ) ),
|
|
|
|
m_hint( hint )
|
|
|
|
{}
|
2017-06-13 00:04:24 +02:00
|
|
|
|
|
|
|
template<typename LambdaT>
|
2017-09-26 00:12:00 +02:00
|
|
|
ParserRefImpl( LambdaT const &ref, std::string const &hint )
|
|
|
|
: m_ref( std::make_shared<BoundLambda<LambdaT>>( ref ) ),
|
|
|
|
m_hint(hint)
|
|
|
|
{}
|
2017-06-13 00:04:24 +02:00
|
|
|
|
2017-09-26 00:12:00 +02:00
|
|
|
auto operator()( std::string const &description ) -> DerivedT & {
|
2017-06-13 00:04:24 +02:00
|
|
|
m_description = description;
|
|
|
|
return static_cast<DerivedT &>( *this );
|
|
|
|
}
|
|
|
|
|
|
|
|
auto optional() -> DerivedT & {
|
|
|
|
m_optionality = Optionality::Optional;
|
|
|
|
return static_cast<DerivedT &>( *this );
|
|
|
|
};
|
|
|
|
|
|
|
|
auto required() -> DerivedT & {
|
|
|
|
m_optionality = Optionality::Required;
|
|
|
|
return static_cast<DerivedT &>( *this );
|
|
|
|
};
|
|
|
|
|
|
|
|
auto isOptional() const -> bool {
|
|
|
|
return m_optionality == Optionality::Optional;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto cardinality() const -> size_t override {
|
2017-09-26 00:12:00 +02:00
|
|
|
if( m_ref->isContainer() )
|
2017-06-13 00:04:24 +02:00
|
|
|
return 0;
|
|
|
|
else
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto hint() const -> std::string { return m_hint; }
|
|
|
|
};
|
|
|
|
|
|
|
|
class ExeName : public ComposableParserImpl<ExeName> {
|
|
|
|
std::shared_ptr<std::string> m_name;
|
|
|
|
std::shared_ptr<BoundRefBase> m_ref;
|
|
|
|
|
|
|
|
template<typename LambdaT>
|
|
|
|
static auto makeRef(LambdaT const &lambda) -> std::shared_ptr<BoundRefBase> {
|
2017-09-26 00:12:00 +02:00
|
|
|
return std::make_shared<BoundLambda<LambdaT>>( lambda) ;
|
2017-06-13 00:04:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
2017-09-26 00:12:00 +02:00
|
|
|
ExeName() : m_name( std::make_shared<std::string>( "<executable>" ) ) {}
|
2017-06-13 00:04:24 +02:00
|
|
|
|
2017-09-26 00:12:00 +02:00
|
|
|
explicit ExeName( std::string &ref ) : ExeName() {
|
2017-06-13 00:04:24 +02:00
|
|
|
m_ref = std::make_shared<BoundRef<std::string>>( ref );
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename LambdaT>
|
|
|
|
explicit ExeName( LambdaT const& lambda ) : ExeName() {
|
|
|
|
m_ref = std::make_shared<BoundLambda<LambdaT>>( lambda );
|
|
|
|
}
|
|
|
|
|
|
|
|
// The exe name is not parsed out of the normal tokens, but is handled specially
|
|
|
|
auto parse( std::string const&, TokenStream const &tokens ) const -> InternalParseResult override {
|
2017-09-26 00:12:00 +02:00
|
|
|
return InternalParseResult::ok( ParseState( ParseResultType::NoMatch, tokens ) );
|
2017-06-13 00:04:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
auto name() const -> std::string { return *m_name; }
|
|
|
|
auto set( std::string const& newName ) -> ParserResult {
|
|
|
|
|
|
|
|
auto lastSlash = newName.find_last_of( "\\/" );
|
2017-09-26 00:12:00 +02:00
|
|
|
auto filename = ( lastSlash == std::string::npos )
|
2017-06-13 00:04:24 +02:00
|
|
|
? newName
|
|
|
|
: newName.substr( lastSlash+1 );
|
|
|
|
|
|
|
|
*m_name = filename;
|
|
|
|
if( m_ref )
|
|
|
|
return m_ref->setValue( filename );
|
|
|
|
else
|
|
|
|
return ParserResult::ok( ParseResultType::Matched );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class Arg : public ParserRefImpl<Arg> {
|
|
|
|
public:
|
|
|
|
using ParserRefImpl::ParserRefImpl;
|
|
|
|
|
2017-09-26 00:12:00 +02:00
|
|
|
auto parse( std::string const &, TokenStream const &tokens ) const -> InternalParseResult override {
|
2017-06-13 09:29:56 +02:00
|
|
|
auto validationResult = validate();
|
2017-09-26 00:12:00 +02:00
|
|
|
if( !validationResult )
|
|
|
|
return InternalParseResult( validationResult );
|
2017-06-13 00:04:24 +02:00
|
|
|
|
|
|
|
auto remainingTokens = tokens;
|
|
|
|
auto const &token = *remainingTokens;
|
2017-09-26 00:12:00 +02:00
|
|
|
if( token.type != TokenType::Argument )
|
|
|
|
return InternalParseResult::ok( ParseState( ParseResultType::NoMatch, remainingTokens ) );
|
2017-06-13 00:04:24 +02:00
|
|
|
|
2017-09-26 00:12:00 +02:00
|
|
|
auto result = m_ref->setValue( remainingTokens->token );
|
|
|
|
if( !result )
|
|
|
|
return InternalParseResult( result );
|
2017-06-13 00:04:24 +02:00
|
|
|
else
|
2017-09-26 00:12:00 +02:00
|
|
|
return InternalParseResult::ok( ParseState( ParseResultType::Matched, ++remainingTokens ) );
|
2017-06-13 00:04:24 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-09-26 00:12:00 +02:00
|
|
|
inline auto normaliseOpt( std::string const &optName ) -> std::string {
|
|
|
|
if( optName[0] == '/' )
|
|
|
|
return "-" + optName.substr( 1 );
|
2017-06-13 00:04:24 +02:00
|
|
|
else
|
|
|
|
return optName;
|
|
|
|
}
|
|
|
|
|
|
|
|
class Opt : public ParserRefImpl<Opt> {
|
|
|
|
protected:
|
|
|
|
std::vector<std::string> m_optNames;
|
|
|
|
|
|
|
|
public:
|
|
|
|
template<typename LambdaT>
|
2017-09-26 00:12:00 +02:00
|
|
|
explicit Opt( LambdaT const &ref ) : ParserRefImpl( std::make_shared<BoundFlagLambda<LambdaT>>( ref ) ) {}
|
2017-06-13 00:04:24 +02:00
|
|
|
|
2017-09-26 00:12:00 +02:00
|
|
|
explicit Opt( bool &ref ) : ParserRefImpl( std::make_shared<BoundFlagRef>( ref ) ) {}
|
2017-06-13 00:04:24 +02:00
|
|
|
|
2017-06-22 17:07:05 +02:00
|
|
|
template<typename LambdaT>
|
|
|
|
Opt( LambdaT const &ref, std::string const &hint ) : ParserRefImpl( ref, hint ) {}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
Opt( T &ref, std::string const &hint ) : ParserRefImpl( ref, hint ) {}
|
|
|
|
|
2017-09-26 00:12:00 +02:00
|
|
|
auto operator[]( std::string const &optName ) -> Opt & {
|
|
|
|
m_optNames.push_back( optName );
|
2017-06-13 00:04:24 +02:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto getHelpColumns() const -> std::vector<HelpColumns> {
|
|
|
|
std::ostringstream oss;
|
|
|
|
bool first = true;
|
2017-09-26 00:12:00 +02:00
|
|
|
for( auto const &opt : m_optNames ) {
|
2017-06-13 00:04:24 +02:00
|
|
|
if (first)
|
|
|
|
first = false;
|
|
|
|
else
|
|
|
|
oss << ", ";
|
|
|
|
oss << opt;
|
|
|
|
}
|
2017-09-26 00:12:00 +02:00
|
|
|
if( !m_hint.empty() )
|
2017-06-13 00:04:24 +02:00
|
|
|
oss << " <" << m_hint << ">";
|
2017-09-26 00:12:00 +02:00
|
|
|
return { { oss.str(), m_description } };
|
2017-06-13 00:04:24 +02:00
|
|
|
}
|
|
|
|
|
2017-09-26 00:12:00 +02:00
|
|
|
auto isMatch( std::string const &optToken ) const -> bool {
|
|
|
|
#ifdef CATCH_PLATFORM_WINDOWS
|
2017-06-13 00:04:24 +02:00
|
|
|
auto normalisedToken = normaliseOpt( optToken );
|
|
|
|
#else
|
|
|
|
auto const &normalisedToken = optToken;
|
|
|
|
#endif
|
2017-09-26 00:12:00 +02:00
|
|
|
for( auto const &name : m_optNames ) {
|
|
|
|
if( normaliseOpt( name ) == normalisedToken )
|
2017-06-13 00:04:24 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
using ParserBase::parse;
|
|
|
|
|
2017-06-20 19:03:37 +02:00
|
|
|
auto parse( std::string const&, TokenStream const &tokens ) const -> InternalParseResult override {
|
|
|
|
auto validationResult = validate();
|
2017-09-26 00:12:00 +02:00
|
|
|
if( !validationResult )
|
|
|
|
return InternalParseResult( validationResult );
|
2017-06-13 00:04:24 +02:00
|
|
|
|
|
|
|
auto remainingTokens = tokens;
|
2017-09-26 00:12:00 +02:00
|
|
|
if( remainingTokens && remainingTokens->type == TokenType::Option ) {
|
2017-06-13 00:04:24 +02:00
|
|
|
auto const &token = *remainingTokens;
|
2017-09-26 00:12:00 +02:00
|
|
|
if( isMatch(token.token ) ) {
|
|
|
|
if( m_ref->isFlag() ) {
|
|
|
|
auto result = m_ref->setFlag( true );
|
|
|
|
if( !result )
|
|
|
|
return InternalParseResult( result );
|
|
|
|
if( result.value() == ParseResultType::ShortCircuitAll )
|
|
|
|
return InternalParseResult::ok( ParseState( result.value(), remainingTokens ) );
|
2017-06-13 00:04:24 +02:00
|
|
|
} else {
|
|
|
|
++remainingTokens;
|
2017-09-26 00:12:00 +02:00
|
|
|
if( !remainingTokens )
|
|
|
|
return InternalParseResult::runtimeError( "Expected argument following " + token.token );
|
2017-06-13 00:04:24 +02:00
|
|
|
auto const &argToken = *remainingTokens;
|
2017-09-26 00:12:00 +02:00
|
|
|
if( argToken.type != TokenType::Argument )
|
|
|
|
return InternalParseResult::runtimeError( "Expected argument following " + token.token );
|
|
|
|
auto result = m_ref->setValue( argToken.token );
|
|
|
|
if( !result )
|
|
|
|
return InternalParseResult( result );
|
|
|
|
if( result.value() == ParseResultType::ShortCircuitAll )
|
|
|
|
return InternalParseResult::ok( ParseState( result.value(), remainingTokens ) );
|
2017-06-13 00:04:24 +02:00
|
|
|
}
|
2017-09-26 00:12:00 +02:00
|
|
|
return InternalParseResult::ok( ParseState( ParseResultType::Matched, ++remainingTokens ) );
|
2017-06-13 00:04:24 +02:00
|
|
|
}
|
|
|
|
}
|
2017-09-26 00:12:00 +02:00
|
|
|
return InternalParseResult::ok( ParseState( ParseResultType::NoMatch, remainingTokens ) );
|
2017-06-13 00:04:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
auto validate() const -> Result override {
|
2017-09-26 00:12:00 +02:00
|
|
|
if( m_optNames.empty() )
|
|
|
|
return Result::logicError( "No options supplied to Opt" );
|
|
|
|
for( auto const &name : m_optNames ) {
|
|
|
|
if( name.empty() )
|
|
|
|
return Result::logicError( "Option name cannot be empty" );
|
|
|
|
if( name[0] != '-' && name[0] != '/' )
|
|
|
|
return Result::logicError( "Option name must begin with '-' or '/'" );
|
2017-06-13 00:04:24 +02:00
|
|
|
}
|
|
|
|
return ParserRefImpl::validate();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Help : Opt {
|
|
|
|
Help( bool &showHelpFlag )
|
|
|
|
: Opt([&]( bool flag ) {
|
2017-09-26 00:12:00 +02:00
|
|
|
showHelpFlag = flag;
|
|
|
|
return ParserResult::ok( ParseResultType::ShortCircuitAll );
|
2017-06-13 00:04:24 +02:00
|
|
|
})
|
|
|
|
{
|
2017-09-26 00:12:00 +02:00
|
|
|
static_cast<Opt &>( *this )
|
2017-06-13 00:04:24 +02:00
|
|
|
("display usage information")
|
|
|
|
["-?"]["-h"]["--help"]
|
|
|
|
.optional();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
struct Parser : ParserBase {
|
|
|
|
|
|
|
|
mutable ExeName m_exeName;
|
|
|
|
std::vector<Opt> m_options;
|
|
|
|
std::vector<Arg> m_args;
|
|
|
|
|
2017-09-26 00:12:00 +02:00
|
|
|
auto operator|=( ExeName const &exeName ) -> Parser & {
|
2017-06-13 00:04:24 +02:00
|
|
|
m_exeName = exeName;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2017-09-26 00:12:00 +02:00
|
|
|
auto operator|=( Arg const &arg ) -> Parser & {
|
2017-06-13 00:04:24 +02:00
|
|
|
m_args.push_back(arg);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2017-09-26 00:12:00 +02:00
|
|
|
auto operator|=( Opt const &opt ) -> Parser & {
|
2017-06-13 00:04:24 +02:00
|
|
|
m_options.push_back(opt);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2017-09-26 00:12:00 +02:00
|
|
|
auto operator|=( Parser const &other ) -> Parser & {
|
2017-06-13 00:04:24 +02:00
|
|
|
m_options.insert(m_options.end(), other.m_options.begin(), other.m_options.end());
|
|
|
|
m_args.insert(m_args.end(), other.m_args.begin(), other.m_args.end());
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2017-09-26 00:12:00 +02:00
|
|
|
auto operator|( T const &other ) const -> Parser {
|
|
|
|
return Parser( *this ) |= other;
|
2017-06-13 00:04:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
auto getHelpColumns() const -> std::vector<HelpColumns> {
|
|
|
|
std::vector<HelpColumns> cols;
|
|
|
|
for (auto const &o : m_options) {
|
|
|
|
auto childCols = o.getHelpColumns();
|
2017-09-26 00:12:00 +02:00
|
|
|
cols.insert( cols.end(), childCols.begin(), childCols.end() );
|
2017-06-13 00:04:24 +02:00
|
|
|
}
|
|
|
|
return cols;
|
|
|
|
}
|
|
|
|
|
2017-09-26 00:12:00 +02:00
|
|
|
void writeToStream( std::ostream &os ) const {
|
2017-06-13 00:04:24 +02:00
|
|
|
if (!m_exeName.name().empty()) {
|
|
|
|
os << "usage:\n" << " " << m_exeName.name() << " ";
|
|
|
|
bool required = true, first = true;
|
2017-09-26 00:12:00 +02:00
|
|
|
for( auto const &arg : m_args ) {
|
2017-06-13 00:04:24 +02:00
|
|
|
if (first)
|
|
|
|
first = false;
|
|
|
|
else
|
|
|
|
os << " ";
|
2017-09-26 00:12:00 +02:00
|
|
|
if( arg.isOptional() && required ) {
|
2017-06-13 00:04:24 +02:00
|
|
|
os << "[";
|
|
|
|
required = false;
|
|
|
|
}
|
|
|
|
os << "<" << arg.hint() << ">";
|
2017-09-26 00:12:00 +02:00
|
|
|
if( arg.cardinality() == 0 )
|
2017-06-13 00:04:24 +02:00
|
|
|
os << " ... ";
|
|
|
|
}
|
2017-09-26 00:12:00 +02:00
|
|
|
if( !required )
|
2017-06-13 00:04:24 +02:00
|
|
|
os << "]";
|
2017-09-26 00:12:00 +02:00
|
|
|
if( !m_options.empty() )
|
2017-06-13 00:04:24 +02:00
|
|
|
os << " options";
|
|
|
|
os << "\n\nwhere options are:" << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto rows = getHelpColumns();
|
2017-06-22 17:07:05 +02:00
|
|
|
size_t consoleWidth = CATCH_CLARA_CONFIG_CONSOLE_WIDTH;
|
2017-06-13 00:04:24 +02:00
|
|
|
size_t optWidth = 0;
|
2017-09-26 00:12:00 +02:00
|
|
|
for( auto const &cols : rows )
|
2017-06-13 00:04:24 +02:00
|
|
|
optWidth = std::max(optWidth, cols.left.size() + 2);
|
|
|
|
|
2017-09-26 00:12:00 +02:00
|
|
|
for( auto const &cols : rows ) {
|
2017-06-13 00:04:24 +02:00
|
|
|
auto row =
|
2017-09-26 00:12:00 +02:00
|
|
|
TextFlow::Column( cols.left ).width( optWidth ).indent( 2 ) +
|
2017-06-13 00:04:24 +02:00
|
|
|
TextFlow::Spacer(4) +
|
2017-09-26 00:12:00 +02:00
|
|
|
TextFlow::Column( cols.right ).width( consoleWidth - 7 - optWidth );
|
2017-06-13 00:04:24 +02:00
|
|
|
os << row << std::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-26 00:12:00 +02:00
|
|
|
friend auto operator<<( std::ostream &os, Parser const &parser ) -> std::ostream& {
|
|
|
|
parser.writeToStream( os );
|
2017-06-13 00:04:24 +02:00
|
|
|
return os;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto validate() const -> Result override {
|
2017-09-26 00:12:00 +02:00
|
|
|
for( auto const &opt : m_options ) {
|
2017-06-13 00:04:24 +02:00
|
|
|
auto result = opt.validate();
|
2017-09-26 00:12:00 +02:00
|
|
|
if( !result )
|
2017-06-13 00:04:24 +02:00
|
|
|
return result;
|
|
|
|
}
|
2017-09-26 00:12:00 +02:00
|
|
|
for( auto const &arg : m_args ) {
|
2017-06-13 00:04:24 +02:00
|
|
|
auto result = arg.validate();
|
2017-09-26 00:12:00 +02:00
|
|
|
if( !result )
|
2017-06-13 00:04:24 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
return Result::ok();
|
|
|
|
}
|
|
|
|
|
|
|
|
using ParserBase::parse;
|
|
|
|
|
2017-09-26 00:12:00 +02:00
|
|
|
auto parse( std::string const& exeName, TokenStream const &tokens ) const -> InternalParseResult override {
|
2017-06-13 00:04:24 +02:00
|
|
|
|
2017-09-26 00:12:00 +02:00
|
|
|
struct ParserInfo {
|
|
|
|
ParserBase const* parser = nullptr;
|
|
|
|
size_t count = 0;
|
|
|
|
};
|
|
|
|
const size_t totalParsers = m_options.size() + m_args.size();
|
2017-09-27 00:55:34 +02:00
|
|
|
assert( totalParsers < 512 );
|
|
|
|
// ParserInfo parseInfos[totalParsers]; // <-- this is what we really want to do
|
|
|
|
ParserInfo parseInfos[512];
|
2017-09-26 00:12:00 +02:00
|
|
|
size_t i = 0;
|
|
|
|
for( auto const& opt : m_options ) parseInfos[i++].parser = &opt;
|
|
|
|
for( auto const& arg : m_args ) parseInfos[i++].parser = &arg;
|
2017-06-13 00:04:24 +02:00
|
|
|
|
|
|
|
m_exeName.set( exeName );
|
|
|
|
|
2017-09-26 00:12:00 +02:00
|
|
|
auto result = InternalParseResult::ok( ParseState( ParseResultType::NoMatch, tokens ) );
|
|
|
|
while( result.value().remainingTokens() ) {
|
|
|
|
bool tokenParsed = false;
|
|
|
|
|
2017-09-27 00:55:34 +02:00
|
|
|
for( size_t i = 0; i < totalParsers; ++i ) {
|
|
|
|
auto& parseInfo = parseInfos[i];
|
2017-09-26 00:12:00 +02:00
|
|
|
if( parseInfo.parser->cardinality() == 0 || parseInfo.count < parseInfo.parser->cardinality() ) {
|
|
|
|
result = parseInfo.parser->parse(exeName, result.value().remainingTokens());
|
|
|
|
if (!result)
|
|
|
|
return result;
|
|
|
|
if (result.value().type() != ParseResultType::NoMatch) {
|
|
|
|
tokenParsed = true;
|
|
|
|
++parseInfo.count;
|
|
|
|
break;
|
|
|
|
}
|
2017-06-13 00:04:24 +02:00
|
|
|
}
|
|
|
|
}
|
2017-09-26 00:12:00 +02:00
|
|
|
|
|
|
|
if( result.value().type() == ParseResultType::ShortCircuitAll )
|
2017-06-13 00:04:24 +02:00
|
|
|
return result;
|
2017-09-26 00:12:00 +02:00
|
|
|
if( !tokenParsed )
|
|
|
|
return InternalParseResult::runtimeError( "Unrecognised token: " + result.value().remainingTokens()->token );
|
2017-06-13 00:04:24 +02:00
|
|
|
}
|
|
|
|
// !TBD Check missing required options
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename DerivedT>
|
|
|
|
template<typename T>
|
2017-09-26 00:12:00 +02:00
|
|
|
auto ComposableParserImpl<DerivedT>::operator|( T const &other ) const -> Parser {
|
|
|
|
return Parser() | static_cast<DerivedT const &>( *this ) | other;
|
2017-06-13 00:04:24 +02:00
|
|
|
}
|
|
|
|
} // namespace detail
|
|
|
|
|
|
|
|
|
|
|
|
// A Combined parser
|
|
|
|
using detail::Parser;
|
|
|
|
|
|
|
|
// A parser for options
|
|
|
|
using detail::Opt;
|
|
|
|
|
|
|
|
// A parser for arguments
|
|
|
|
using detail::Arg;
|
|
|
|
|
|
|
|
// Wrapper for argc, argv from main()
|
|
|
|
using detail::Args;
|
|
|
|
|
|
|
|
// Specifies the name of the executable
|
|
|
|
using detail::ExeName;
|
|
|
|
|
|
|
|
// Convenience wrapper for option parser that specifies the help option
|
|
|
|
using detail::Help;
|
|
|
|
|
|
|
|
// enum of result types from a parse
|
|
|
|
using detail::ParseResultType;
|
|
|
|
|
|
|
|
// Result type for parser operation
|
|
|
|
using detail::ParserResult;
|
|
|
|
|
|
|
|
|
2017-06-20 19:03:37 +02:00
|
|
|
}} // namespace Catch::clara
|
|
|
|
|
2017-06-13 00:04:24 +02:00
|
|
|
|
|
|
|
#endif // CATCH_CLARA_HPP_INCLUDED
|