New CLI parser impl

This commit is contained in:
Phil Nash 2013-05-15 19:25:34 +01:00
parent 597ed1f79d
commit 372a8b018d

View File

@ -14,292 +14,362 @@
namespace Clara { namespace Clara {
template<typename T> struct RemoveConstRef{ typedef T type; }; template<typename T> struct RemoveConstRef{ typedef T type; };
template<typename T> struct RemoveConstRef<T&>{ typedef T type; }; template<typename T> struct RemoveConstRef<T&>{ typedef T type; };
template<typename T> struct RemoveConstRef<T const&>{ typedef T type; }; template<typename T> struct RemoveConstRef<T const&>{ typedef T type; };
template<typename T> struct RemoveConstRef<T const>{ typedef T type; }; template<typename T> struct RemoveConstRef<T const>{ typedef T type; };
using namespace Catch; template<typename T> struct IsBool { static const bool value = false; };
template<> struct IsBool<bool> { static const bool value = true; };
template<typename T> template<typename T>
bool convertInto( std::string const& _source, T& _dest ) { void convertInto( std::string const& _source, T& _dest ) {
std::stringstream ss; std::stringstream ss;
ss << _source; ss << _source;
ss >> _dest; ss >> _dest;
return !ss.fail(); if( ss.fail() )
} throw std::runtime_error( "Unable to convert " + _source + " to destination type" );
inline bool convertInto( std::string const& _source, std::string& _dest ) { }
_dest = _source; inline void convertInto( std::string const& _source, std::string& _dest ) {
return true; _dest = _source;
} }
inline bool convertInto( std::string const& _source, bool _dest ) { inline void convertInto( std::string const& _source, bool& _dest ) {
std::string sourceLC = toLower( _source ); std::string sourceLC = _source;
if( sourceLC == "1" || sourceLC == "true" || sourceLC == "yes" || sourceLC == "on" ) std::transform( sourceLC.begin(), sourceLC.end(), sourceLC.begin(), ::tolower );
_dest = true; if( sourceLC == "1" || sourceLC == "true" || sourceLC == "yes" || sourceLC == "on" )
else if( sourceLC == "0" || sourceLC == "false" || sourceLC == "no" || sourceLC == "off" ) _dest = true;
_dest = false; else if( sourceLC == "0" || sourceLC == "false" || sourceLC == "no" || sourceLC == "off" )
else _dest = false;
return false; else
return true; throw std::runtime_error( "Expected a boolean value but did recognise: '" + _source + "'" );
} }
inline void convertInto( bool _source, bool& _dest ) {
template<typename T> _dest = _source;
class Opt { }
public: template<typename T>
Opt( std::string const& _synposis ) : m_synopsis( _synposis ) {} inline void convertInto( bool, T& ) {
Opt& shortName( std::string const& _value ) { m_shortName = _value; return *this; } throw std::runtime_error( "Invalid conversion" );
Opt& longName( std::string const& _value ) { m_longName = _value; return *this; }
template<typename M>
Opt& optArg( std::string const& _name, M const& _member ){
m_argName = _name;
m_field = new Field<M>( _member );
return *this;
} }
template<typename M> template<typename ConfigT>
Opt& flag( M const& _member ){ struct IBoundMember {
m_field = new FlagField<M>( _member ); virtual ~IBoundMember() {}
return *this; virtual void set( ConfigT& config, std::string const& value ) const = 0;
} virtual void setFlag( ConfigT& config ) const = 0;
virtual bool takesArg() const = 0;
std::size_t takesArg() const { return !m_argName.empty(); } virtual IBoundMember* clone() const = 0;
std::string synopsis() const { return m_synopsis; }
std::string shortName() const { return m_shortName; }
std::string longName() const { return m_longName; }
bool parseInto( std::string const& _arg, T& _config ) const {
if( m_argName.empty() )
m_field->set( _config );
else if( !m_field->parseInto( _arg, _config ) )
throw std::domain_error( "'" + _arg + "' was not valid for <" + m_argName + ">" );
return true;
}
std::string usage() const {
std::ostringstream oss;
writeToStream( oss );
return oss.str();
}
friend std::ostream& operator << ( std::ostream& os, Opt const& _opt ) {
_opt.writeToStream( os );
return os;
}
void writeToStream ( std::ostream& os ) const {
if( !m_shortName.empty() )
os << "-" << m_shortName;
if( !m_longName.empty() )
os << ", ";
if( !m_longName.empty() )
os << "--" << m_longName;
if( takesArg() )
os << " <" << m_argName << ">";
}
private:
struct IField : SharedImpl<> {
virtual ~IField() {}
virtual bool parseInto( std::string const&, T& ) const {
throw std::logic_error( "cannot pass argument to bool binder" );
}
virtual void set( T& ) const {
throw std::logic_error( "field requires an argument" );
}
}; };
template<typename F>
struct Field;
template<typename F> template<typename ConfigT>
struct FlagField; class BoundField {
public:
// Data member : with argument BoundField( IBoundMember<ConfigT>* _boundMember ) : boundMember( _boundMember ) {}
template<typename C, typename M> BoundField( BoundField const& other ) : boundMember( other.boundMember->clone() ) {}
struct Field<M C::*> : IField { BoundField& operator = ( BoundField const& other ) {
Field( M C::* _member ) : member( _member ) {} IBoundMember<ConfigT> newMember = other.clone();
bool parseInto( std::string const& _arg, T& _config ) const { delete boundMember;
return convertInto( _arg, _config.*member ); boundMember = newMember;
return *this;
} }
~BoundField() { delete boundMember; }
void set( ConfigT& config, std::string const& value ) const {
boundMember->set( config, value );
}
void setFlag( ConfigT& config ) const {
boundMember->setFlag( config );
}
bool takesArg() const { return boundMember->takesArg(); }
private:
IBoundMember<ConfigT>* boundMember;
};
template<typename C, typename M>
struct BoundDataMember : IBoundMember<C>{
BoundDataMember( M C::* _member ) : member( _member ) {}
virtual void set( C& p, std::string const& stringValue ) const {
convertInto( stringValue, p.*member );
}
virtual void setFlag( C& p ) const {
convertInto( true, p.*member );
}
virtual bool takesArg() const { return !IsBool<M>::value; }
virtual IBoundMember<C>* clone() const { return new BoundDataMember( *this ); }
M C::* member; M C::* member;
}; };
// Data member : flag
template<typename C, typename M> template<typename C, typename M>
struct FlagField<M C::*> : IField { struct BoundUnaryMethod : IBoundMember<C>{
FlagField( M C::* _member ) : member( _member ) {} BoundUnaryMethod( void (C::*_member)( M ) ) : member( _member ) {}
void set( T& _config ) const { virtual void set( C& p, std::string const& stringValue ) const {
_config.*member = true;
}
M C::* member;
};
// Unary method : with argument
template<typename C, typename M>
struct Field<void (C::*)( M )> : IField {
Field( void (C::*_method)( M ) ) : method( _method ) {}
bool parseInto( std::string const& _arg, T& _config ) const {
typename RemoveConstRef<M>::type value; typename RemoveConstRef<M>::type value;
if( !convertInto( _arg, value ) ) convertInto( stringValue, value );
return false; (p.*member)( value );
( _config.*method )( value );
return true;
} }
void (C::*method)( M ); virtual void setFlag( C& p ) const {
}; typename RemoveConstRef<M>::type value;
convertInto( true, value );
// Unary method : flag (p.*member)( value );
template<typename C, typename M>
struct FlagField<void (C::*)( M )> : IField {
FlagField( void (C::*_method)( M ) ) : method( _method ) {}
void set( T& _config ) const {
( _config.*method )( true );
} }
void (C::*method)( M ); virtual bool takesArg() const { return !IsBool<M>::value; }
virtual IBoundMember<C>* clone() const { return new BoundUnaryMethod( *this ); }
void (C::*member)( M );
}; };
// Nullary method : flag
template<typename C> template<typename C>
struct FlagField<void (C::*)()> : IField { struct BoundNullaryMethod : IBoundMember<C>{
FlagField( void (C::*_method)() ) : method( _method ) {} BoundNullaryMethod( void (C::*_member)() ) : member( _member ) {}
void set( T& _config ) const { virtual void set( C& p, std::string const& stringValue ) const {
( _config.*method )(); bool value;
convertInto( stringValue, value );
if( value )
(p.*member)();
} }
void (C::*method)(); virtual void setFlag( C& p ) const {
(p.*member)();
}
virtual bool takesArg() const { return false; }
virtual IBoundMember<C>* clone() const { return new BoundNullaryMethod( *this ); }
void (C::*member)();
}; };
std::string m_synopsis; template<typename C, typename M>
std::string m_shortName; BoundField<C> makeBoundField( M C::* _member ) {
std::string m_longName; return BoundField<C>( new BoundDataMember<C,M>( _member ) );
std::string m_argName; }
Ptr<IField> m_field; template<typename C, typename M>
}; BoundField<C> makeBoundField( void (C::*_member)( M ) ) {
return BoundField<C>( new BoundUnaryMethod<C,M>( _member ) );
template<typename T> }
class Parser template<typename C>
{ BoundField<C> makeBoundField( void (C::*_member)() ) {
public: return BoundField<C>( new BoundNullaryMethod<C>( _member ) );
Parser()
: m_separatorChars( "=: " ),
m_allowSpaceSeparator( m_separatorChars.find( ' ' ) != std::string::npos )
{}
template<typename M>
Parser( std::string const&, M ) // !TBD
: m_separatorChars( "=: " ),
m_allowSpaceSeparator( m_separatorChars.find( ' ' ) != std::string::npos )
{}
template<typename M>
Parser& operator()( std::string const&, M ) { return *this; } // !TBD
Opt<T>& addOption( std::string const& _synposis ) {
m_allOptionParsers.push_back( _synposis );
return m_allOptionParsers.back();
} }
void parseInto( int argc, const char* const argv[], T& _config ) {
std::vector<std::string> args; template<typename ConfigT>
args.reserve( static_cast<std::size_t>( argc ) ); class CommandLine {
for( int i = 0; i < argc; ++i ) struct ArgToken {
args.push_back( argv[i] ); enum TokenType {
Positional,
ShortOpt,
LongOpt
};
ArgToken() : type( Positional ) {}
ArgToken( TokenType _type, std::string const& _arg )
: type( _type ), arg( _arg ) {}
TokenType type;
std::string arg;
};
class ArgBinder {
public:
ArgBinder( CommandLine* cl ) : m_cl( cl ) {}
template<typename F>
ArgBinder& bind( F f ) {
if( !m_cl->opts.empty() )
m_cl->opts.back().validate();
m_cl->opts.push_back( Opt( makeBoundField( f ) ) );
return *this;
}
ArgBinder& shortOpt( std::string const& name ) {
m_cl->opts.back().shortNames.push_back( name );
return *this;
}
ArgBinder& longOpt( std::string const& name ) {
m_cl->opts.back().longName = name;
return *this;
}
ArgBinder& describe( std::string const& description ) {
m_cl->opts.back().description = description;
return *this;
}
ArgBinder& argName( std::string const& argName ) {
m_cl->opts.back().argName = argName;
return *this;
}
ArgBinder& position( int /*position*/ ) {
// !TBD: Support for positional args in fixed positions
return *this;
}
private:
CommandLine* m_cl;
};
struct Opt {
public:
Opt( BoundField<ConfigT> const& _boundField ) : boundField( _boundField ) {}
bool hasShortName( std::string const& shortName ) const {
for( std::vector<std::string>::const_iterator
it = shortNames.begin(), itEnd = shortNames.end();
it != itEnd;
++it )
if( *it == shortName )
return true;
return false;
}
bool hasLongName( std::string const& _longName ) const {
return _longName == longName;
}
bool takesArg() const {
return !argName.empty();
}
bool isPositional() const {
return shortNames.empty() && longName.empty();
}
std::string dbgName() const {
if( !longName.empty() )
return "--" + longName;
if( !shortNames.empty() )
return "-" + shortNames[0];
return "positional args";
}
void validate() const {
if( boundField.takesArg() && !takesArg() )
throw std::logic_error( dbgName() + " must specify an arg name" );
}
std::string commands() const {
std::ostringstream oss;
bool first = true;
std::vector<std::string>::const_iterator it = shortNames.begin(), itEnd = shortNames.end();
for(; it != itEnd; ++it ) {
if( first )
first = false;
else
oss << ", ";
oss << "-" << *it;
}
if( !longName.empty() ) {
if( !first )
oss << ", ";
oss << "--" << longName;
}
if( !argName.empty() )
oss << " <" << argName << ">";
return oss.str();
}
BoundField<ConfigT> boundField;
std::vector<std::string> shortNames;
std::string longName;
std::string description;
std::string argName;
};
public:
template<typename F>
ArgBinder bind( F f ) {
ArgBinder binder( this );
binder.bind( f );
return binder;
}
void parseInto( int argc, char const* argv[], ConfigT& config ) const {
if( opts.empty() )
throw std::logic_error( "No options or arguments specified" );
opts.back().validate();
std::vector<ArgToken> tokens;
parseIntoTokens( argc, argv, tokens );
setFromTokens( tokens, config );
}
void usage( std::ostream& os ) const {
typename std::vector<Opt>::const_iterator itBegin = opts.begin(), itEnd = opts.end(), it;
std::size_t maxWidth = 0;
for( it = itBegin; it != itEnd; ++it )
maxWidth = (std::max)( maxWidth, it->commands().size() );
for( it = itBegin; it != itEnd; ++it ) {
Catch::Text usage( it->commands(), Catch::TextAttributes().setWidth( maxWidth ) );
// !TBD handle longer usage strings
Catch::Text desc( it->description, Catch::TextAttributes().setWidth( CATCH_CONFIG_CONSOLE_WIDTH - maxWidth -3 ) );
for( std::size_t i = 0; i < std::max( usage.size(), desc.size() ); ++i ) {
std::string usageCol = i < usage.size() ? usage[i] : "";
os << usageCol;
if( i < desc.size() && !desc[i].empty() )
os << std::string( 2 + maxWidth - usageCol.size(), ' ' )
<< desc[i];
os << "\n";
}
}
}
std::string usage() const {
std::ostringstream oss;
usage( oss );
return oss.str();
}
friend std::ostream& operator << ( std::ostream& os, CommandLine const& parser ) {
parser.usage( os );
return os;
}
parseInto( args, _config ); private:
} void parseIntoTokens( int argc, char const* argv[], std::vector<ArgToken>& tokens ) const {
for( int i = 1; i < argc; ++i ) {
template<typename U> std::string arg = argv[i];
void parseRemainingArgs( Parser<U>& _parser, T& _config ) { while( !arg.empty() ) {
parseInto( _parser.m_unusedOpts, _config ); ArgToken token( ArgToken::Positional, arg );
} arg = "";
if( token.arg[0] == '-' ) {
void parseInto( std::vector<std::string> const& _args, T& _config ) { if( token.arg.size() > 1 && token.arg[1] == '-' )
ensureOptions(); token = ArgToken( ArgToken::LongOpt, token.arg.substr( 2 ) );
for( std::size_t i = 0; i < _args.size(); ++i ) { else
std::string const& arg = _args[i]; token = ArgToken( ArgToken::ShortOpt, token.arg.substr( 1 ) );
if( arg[0] == '-' ) { }
std::string optArgs, optName; if( token.type != ArgToken::Positional ) {
std::size_t pos = arg.find_first_of( m_separatorChars ); std::size_t pos = token.arg.find_first_of( " \t=:" );
if( pos == std::string::npos ) { if( pos != std::string::npos ) {
optName = arg; arg = token.arg.substr( pos+1 );
} token.arg = token.arg.substr( 0, pos );
else {
optName = arg.substr(0, pos );
optArgs = arg.substr( pos+1 );
}
typename std::map<std::string, Opt<T> const*>::const_iterator it = m_optionsByName.find( optName );
bool used = false;
if( it != m_optionsByName.end() ) {
Opt<T> const& opt = *(it->second);
if( opt.takesArg() ) {
if( optArgs.empty() ) {
if( i < _args.size() && _args[i+1][0] != '-' )
optArgs = _args[++i];
else
throw std::domain_error( "Expected argument"); // !TBD better error
} }
} }
try { tokens.push_back( token );
used = opt.parseInto( optArgs, _config );
}
catch( std::exception& ex ) {
throw std::domain_error( "Error in " + optName + " option: " + ex.what() );
}
} }
if( !used )
m_unusedOpts.push_back( arg );
}
else {
m_args.push_back( arg );
}
}
}
friend std::ostream& operator <<( std::ostream& os, Parser const& _parser ) {
typename std::vector<Opt<T> >::const_iterator it, itEnd = _parser.m_allOptionParsers.end();
std::size_t maxWidth = 0;
for(it = _parser.m_allOptionParsers.begin(); it != itEnd; ++it )
maxWidth = (std::max)( it->usage().size(), maxWidth );
for(it = _parser.m_allOptionParsers.begin(); it != itEnd; ++it ) {
Text usage( it->usage(), TextAttributes().setWidth( maxWidth ) );
// !TBD handle longer usage strings
Text synopsis( it->synopsis(), TextAttributes().setWidth( CATCH_CONFIG_CONSOLE_WIDTH - maxWidth -3 ) );
for( std::size_t i = 0; i < std::max( usage.size(), synopsis.size() ); ++i ) {
std::string usageCol = i < usage.size() ? usage[i] : "";
std::cout << usageCol;
if( i < synopsis.size() && !synopsis[i].empty() )
std::cout << std::string( 2 + maxWidth - usageCol.size(), ' ' )
<< synopsis[i];
std::cout << "\n";
} }
} }
return os; void setFromTokens( std::vector<ArgToken>const & tokens, ConfigT& config ) const {
} for( std::size_t i = 0; i < tokens.size(); ++i ) {
ArgToken const& token = tokens[i];
private: typename std::vector<Opt>::const_iterator it = opts.begin(), itEnd = opts.end();
void ensureOptions() const { for(; it != itEnd; ++it ) {
if( m_allOptionParsers.size() != m_optionsByName.size() ) { Opt const& opt = *it;
m_optionsByName.clear();
typename std::vector<Opt<T> >::const_iterator it, itEnd = m_allOptionParsers.end(); if( ( token.type == ArgToken::ShortOpt && opt.hasShortName( token.arg ) ) ||
for( it = m_allOptionParsers.begin(); it != itEnd; ++it ) { ( token.type == ArgToken::LongOpt && opt.hasLongName( token.arg ) ) ) {
if( !it->shortName().empty() ) if( opt.takesArg() ) {
m_optionsByName.insert( std::make_pair( "-" + it->shortName(), &*it ) ); if( i == tokens.size()-1 || tokens[i+1].type != ArgToken::Positional )
if( !it->longName().empty() ) throw std::domain_error( "Expected argument to option " + token.arg );
m_optionsByName.insert( std::make_pair( "--" + it->longName(), &*it ) ); opt.boundField.set( config, tokens[++i].arg );
}
else {
opt.boundField.setFlag( config );
}
break;
}
else if( token.type == ArgToken::Positional && opt.isPositional() ) {
opt.boundField.set( config, token.arg );
break;
}
}
if( it == itEnd )
unhandledTokens.push_back( token );
} }
} }
}
template<typename U> std::vector<Opt> opts;
friend class Parser; mutable std::vector<ArgToken> unhandledTokens; // !TBD
};
std::vector<Opt<T> > m_allOptionParsers;
mutable std::map<std::string, Opt<T> const*> m_optionsByName;
std::vector<std::string> m_args;
std::vector<std::string> m_unusedOpts;
std::string m_separatorChars;
bool m_allowSpaceSeparator;
};
} // end namespace Catch } // end namespace Clara
struct TestOpt { struct TestOpt {
TestOpt() : number( 0 ), index( 0 ), flag( false ) {} TestOpt() : number( 0 ), index( 0 ), flag( false ) {}
@ -325,11 +395,12 @@ struct TestOpt2 {
TEST_CASE( "cmdline", "" ) { TEST_CASE( "cmdline", "" ) {
TestOpt config; TestOpt config;
Clara::Parser<TestOpt> parser; Clara::CommandLine<TestOpt> parser;
parser.addOption( "specifies output file" ) parser.bind( &TestOpt::fileName )
.shortName( "o" ) .describe( "specifies output file" )
.longName( "output" ) .shortOpt( "o" )
.optArg( "<filename>", &TestOpt::fileName ); .longOpt( "output" )
.argName( "<filename>" );
SECTION( "plain filename" ) { SECTION( "plain filename" ) {
const char* argv[] = { "test", "-o filename.ext" }; const char* argv[] = { "test", "-o filename.ext" };
@ -356,9 +427,9 @@ TEST_CASE( "cmdline", "" ) {
CHECK( config.fileName == "%stdout" ); CHECK( config.fileName == "%stdout" );
} }
parser.addOption( "a number" ) parser.bind( &TestOpt::number )
.shortName( "n" ) .shortOpt( "n" )
.optArg( "<an integral value>", &TestOpt::number ); .argName( "<an integral value>" );
SECTION( "a number" ) { SECTION( "a number" ) {
const char* argv[] = { "test", "-n 42" }; const char* argv[] = { "test", "-n 42" };
@ -377,27 +448,30 @@ TEST_CASE( "cmdline", "" ) {
TestOpt config1; TestOpt config1;
TestOpt2 config2; TestOpt2 config2;
Clara::Parser<TestOpt2> parser2; Clara::CommandLine<TestOpt2> parser2;
parser2.addOption( "description" ) parser2.bind( &TestOpt2::description )
.shortName( "d" ) .describe( "description" )
.longName( "description" ) .shortOpt( "d" )
.optArg( "<some text>", &TestOpt2::description ); .longOpt( "description" )
.argName( "<some text>" );
const char* argv[] = { "test", "-n 42", "-d some text" }; const char* argv[] = { "test", "-n 42", "-d some text" };
parser.parseInto( sizeof(argv)/sizeof(char*), argv, config1 ); parser.parseInto( sizeof(argv)/sizeof(char*), argv, config1 );
CHECK( config1.number == 42 ); CHECK( config1.number == 42 );
parser2.parseRemainingArgs( parser, config2 ); // !TBD
CHECK( config2.description == "some text" ); // parser2.parseRemainingArgs( parser, config2 );
// CHECK( config2.description == "some text" );
} }
SECTION( "methods" ) { SECTION( "methods" ) {
parser.addOption( "An index, which is an integer between 0 and 10, inclusive" ) parser.bind( &TestOpt::setValidIndex )
.shortName( "i" ) .describe( "An index, which is an integer between 0 and 10, inclusive" )
.optArg( "<index>", &TestOpt::setValidIndex ); .shortOpt( "i" )
.argName( "<index>" );
SECTION( "in range" ) { SECTION( "in range" ) {
const char* argv[] = { "test", "-i 3" }; const char* argv[] = { "test", "-i 3" };
@ -413,9 +487,9 @@ TEST_CASE( "cmdline", "" ) {
} }
SECTION( "flags" ) { SECTION( "flags" ) {
parser.addOption( "A flag" ) parser.bind( &TestOpt::flag )
.shortName( "f" ) .describe( "A flag" )
.flag( &TestOpt::flag ); .shortOpt( "f" );
SECTION( "set" ) { SECTION( "set" ) {
const char* argv[] = { "test", "-f" }; const char* argv[] = { "test", "-f" };
@ -453,79 +527,85 @@ struct Config {
TEST_CASE( "growing new Catch cli" ) { TEST_CASE( "growing new Catch cli" ) {
Clara::Parser<Config> parser; Clara::CommandLine<Config> parser;
Clara::Parser<Config> parser.bind( &Config::showHelp )
( "-h, --help display usage information", &Config::showHelp ) .describe( "display usage information" )
( "-l, --list list all (or matching) test cases", &Config::listTests ) .shortOpt( "?")
( "-t, --tags list all (or matching) tags", &Config::listTags ) .shortOpt( "h")
( "-p, --passing show passing test output", &Config::showPassingTests ) .longOpt( "help" );
( "-b, --break break into debugger on failure", &Config::breakIntoDebugger )
( "-e, --nothrow Skip exception tests", &Config::noThrow )
( "-o, --out <file name> output filename", &Config::fileName )
( "-n, --name <name> suite name", &Config::suiteName )
( "-a, --abort abort at first failure", &Config::abortAfterFirst )
( "-x, --abortx <number of failures> abort after x failures", &Config::abortAfterX )
( "-w, --warn <warning name> enables warnings", &Config::addWarning );
// .parseInto( argc, argv, config );
parser.addOption( "display usage information" ) parser.bind( &Config::listTests )
.shortName( "?") .describe( "list all (or matching) test cases" )
.shortName( "h") .shortOpt( "l")
.longName( "help" ) .longOpt( "list" );
.flag( &Config::showHelp );
parser.addOption( "list all (or matching) test cases" ) parser.bind( &Config::listTags )
.shortName( "l") .describe( "list all (or matching) tags" )
.longName( "list" ) .shortOpt( "t")
.flag( &Config::listTests ); .longOpt( "tags" );
parser.addOption( "list all (or matching) tags" ) parser.bind( &Config::showPassingTests )
.shortName( "t") .describe( "show passing test output" )
.longName( "tags" ) .shortOpt( "p")
.flag( &Config::listTags ); .longOpt( "passing" );
parser.addOption( "show passing test output" ) parser.bind( &Config::breakIntoDebugger )
.shortName( "p") .describe( "break into debugger on failure" )
.longName( "passing" ) .shortOpt( "b")
.flag( &Config::showPassingTests ); .longOpt( "break" );
parser.addOption( "break into debugger on failure" ) parser.bind( &Config::noThrow )
.shortName( "b") .describe( "Skip exception tests" )
.longName( "break" ) .shortOpt( "e")
.flag( &Config::breakIntoDebugger ); .longOpt( "nothrow" );
parser.addOption( "Skip exception tests" ) parser.bind( &Config::fileName )
.shortName( "e") .describe( "output filename" )
.longName( "nothrow" ) .shortOpt( "o")
.flag( &Config::noThrow ); .longOpt( "out" )
.argName( "file name" );
parser.addOption( "output filename" ) parser.bind( &Config::suiteName )
.shortName( "o") .describe( "suite name" )
.longName( "out" ) .shortOpt( "n")
.optArg( "<file name>", &Config::fileName ); .longOpt( "name" )
.argName( "name" );
parser.addOption( "suite name" ) parser.bind( &Config::abortAfterFirst )
.shortName( "n") .describe( "abort at first failure" )
.longName( "name" ) .shortOpt( "a")
.optArg( "<name>", &Config::suiteName ); .longOpt( "abort" );
parser.addOption( "abort at first failure" ) parser.bind( &Config::abortAfterX )
.shortName( "a") .describe( "abort after x failures" )
.longName( "abort" ) .shortOpt( "x")
.flag( &Config::abortAfterFirst ); .longOpt( "abortx" )
.argName( "number of failures" );
parser.addOption( "abort after x failures" ) parser.bind( &Config::addWarning )
.shortName( "x") .describe( "enables warnings" )
.longName( "abortx" ) .shortOpt( "w")
.optArg( "<number of failures>", &Config::abortAfterX ); .longOpt( "warn" )
.argName( "warning name" );
parser.addOption( "enables warnings" )
.shortName( "w")
.longName( "warn" )
.optArg( "<warning name>", &Config::addWarning );
std::cout << parser << std::endl; std::cout << parser << std::endl;
} }
// !TBD still support this?
// Clara::Parser<Config>
// ( "-h, --help display usage information", &Config::showHelp )
// ( "-l, --list list all (or matching) test cases", &Config::listTests )
// ( "-t, --tags list all (or matching) tags", &Config::listTags )
// ( "-p, --passing show passing test output", &Config::showPassingTests )
// ( "-b, --break break into debugger on failure", &Config::breakIntoDebugger )
// ( "-e, --nothrow Skip exception tests", &Config::noThrow )
// ( "-o, --out <file name> output filename", &Config::fileName )
// ( "-n, --name <name> suite name", &Config::suiteName )
// ( "-a, --abort abort at first failure", &Config::abortAfterFirst )
// ( "-x, --abortx <number of failures> abort after x failures", &Config::abortAfterX )
// ( "-w, --warn <warning name> enables warnings", &Config::addWarning );
// .parseInto( argc, argv, config );
#endif #endif