From 11e55161e1bd859ede9b5d67b6a7b436291fa5af Mon Sep 17 00:00:00 2001 From: Phil Nash Date: Tue, 7 May 2013 18:53:14 +0100 Subject: [PATCH] Some refactoring of Clara impl and experimental string based interface --- projects/SelfTest/CmdLineTests.cpp | 455 +++++++++++++++-------------- 1 file changed, 242 insertions(+), 213 deletions(-) diff --git a/projects/SelfTest/CmdLineTests.cpp b/projects/SelfTest/CmdLineTests.cpp index 55bf159f..98c0963a 100644 --- a/projects/SelfTest/CmdLineTests.cpp +++ b/projects/SelfTest/CmdLineTests.cpp @@ -14,51 +14,13 @@ namespace Clara { +template struct RemoveConstRef{ typedef T type; }; +template struct RemoveConstRef{ typedef T type; }; +template struct RemoveConstRef{ typedef T type; }; +template struct RemoveConstRef{ typedef T type; }; + using namespace Catch; -class ArgData { -public: - ArgData( std::string const& _arg ) : m_weight( 2 ) - { - std::size_t first = _arg.find( '<' ); - std::size_t last = _arg.find_last_of( '>' ); - if( first == std::string::npos || last == std::string::npos || last <= first+1 ) - throw std::logic_error( "Argument must contain a name in angle brackets but it was: " + _arg ); - m_prefix = _arg.substr( 0, first ); - m_postfix = _arg.substr( last+1 ); - m_name = _arg.substr( first+1, last-first-1 ); - if( !m_prefix.empty() ) - --m_weight; - if( !m_postfix.empty() ) - --m_weight; - } - std::string const& name() const { return m_name; } - std::string const& prefix() const { return m_prefix; } - std::string const& postfix() const { return m_postfix; } - - bool isMatch( std::string const& _arg ) const { - return startsWith( _arg, m_prefix ) && endsWith( _arg, m_postfix ); - } - std::string strip( std::string const& _arg ) const { - return _arg.substr( m_prefix.size(), - _arg.size() - m_prefix.size() - m_postfix.size() ); - } - bool operator < ( ArgData const& _other ) const { - return m_weight < _other.m_weight; - } - - friend std::ostream& operator << ( std::ostream& os, ArgData const& _arg ) { - os << _arg.m_prefix << "<" << _arg.m_name << ">" << _arg.m_postfix; - return os; - } - -protected: - std::string m_prefix; - std::string m_postfix; - std::string m_name; - int m_weight; -}; - template bool convertInto( std::string const& _source, T& _dest ) { std::stringstream ss; @@ -70,75 +32,88 @@ inline bool convertInto( std::string const& _source, std::string& _dest ) { _dest = _source; return true; } +inline bool convertInto( std::string const& _source, bool _dest ) { + std::string sourceLC = toLower( _source ); + if( sourceLC == "1" || sourceLC == "true" || sourceLC == "yes" || sourceLC == "on" ) + _dest = true; + else if( sourceLC == "0" || sourceLC == "false" || sourceLC == "no" || sourceLC == "off" ) + _dest = false; + else + return false; + return true; +} template class Opt { public: Opt( std::string const& _synposis ) : m_synopsis( _synposis ) {} - Opt& shortOpt( std::string const& _value ) { m_shortOpt = _value; return *this; } - Opt& longOpt( std::string const& _value ) { m_longOpt = _value; return *this; } + Opt& shortName( std::string const& _value ) { m_shortName = _value; return *this; } + Opt& longName( std::string const& _value ) { m_longName = _value; return *this; } template - Opt& addArg( std::string const& _name, M const& _member ){ - m_args.push_back( Arg( _name, _member ) ); + Opt& optArg( std::string const& _name, M const& _member ){ + m_argName = _name; + m_field = new Field( _member ); + return *this; + } + + template + Opt& flag( M const& _member ){ + m_field = new FlagField( _member ); return *this; } - std::size_t takesArg() const { return !m_args.empty(); } - + std::size_t takesArg() const { return !m_argName.empty(); } std::string synopsis() const { return m_synopsis; } - std::string shortOpt() const { return m_shortOpt; } - std::string longOpt() const { return m_longOpt; } + std::string shortName() const { return m_shortName; } + std::string longName() const { return m_longName; } bool parseInto( std::string const& _arg, T& _config ) const { - ensureWeightedArgsAreSorted(); - typename std::vector::const_iterator - it = m_argsInWeightedOrder.begin(), - itEnd = m_argsInWeightedOrder.end(); - for( ; it != itEnd; ++it ) - if( (*it)->isMatch( _arg ) ) { - (*it)->parseInto( _arg, _config ); - return true; - } - return false; + 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; - oss << *this; + writeToStream( oss ); return oss.str(); } friend std::ostream& operator << ( std::ostream& os, Opt const& _opt ) { - if( !_opt.m_shortOpt.empty() ) - os << "-" << _opt.m_shortOpt; - if( !_opt.m_longOpt.empty() ) - os << ", "; - if( !_opt.m_longOpt.empty() ) - os << "--" << _opt.m_longOpt; - if( !_opt.m_args.empty() ) { - os << " "; - typename std::vector::const_iterator - it = _opt.m_args.begin(), - itEnd = _opt.m_args.end(); - while( it != itEnd ) { - os << static_cast( *it ); - if( ++it!=itEnd ) - os << "|"; - } - } + _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& _arg, T& _config ) const = 0; + 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 struct Field; + template + struct FlagField; + + // Data member : with argument template struct Field : IField { Field( M C::* _member ) : member( _member ) {} @@ -147,11 +122,22 @@ private: } M C::* member; }; + // Data member : flag + template + struct FlagField : IField { + FlagField( M C::* _member ) : member( _member ) {} + void set( T& _config ) const { + _config.*member = true; + } + M C::* member; + }; + + // Unary method : with argument template struct Field : IField { Field( void (C::*_method)( M ) ) : method( _method ) {} bool parseInto( std::string const& _arg, T& _config ) const { - M value; + typename RemoveConstRef::type value; if( !convertInto( _arg, value ) ) return false; ( _config.*method )( value ); @@ -159,42 +145,32 @@ private: } void (C::*method)( M ); }; - - class Arg : public ArgData { - public: - Arg() : m_field( NULL ) {} - template - Arg( std::string const& _name, M const& _member ) - : ArgData( _name ), - m_field( new Field( _member ) ) - {} - void parseInto( std::string const& _arg, T& _config ) const { - if( !m_field->parseInto( strip( _arg ), _config ) ) - throw std::domain_error( "'" + _arg + "' was not valid for <" + m_name + ">" ); + + // Unary method : flag + template + struct FlagField : IField { + FlagField( void (C::*_method)( M ) ) : method( _method ) {} + void set( T& _config ) const { + ( _config.*method )( true ); } - - private: - Ptr m_field; + void (C::*method)( M ); + }; + + // Nullary method : flag + template + struct FlagField : IField { + FlagField( void (C::*_method)() ) : method( _method ) {} + void set( T& _config ) const { + ( _config.*method )(); + } + void (C::*method)(); }; - static bool argLess( Arg const* lhs, Arg const* rhs ) { - return *lhs < *rhs; - } - void ensureWeightedArgsAreSorted() const { - if( m_args.size() > m_argsInWeightedOrder.size() ) { - m_argsInWeightedOrder.clear(); - typename std::vector::const_iterator it = m_args.begin(), - itEnd = m_args.end(); - for( ; it != itEnd; ++it ) - m_argsInWeightedOrder.push_back( &*it ); - sort( m_argsInWeightedOrder.begin(), m_argsInWeightedOrder.end(), &Opt::argLess ); - } - } std::string m_synopsis; - std::string m_shortOpt; - std::string m_longOpt; - std::vector m_args; - mutable std::vector m_argsInWeightedOrder; + std::string m_shortName; + std::string m_longName; + std::string m_argName; + Ptr m_field; }; template @@ -205,27 +181,34 @@ public: : m_separatorChars( "=: " ), m_allowSpaceSeparator( m_separatorChars.find( ' ' ) != std::string::npos ) {} + template + Parser( std::string const&, M ) // !TBD + : m_separatorChars( "=: " ), + m_allowSpaceSeparator( m_separatorChars.find( ' ' ) != std::string::npos ) + {} + template + Parser& operator()( std::string const&, M ) { return *this; } // !TBD Opt& addOption( std::string const& _synposis ) { m_allOptionParsers.push_back( _synposis ); return m_allOptionParsers.back(); } - void parseArgs( int argc, const char* const argv[], T& _config ) { + void parseInto( int argc, const char* const argv[], T& _config ) { std::vector args; args.reserve( static_cast( argc ) ); for( int i = 0; i < argc; ++i ) args.push_back( argv[i] ); - parseArgs( args, _config ); + parseInto( args, _config ); } template void parseRemainingArgs( Parser& _parser, T& _config ) { - parseArgs( _parser.m_unusedOpts, _config ); + parseInto( _parser.m_unusedOpts, _config ); } - void parseArgs( std::vector const& _args, T& _config ) { + void parseInto( std::vector const& _args, T& _config ) { ensureOptions(); for( std::size_t i = 0; i < _args.size(); ++i ) { std::string const& arg = _args[i]; @@ -297,10 +280,10 @@ private: m_optionsByName.clear(); typename std::vector >::const_iterator it, itEnd = m_allOptionParsers.end(); for( it = m_allOptionParsers.begin(); it != itEnd; ++it ) { - if( !it->shortOpt().empty() ) - m_optionsByName.insert( std::make_pair( "-" + it->shortOpt(), &*it ) ); - if( !it->longOpt().empty() ) - m_optionsByName.insert( std::make_pair( "--" + it->longOpt(), &*it ) ); + if( !it->shortName().empty() ) + m_optionsByName.insert( std::make_pair( "-" + it->shortName(), &*it ) ); + if( !it->longName().empty() ) + m_optionsByName.insert( std::make_pair( "--" + it->longName(), &*it ) ); } } } @@ -319,12 +302,12 @@ private: } // end namespace Catch struct TestOpt { - TestOpt() : number( 0 ), index( 0 ) {} + TestOpt() : number( 0 ), index( 0 ), flag( false ) {} std::string fileName; - std::string streamName; int number; int index; + bool flag; void setValidIndex( int i ) { if( i < 0 || i > 10 ) @@ -337,120 +320,54 @@ struct TestOpt2 { std::string description; }; -TEST_CASE( "Arg" ) { - SECTION( "pre and post" ) { - Clara::ArgData preAndPost( "prefixpostfix" ); - CHECK( preAndPost.prefix() == "prefix" ); - CHECK( preAndPost.postfix() == "postfix" ); - CHECK( preAndPost.name() == "arg" ); - - CHECK( preAndPost.isMatch( "prefixpayloadpostfix" ) ); - CHECK( preAndPost.strip( "prefixpayloadpostfix" ) == "payload" ); - CHECK_FALSE( preAndPost.isMatch( "payload" ) ); - CHECK_FALSE( preAndPost.isMatch( "postfixpayloadpostfix" ) ); - CHECK_FALSE( preAndPost.isMatch( "prefixpayloadpostfixx" ) ); - } - SECTION( "pre" ) { - Clara::ArgData preAndPost( "prefix" ); - CHECK( preAndPost.prefix() == "prefix" ); - CHECK( preAndPost.postfix() == "" ); - CHECK( preAndPost.name() == "arg" ); - - CHECK( preAndPost.isMatch( "prefixpayload" ) ); - CHECK( preAndPost.strip( "prefixpayload" ) == "payload" ); - CHECK_FALSE( preAndPost.isMatch( "payload" ) ); - CHECK_FALSE( preAndPost.isMatch( "postfixpayload" ) ); - } - SECTION( "post" ) { - Clara::ArgData preAndPost( "postfix" ); - CHECK( preAndPost.prefix() == "" ); - CHECK( preAndPost.postfix() == "postfix" ); - CHECK( preAndPost.name() == "arg" ); - - CHECK( preAndPost.isMatch( "payloadpostfix" ) ); - CHECK( preAndPost.strip( "payloadpostfix" ) == "payload" ); - CHECK_FALSE( preAndPost.isMatch( "payload" ) ); - CHECK_FALSE( preAndPost.isMatch( "payloadpostfixx" ) ); - } - SECTION( "none" ) { - Clara::ArgData preAndPost( "" ); - CHECK( preAndPost.prefix() == "" ); - CHECK( preAndPost.postfix() == "" ); - CHECK( preAndPost.name() == "arg" ); - - CHECK( preAndPost.isMatch( "payload" ) ); - CHECK( preAndPost.strip( "payload" ) == "payload" ); - } - SECTION( "errors" ) { - CHECK_THROWS( Clara::ArgData( "" ) ); - CHECK_THROWS( Clara::ArgData( "no brackets" ) ); - CHECK_THROWS( Clara::ArgData( "" ) ); - CHECK_THROWS( Clara::ArgData( "><" ) ); - CHECK_THROWS( Clara::ArgData( "<>" ) ); - } -} - TEST_CASE( "cmdline", "" ) { TestOpt config; Clara::Parser parser; parser.addOption( "specifies output file" ) - .shortOpt( "o" ) - .longOpt( "output" ) - .addArg( "", &TestOpt::fileName ) - .addArg( "%", &TestOpt::streamName ); + .shortName( "o" ) + .longName( "output" ) + .optArg( "", &TestOpt::fileName ); SECTION( "plain filename" ) { const char* argv[] = { "test", "-o filename.ext" }; - parser.parseArgs( sizeof(argv)/sizeof(char*), argv, config ); + parser.parseInto( sizeof(argv)/sizeof(char*), argv, config ); CHECK( config.fileName == "filename.ext" ); - CHECK( config.streamName == "" ); } SECTION( "plain filename with colon" ) { const char* argv[] = { "test", "-o:filename.ext" }; - parser.parseArgs( sizeof(argv)/sizeof(char*), argv, config ); + parser.parseInto( sizeof(argv)/sizeof(char*), argv, config ); CHECK( config.fileName == "filename.ext" ); - CHECK( config.streamName == "" ); } SECTION( "plain filename with =" ) { const char* argv[] = { "test", "-o=filename.ext" }; - parser.parseArgs( sizeof(argv)/sizeof(char*), argv, config ); + parser.parseInto( sizeof(argv)/sizeof(char*), argv, config ); CHECK( config.fileName == "filename.ext" ); - CHECK( config.streamName == "" ); - } - SECTION( "stream name" ) { - const char* argv[] = { "test", "-o %stdout" }; - - parser.parseArgs( sizeof(argv)/sizeof(char*), argv, config ); - CHECK( config.fileName == "" ); - CHECK( config.streamName == "stdout" ); } SECTION( "long opt" ) { const char* argv[] = { "test", "--output %stdout" }; - parser.parseArgs( sizeof(argv)/sizeof(char*), argv, config ); - CHECK( config.fileName == "" ); - CHECK( config.streamName == "stdout" ); + parser.parseInto( sizeof(argv)/sizeof(char*), argv, config ); + CHECK( config.fileName == "%stdout" ); } parser.addOption( "a number" ) - .shortOpt( "n" ) - .addArg( "", &TestOpt::number ); + .shortName( "n" ) + .optArg( "", &TestOpt::number ); SECTION( "a number" ) { const char* argv[] = { "test", "-n 42" }; - parser.parseArgs( sizeof(argv)/sizeof(char*), argv, config ); + parser.parseInto( sizeof(argv)/sizeof(char*), argv, config ); CHECK( config.number == 42 ); } SECTION( "not a number" ) { const char* argv[] = { "test", "-n forty-two" }; - CHECK_THROWS( parser.parseArgs( sizeof(argv)/sizeof(char*), argv, config ) ); + CHECK_THROWS( parser.parseInto( sizeof(argv)/sizeof(char*), argv, config ) ); CHECK( config.number == 0 ); } @@ -461,13 +378,13 @@ TEST_CASE( "cmdline", "" ) { Clara::Parser parser2; parser2.addOption( "description" ) - .shortOpt( "d" ) - .longOpt( "description" ) - .addArg( "", &TestOpt2::description ); + .shortName( "d" ) + .longName( "description" ) + .optArg( "", &TestOpt2::description ); const char* argv[] = { "test", "-n 42", "-d some text" }; - parser.parseArgs( sizeof(argv)/sizeof(char*), argv, config1 ); + parser.parseInto( sizeof(argv)/sizeof(char*), argv, config1 ); CHECK( config1.number == 42 ); parser2.parseRemainingArgs( parser, config2 ); @@ -477,22 +394,134 @@ TEST_CASE( "cmdline", "" ) { SECTION( "methods" ) { parser.addOption( "An index, which is an integer between 0 and 10, inclusive" ) - .shortOpt( "i" ) - .addArg( "", &TestOpt::setValidIndex ); + .shortName( "i" ) + .optArg( "", &TestOpt::setValidIndex ); SECTION( "in range" ) { const char* argv[] = { "test", "-i 3" }; - parser.parseArgs( sizeof(argv)/sizeof(char*), argv, config ); + parser.parseInto( sizeof(argv)/sizeof(char*), argv, config ); REQUIRE( config.index == 3 ); } SECTION( "out of range" ) { const char* argv[] = { "test", "-i 42" }; - REQUIRE_THROWS( parser.parseArgs( sizeof(argv)/sizeof(char*), argv, config ) ); + REQUIRE_THROWS( parser.parseInto( sizeof(argv)/sizeof(char*), argv, config ) ); } - - std::cout << parser << std::endl; } + SECTION( "flags" ) { + parser.addOption( "A flag" ) + .shortName( "f" ) + .flag( &TestOpt::flag ); + + SECTION( "set" ) { + const char* argv[] = { "test", "-f" }; + + parser.parseInto( sizeof(argv)/sizeof(char*), argv, config ); + REQUIRE( config.flag ); + } + SECTION( "not set" ) { + const char* argv[] = { "test" }; + + parser.parseInto( sizeof(argv)/sizeof(char*), argv, config ); + REQUIRE( config.flag == false ); + } + } +} + +struct Config { + bool listTests; + bool listTags; + bool showPassingTests; + bool breakIntoDebugger; + bool noThrow; + bool showHelp; + + int abortAfter; + std::string reporterName; + std::string fileName; + std::string suiteName; + std::vector warnings; + + void abortAfterFirst() { abortAfter = 1; } + void abortAfterX( int x ) { abortAfter = x; } + void addWarning( std::string const& _warning ) { warnings.push_back( _warning ); } +}; + + +TEST_CASE( "growing new Catch cli" ) { + Clara::Parser parser; + + Clara::Parser + ( "-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 output filename", &Config::fileName ) + ( "-n, --name suite name", &Config::suiteName ) + ( "-a, --abort abort at first failure", &Config::abortAfterFirst ) + ( "-x, --abortx abort after x failures", &Config::abortAfterX ) + ( "-w, --warn enables warnings", &Config::addWarning ); +// .parseInto( argc, argv, config ); + + parser.addOption( "display usage information" ) + .shortName( "?") + .shortName( "h") + .longName( "help" ) + .flag( &Config::showHelp ); + + parser.addOption( "list all (or matching) test cases" ) + .shortName( "l") + .longName( "list" ) + .flag( &Config::listTests ); + + parser.addOption( "list all (or matching) tags" ) + .shortName( "t") + .longName( "tags" ) + .flag( &Config::listTags ); + + parser.addOption( "show passing test output" ) + .shortName( "p") + .longName( "passing" ) + .flag( &Config::showPassingTests ); + + parser.addOption( "break into debugger on failure" ) + .shortName( "b") + .longName( "break" ) + .flag( &Config::breakIntoDebugger ); + + parser.addOption( "Skip exception tests" ) + .shortName( "e") + .longName( "nothrow" ) + .flag( &Config::noThrow ); + + parser.addOption( "output filename" ) + .shortName( "o") + .longName( "out" ) + .optArg( "", &Config::fileName ); + + parser.addOption( "suite name" ) + .shortName( "n") + .longName( "name" ) + .optArg( "", &Config::suiteName ); + + parser.addOption( "abort at first failure" ) + .shortName( "a") + .longName( "abort" ) + .flag( &Config::abortAfterFirst ); + + parser.addOption( "abort after x failures" ) + .shortName( "x") + .longName( "abortx" ) + .optArg( "", &Config::abortAfterX ); + + parser.addOption( "enables warnings" ) + .shortName( "w") + .longName( "warn" ) + .optArg( "", &Config::addWarning ); + + std::cout << parser << std::endl; }