diff --git a/README.md b/README.md index 59325a4e..dba8e0c2 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ ![catch logo](catch-logo-small.png) -*v1.0 build 20 (master branch)* +*v1.0 build 23 (master branch)* Build status (on Travis CI) [![Build Status](https://travis-ci.org/philsquared/Catch.png)](https://travis-ci.org/philsquared/Catch) diff --git a/include/catch_runner.hpp b/include/catch_runner.hpp index 52a475e8..f2495e56 100644 --- a/include/catch_runner.hpp +++ b/include/catch_runner.hpp @@ -144,9 +144,8 @@ namespace Catch { int applyCommandLine( int argc, char* const argv[], OnUnusedOptions::DoWhat unusedOptionBehaviour = OnUnusedOptions::Fail ) { try { + m_cli.setThrowOnUnrecognisedTokens( unusedOptionBehaviour == OnUnusedOptions::Fail ); m_unusedTokens = m_cli.parseInto( argc, argv, m_configData ); - if( unusedOptionBehaviour == OnUnusedOptions::Fail ) - enforceNoUsedTokens(); if( m_configData.showHelp ) showHelp( m_configData.processName ); m_config.reset(); @@ -154,7 +153,7 @@ namespace Catch { catch( std::exception& ex ) { { Colour colourGuard( Colour::Red ); - std::cerr << "\nError in input:\n" + std::cerr << "\nError(s) in input:\n" << Text( ex.what(), TextAttributes().setIndent(2) ) << "\n\n"; } @@ -169,18 +168,6 @@ namespace Catch { m_config.reset(); } - void enforceNoUsedTokens() const { - if( !m_unusedTokens.empty() ) { - std::vector::const_iterator - it = m_unusedTokens.begin(), - itEnd = m_unusedTokens.end(); - std::string msg; - for(; it != itEnd; ++it ) - msg += " unrecognised option: " + it->data + "\n"; - throw std::runtime_error( msg.substr( 0, msg.size()-1 ) ); - } - } - int run( int argc, char* const argv[] ) { int returnCode = applyCommandLine( argc, argv ); diff --git a/include/internal/catch_approx.hpp b/include/internal/catch_approx.hpp index 67dba619..4370ab3e 100644 --- a/include/internal/catch_approx.hpp +++ b/include/internal/catch_approx.hpp @@ -70,7 +70,7 @@ namespace Detail { std::string toString() const { std::ostringstream oss; - oss << "Approx( " << m_value << " )"; + oss << "Approx( " << Catch::toString( m_value ) << " )"; return oss.str(); } diff --git a/include/internal/catch_version.hpp b/include/internal/catch_version.hpp index 8cf1cabc..4542254c 100644 --- a/include/internal/catch_version.hpp +++ b/include/internal/catch_version.hpp @@ -14,7 +14,7 @@ namespace Catch { // These numbers are maintained by a script template - const T LibraryVersionInfo::value( 1, 0, 20, "master" ); + const T LibraryVersionInfo::value( 1, 0, 23, "master" ); } #endif // TWOBLUECUBES_CATCH_VERSION_HPP_INCLUDED diff --git a/include/internal/clara.h b/include/internal/clara.h index a401edd8..b44d129a 100644 --- a/include/internal/clara.h +++ b/include/internal/clara.h @@ -206,7 +206,8 @@ namespace Clara { }; void parseIntoTokens( int argc, char const * const * argv, std::vector& tokens ) const { - for( int i = 1; i < argc; ++i ) + const std::string doubleDash = "--"; + for( int i = 1; i < argc && argv[i] != doubleDash; ++i ) parseIntoTokens( argv[i] , tokens); } void parseIntoTokens( std::string arg, std::vector& tokens ) const { @@ -371,18 +372,25 @@ namespace Clara { CommandLine() : m_boundProcessName( new Detail::NullBinder() ), - m_highestSpecifiedArgPosition( 0 ) + m_highestSpecifiedArgPosition( 0 ), + m_throwOnUnrecognisedTokens( false ) {} CommandLine( CommandLine const& other ) : m_boundProcessName( other.m_boundProcessName ), m_options ( other.m_options ), m_positionalArgs( other.m_positionalArgs ), - m_highestSpecifiedArgPosition( other.m_highestSpecifiedArgPosition ) + m_highestSpecifiedArgPosition( other.m_highestSpecifiedArgPosition ), + m_throwOnUnrecognisedTokens( other.m_throwOnUnrecognisedTokens ) { if( other.m_arg.get() ) m_arg = ArgAutoPtr( new Arg( *other.m_arg ) ); } + CommandLine& setThrowOnUnrecognisedTokens( bool shouldThrow = true ) { + m_throwOnUnrecognisedTokens = shouldThrow; + return *this; + } + template ArgBinder bind( F f ) { ArgBinder binder( this, f ); @@ -488,6 +496,7 @@ namespace Clara { std::vector populateOptions( std::vector const& tokens, ConfigT& config ) const { std::vector unusedTokens; + std::vector errors; for( std::size_t i = 0; i < tokens.size(); ++i ) { Parser::Token const& token = tokens[i]; typename std::vector::const_iterator it = m_options.begin(), itEnd = m_options.end(); @@ -499,8 +508,9 @@ namespace Clara { ( token.type == Parser::Token::LongOpt && arg.hasLongName( token.data ) ) ) { if( arg.takesArg() ) { if( i == tokens.size()-1 || tokens[i+1].type != Parser::Token::Positional ) - throw std::domain_error( "Expected argument to option " + token.data ); - arg.boundField.set( config, tokens[++i].data ); + errors.push_back( "Expected argument to option: " + token.data ); + else + arg.boundField.set( config, tokens[++i].data ); } else { arg.boundField.setFlag( config ); @@ -509,11 +519,26 @@ namespace Clara { } } catch( std::exception& ex ) { - throw std::runtime_error( std::string( ex.what() ) + "\n- while parsing: (" + arg.commands() + ")" ); + errors.push_back( std::string( ex.what() ) + "\n- while parsing: (" + arg.commands() + ")" ); } } - if( it == itEnd ) - unusedTokens.push_back( token ); + if( it == itEnd ) { + if( token.type == Parser::Token::Positional || !m_throwOnUnrecognisedTokens ) + unusedTokens.push_back( token ); + else if( m_throwOnUnrecognisedTokens ) + errors.push_back( "unrecognised option: " + token.data ); + } + } + if( !errors.empty() ) { + std::ostringstream oss; + for( std::vector::const_iterator it = errors.begin(), itEnd = errors.end(); + it != itEnd; + ++it ) { + if( it != errors.begin() ) + oss << "\n"; + oss << *it; + } + throw std::runtime_error( oss.str() ); } return unusedTokens; } @@ -552,6 +577,7 @@ namespace Clara { std::map m_positionalArgs; ArgAutoPtr m_arg; int m_highestSpecifiedArgPosition; + bool m_throwOnUnrecognisedTokens; }; } // end namespace Clara diff --git a/projects/SelfTest/Baselines/console.std.approved.txt b/projects/SelfTest/Baselines/console.std.approved.txt index 54d3ee6f..52b4db55 100644 --- a/projects/SelfTest/Baselines/console.std.approved.txt +++ b/projects/SelfTest/Baselines/console.std.approved.txt @@ -49,22 +49,22 @@ with expansion: ConditionTests.cpp:: FAILED: CHECK( data.float_nine_point_one == Approx( 9.11f ) ) with expansion: - 9.1 == Approx( 9.11 ) + 9.1 == Approx( 9.1099996567 ) ConditionTests.cpp:: FAILED: CHECK( data.float_nine_point_one == Approx( 9.0f ) ) with expansion: - 9.1 == Approx( 9 ) + 9.1 == Approx( 9.0 ) ConditionTests.cpp:: FAILED: CHECK( data.float_nine_point_one == Approx( 1 ) ) with expansion: - 9.1 == Approx( 1 ) + 9.1 == Approx( 1.0 ) ConditionTests.cpp:: FAILED: CHECK( data.float_nine_point_one == Approx( 0 ) ) with expansion: - 9.1 == Approx( 0 ) + 9.1 == Approx( 0.0 ) ConditionTests.cpp:: FAILED: CHECK( data.double_pi == Approx( 3.1415 ) ) @@ -110,12 +110,12 @@ with expansion: ConditionTests.cpp:: FAILED: CHECK( data.float_nine_point_one != Approx( 9.1f ) ) with expansion: - 9.1 != Approx( 9.1 ) + 9.1 != Approx( 9.1000003815 ) ConditionTests.cpp:: FAILED: CHECK( data.double_pi != Approx( 3.1415926535 ) ) with expansion: - 3.1415926535 != Approx( 3.14159 ) + 3.1415926535 != Approx( 3.1415926535 ) ConditionTests.cpp:: FAILED: CHECK( data.str_hello != "hello" ) @@ -569,8 +569,8 @@ with expansion: with message: Testing if fib[7] (21) is even -Some information -An error +A string sent directly to stdout +A string sent directly to stderr ------------------------------------------------------------------------------- checkedIf, failing ------------------------------------------------------------------------------- diff --git a/projects/SelfTest/Baselines/console.sw.approved.txt b/projects/SelfTest/Baselines/console.sw.approved.txt index e0c15c68..81ee03db 100644 --- a/projects/SelfTest/Baselines/console.sw.approved.txt +++ b/projects/SelfTest/Baselines/console.sw.approved.txt @@ -73,13 +73,13 @@ ApproxTests.cpp:: PASSED: REQUIRE( 1.23f == Approx( 1.23f ) ) with expansion: - 1.23 == Approx( 1.23 ) + 1.23 == Approx( 1.2300000191 ) ApproxTests.cpp:: PASSED: REQUIRE( 0.0f == Approx( 0.0f ) ) with expansion: - 0 == Approx( 0 ) + 0 == Approx( 0.0 ) ------------------------------------------------------------------------------- Approximate comparisons with ints @@ -90,10 +90,14 @@ ApproxTests.cpp: ApproxTests.cpp:: PASSED: REQUIRE( 1 == Approx( 1 ) ) +with expansion: + 1 == Approx( 1.0 ) ApproxTests.cpp:: PASSED: REQUIRE( 0 == Approx( 0 ) ) +with expansion: + 0 == Approx( 0.0 ) ------------------------------------------------------------------------------- Approximate comparisons with mixed numeric types @@ -105,19 +109,19 @@ ApproxTests.cpp:: PASSED: REQUIRE( 1.0f == Approx( 1 ) ) with expansion: - 1 == Approx( 1 ) + 1 == Approx( 1.0 ) ApproxTests.cpp:: PASSED: REQUIRE( 0 == Approx( dZero) ) with expansion: - 0 == Approx( 0 ) + 0 == Approx( 0.0 ) ApproxTests.cpp:: PASSED: REQUIRE( 0 == Approx( dSmall ).epsilon( 0.001 ) ) with expansion: - 0 == Approx( 1e-05 ) + 0 == Approx( 0.00001 ) ApproxTests.cpp:: PASSED: @@ -129,7 +133,7 @@ ApproxTests.cpp:: PASSED: REQUIRE( dMedium == Approx( 1.234f ) ) with expansion: - 1.234 == Approx( 1.234 ) + 1.234 == Approx( 1.2339999676 ) ------------------------------------------------------------------------------- Use a custom approx @@ -265,13 +269,13 @@ ConditionTests.cpp:: PASSED: REQUIRE( data.float_nine_point_one == Approx( 9.1f ) ) with expansion: - 9.1 == Approx( 9.1 ) + 9.1 == Approx( 9.1000003815 ) ConditionTests.cpp:: PASSED: REQUIRE( data.double_pi == Approx( 3.1415926535 ) ) with expansion: - 3.1415926535 == Approx( 3.14159 ) + 3.1415926535 == Approx( 3.1415926535 ) ConditionTests.cpp:: PASSED: @@ -321,22 +325,22 @@ with expansion: ConditionTests.cpp:: FAILED: CHECK( data.float_nine_point_one == Approx( 9.11f ) ) with expansion: - 9.1 == Approx( 9.11 ) + 9.1 == Approx( 9.1099996567 ) ConditionTests.cpp:: FAILED: CHECK( data.float_nine_point_one == Approx( 9.0f ) ) with expansion: - 9.1 == Approx( 9 ) + 9.1 == Approx( 9.0 ) ConditionTests.cpp:: FAILED: CHECK( data.float_nine_point_one == Approx( 1 ) ) with expansion: - 9.1 == Approx( 1 ) + 9.1 == Approx( 1.0 ) ConditionTests.cpp:: FAILED: CHECK( data.float_nine_point_one == Approx( 0 ) ) with expansion: - 9.1 == Approx( 0 ) + 9.1 == Approx( 0.0 ) ConditionTests.cpp:: FAILED: CHECK( data.double_pi == Approx( 3.1415 ) ) @@ -390,25 +394,25 @@ ConditionTests.cpp:: PASSED: REQUIRE( data.float_nine_point_one != Approx( 9.11f ) ) with expansion: - 9.1 != Approx( 9.11 ) + 9.1 != Approx( 9.1099996567 ) ConditionTests.cpp:: PASSED: REQUIRE( data.float_nine_point_one != Approx( 9.0f ) ) with expansion: - 9.1 != Approx( 9 ) + 9.1 != Approx( 9.0 ) ConditionTests.cpp:: PASSED: REQUIRE( data.float_nine_point_one != Approx( 1 ) ) with expansion: - 9.1 != Approx( 1 ) + 9.1 != Approx( 1.0 ) ConditionTests.cpp:: PASSED: REQUIRE( data.float_nine_point_one != Approx( 0 ) ) with expansion: - 9.1 != Approx( 0 ) + 9.1 != Approx( 0.0 ) ConditionTests.cpp:: PASSED: @@ -454,12 +458,12 @@ with expansion: ConditionTests.cpp:: FAILED: CHECK( data.float_nine_point_one != Approx( 9.1f ) ) with expansion: - 9.1 != Approx( 9.1 ) + 9.1 != Approx( 9.1000003815 ) ConditionTests.cpp:: FAILED: CHECK( data.double_pi != Approx( 3.1415926535 ) ) with expansion: - 3.1415926535 != Approx( 3.14159 ) + 3.1415926535 != Approx( 3.1415926535 ) ConditionTests.cpp:: FAILED: CHECK( data.str_hello != "hello" ) @@ -3014,8 +3018,8 @@ with expansion: with message: Testing if fib[7] (21) is even -Some information -An error +A string sent directly to stdout +A string sent directly to stderr ------------------------------------------------------------------------------- Sends stuff to stdout and stderr ------------------------------------------------------------------------------- diff --git a/projects/SelfTest/Baselines/console.swa4.approved.txt b/projects/SelfTest/Baselines/console.swa4.approved.txt index 65a96d7d..e701d91a 100644 --- a/projects/SelfTest/Baselines/console.swa4.approved.txt +++ b/projects/SelfTest/Baselines/console.swa4.approved.txt @@ -73,13 +73,13 @@ ApproxTests.cpp:: PASSED: REQUIRE( 1.23f == Approx( 1.23f ) ) with expansion: - 1.23 == Approx( 1.23 ) + 1.23 == Approx( 1.2300000191 ) ApproxTests.cpp:: PASSED: REQUIRE( 0.0f == Approx( 0.0f ) ) with expansion: - 0 == Approx( 0 ) + 0 == Approx( 0.0 ) ------------------------------------------------------------------------------- Approximate comparisons with ints @@ -90,10 +90,14 @@ ApproxTests.cpp: ApproxTests.cpp:: PASSED: REQUIRE( 1 == Approx( 1 ) ) +with expansion: + 1 == Approx( 1.0 ) ApproxTests.cpp:: PASSED: REQUIRE( 0 == Approx( 0 ) ) +with expansion: + 0 == Approx( 0.0 ) ------------------------------------------------------------------------------- Approximate comparisons with mixed numeric types @@ -105,19 +109,19 @@ ApproxTests.cpp:: PASSED: REQUIRE( 1.0f == Approx( 1 ) ) with expansion: - 1 == Approx( 1 ) + 1 == Approx( 1.0 ) ApproxTests.cpp:: PASSED: REQUIRE( 0 == Approx( dZero) ) with expansion: - 0 == Approx( 0 ) + 0 == Approx( 0.0 ) ApproxTests.cpp:: PASSED: REQUIRE( 0 == Approx( dSmall ).epsilon( 0.001 ) ) with expansion: - 0 == Approx( 1e-05 ) + 0 == Approx( 0.00001 ) ApproxTests.cpp:: PASSED: @@ -129,7 +133,7 @@ ApproxTests.cpp:: PASSED: REQUIRE( dMedium == Approx( 1.234f ) ) with expansion: - 1.234 == Approx( 1.234 ) + 1.234 == Approx( 1.2339999676 ) ------------------------------------------------------------------------------- Use a custom approx @@ -265,13 +269,13 @@ ConditionTests.cpp:: PASSED: REQUIRE( data.float_nine_point_one == Approx( 9.1f ) ) with expansion: - 9.1 == Approx( 9.1 ) + 9.1 == Approx( 9.1000003815 ) ConditionTests.cpp:: PASSED: REQUIRE( data.double_pi == Approx( 3.1415926535 ) ) with expansion: - 3.1415926535 == Approx( 3.14159 ) + 3.1415926535 == Approx( 3.1415926535 ) ConditionTests.cpp:: PASSED: diff --git a/projects/SelfTest/Baselines/junit.sw.approved.txt b/projects/SelfTest/Baselines/junit.sw.approved.txt index b105e986..e28449ca 100644 --- a/projects/SelfTest/Baselines/junit.sw.approved.txt +++ b/projects/SelfTest/Baselines/junit.sw.approved.txt @@ -30,16 +30,16 @@ ConditionTests.cpp: ConditionTests.cpp: - + ConditionTests.cpp: - + ConditionTests.cpp: - + ConditionTests.cpp: - + ConditionTests.cpp: @@ -66,10 +66,10 @@ ConditionTests.cpp: ConditionTests.cpp: - + ConditionTests.cpp: - + ConditionTests.cpp: @@ -351,10 +351,10 @@ MiscTests.cpp: -Some information +A string sent directly to stdout -An error +A string sent directly to stderr @@ -550,12 +550,12 @@ TrickyTests.cpp: Message from section one Message from section two -Some information +A string sent directly to stdout hello hello -An error +A string sent directly to stderr diff --git a/projects/SelfTest/Baselines/xml.sw.approved.txt b/projects/SelfTest/Baselines/xml.sw.approved.txt index dda010b1..9bebc628 100644 --- a/projects/SelfTest/Baselines/xml.sw.approved.txt +++ b/projects/SelfTest/Baselines/xml.sw.approved.txt @@ -76,7 +76,7 @@ 1.23f == Approx( 1.23f ) - 1.23 == Approx( 1.23 ) + 1.23 == Approx( 1.2300000191 ) @@ -84,7 +84,7 @@ 0.0f == Approx( 0.0f ) - 0 == Approx( 0 ) + 0 == Approx( 0.0 ) @@ -95,7 +95,7 @@ 1 == Approx( 1 ) - 1 == Approx( 1 ) + 1 == Approx( 1.0 ) @@ -103,7 +103,7 @@ 0 == Approx( 0 ) - 0 == Approx( 0 ) + 0 == Approx( 0.0 ) @@ -114,7 +114,7 @@ 1.0f == Approx( 1 ) - 1 == Approx( 1 ) + 1 == Approx( 1.0 ) @@ -122,7 +122,7 @@ 0 == Approx( dZero) - 0 == Approx( 0 ) + 0 == Approx( 0.0 ) @@ -130,7 +130,7 @@ 0 == Approx( dSmall ).epsilon( 0.001 ) - 0 == Approx( 1e-05 ) + 0 == Approx( 0.00001 ) @@ -146,7 +146,7 @@ dMedium == Approx( 1.234f ) - 1.234 == Approx( 1.234 ) + 1.234 == Approx( 1.2339999676 ) @@ -295,7 +295,7 @@ data.float_nine_point_one == Approx( 9.1f ) - 9.1 == Approx( 9.1 ) + 9.1 == Approx( 9.1000003815 ) @@ -303,7 +303,7 @@ data.double_pi == Approx( 3.1415926535 ) - 3.1415926535 == Approx( 3.14159 ) + 3.1415926535 == Approx( 3.1415926535 ) @@ -370,7 +370,7 @@ data.float_nine_point_one == Approx( 9.11f ) - 9.1 == Approx( 9.11 ) + 9.1 == Approx( 9.1099996567 ) @@ -378,7 +378,7 @@ data.float_nine_point_one == Approx( 9.0f ) - 9.1 == Approx( 9 ) + 9.1 == Approx( 9.0 ) @@ -386,7 +386,7 @@ data.float_nine_point_one == Approx( 1 ) - 9.1 == Approx( 1 ) + 9.1 == Approx( 1.0 ) @@ -394,7 +394,7 @@ data.float_nine_point_one == Approx( 0 ) - 9.1 == Approx( 0 ) + 9.1 == Approx( 0.0 ) @@ -469,7 +469,7 @@ data.float_nine_point_one != Approx( 9.11f ) - 9.1 != Approx( 9.11 ) + 9.1 != Approx( 9.1099996567 ) @@ -477,7 +477,7 @@ data.float_nine_point_one != Approx( 9.0f ) - 9.1 != Approx( 9 ) + 9.1 != Approx( 9.0 ) @@ -485,7 +485,7 @@ data.float_nine_point_one != Approx( 1 ) - 9.1 != Approx( 1 ) + 9.1 != Approx( 1.0 ) @@ -493,7 +493,7 @@ data.float_nine_point_one != Approx( 0 ) - 9.1 != Approx( 0 ) + 9.1 != Approx( 0.0 ) @@ -552,7 +552,7 @@ data.float_nine_point_one != Approx( 9.1f ) - 9.1 != Approx( 9.1 ) + 9.1 != Approx( 9.1000003815 ) @@ -560,7 +560,7 @@ data.double_pi != Approx( 3.1415926535 ) - 3.1415926535 != Approx( 3.14159 ) + 3.1415926535 != Approx( 3.1415926535 ) diff --git a/projects/SelfTest/CmdLineTests.cpp b/projects/SelfTest/CmdLineTests.cpp index 968a1529..3eb54d2f 100644 --- a/projects/SelfTest/CmdLineTests.cpp +++ b/projects/SelfTest/CmdLineTests.cpp @@ -116,7 +116,7 @@ TEST_CASE( "cmdline" ) { .shortOpt( "d" ) .longOpt( "description" ) .hint( "some text" ); - + const char* argv[] = { "test", "-n 42", "-d some text" }; std::vector unusedTokens = parseInto( cli, argv, config1 ); diff --git a/projects/SelfTest/MiscTests.cpp b/projects/SelfTest/MiscTests.cpp index 02268686..f62e265e 100644 --- a/projects/SelfTest/MiscTests.cpp +++ b/projects/SelfTest/MiscTests.cpp @@ -116,9 +116,9 @@ namespace MiscTests TEST_CASE( "Sends stuff to stdout and stderr", "" ) { - std::cout << "Some information" << std::endl; + std::cout << "A string sent directly to stdout" << std::endl; - std::cerr << "An error" << std::endl; + std::cerr << "A string sent directly to stderr" << std::endl; } inline const char* makeString( bool makeNull ) diff --git a/single_include/catch.hpp b/single_include/catch.hpp index eb20e5de..770b3537 100644 --- a/single_include/catch.hpp +++ b/single_include/catch.hpp @@ -1,6 +1,6 @@ /* - * CATCH v1.0 build 20 (master branch) - * Generated: 2013-12-18 08:38:15.171445 + * CATCH v1.0 build 23 (master branch) + * Generated: 2013-12-23 10:22:45.547645 * ---------------------------------------------------------- * This file has been merged from multiple headers. Please don't edit it directly * Copyright (c) 2012 Two Blue Cubes Ltd. All rights reserved. @@ -2096,7 +2096,7 @@ namespace Detail { std::string toString() const { std::ostringstream oss; - oss << "Approx( " << m_value << " )"; + oss << "Approx( " << Catch::toString( m_value ) << " )"; return oss.str(); } @@ -3224,7 +3224,8 @@ namespace Clara { }; void parseIntoTokens( int argc, char const * const * argv, std::vector& tokens ) const { - for( int i = 1; i < argc; ++i ) + const std::string doubleDash = "--"; + for( int i = 1; i < argc && argv[i] != doubleDash; ++i ) parseIntoTokens( argv[i] , tokens); } void parseIntoTokens( std::string arg, std::vector& tokens ) const { @@ -3389,18 +3390,25 @@ namespace Clara { CommandLine() : m_boundProcessName( new Detail::NullBinder() ), - m_highestSpecifiedArgPosition( 0 ) + m_highestSpecifiedArgPosition( 0 ), + m_throwOnUnrecognisedTokens( false ) {} CommandLine( CommandLine const& other ) : m_boundProcessName( other.m_boundProcessName ), m_options ( other.m_options ), m_positionalArgs( other.m_positionalArgs ), - m_highestSpecifiedArgPosition( other.m_highestSpecifiedArgPosition ) + m_highestSpecifiedArgPosition( other.m_highestSpecifiedArgPosition ), + m_throwOnUnrecognisedTokens( other.m_throwOnUnrecognisedTokens ) { if( other.m_arg.get() ) m_arg = ArgAutoPtr( new Arg( *other.m_arg ) ); } + CommandLine& setThrowOnUnrecognisedTokens( bool shouldThrow = true ) { + m_throwOnUnrecognisedTokens = shouldThrow; + return *this; + } + template ArgBinder bind( F f ) { ArgBinder binder( this, f ); @@ -3506,6 +3514,7 @@ namespace Clara { std::vector populateOptions( std::vector const& tokens, ConfigT& config ) const { std::vector unusedTokens; + std::vector errors; for( std::size_t i = 0; i < tokens.size(); ++i ) { Parser::Token const& token = tokens[i]; typename std::vector::const_iterator it = m_options.begin(), itEnd = m_options.end(); @@ -3517,8 +3526,9 @@ namespace Clara { ( token.type == Parser::Token::LongOpt && arg.hasLongName( token.data ) ) ) { if( arg.takesArg() ) { if( i == tokens.size()-1 || tokens[i+1].type != Parser::Token::Positional ) - throw std::domain_error( "Expected argument to option " + token.data ); - arg.boundField.set( config, tokens[++i].data ); + errors.push_back( "Expected argument to option: " + token.data ); + else + arg.boundField.set( config, tokens[++i].data ); } else { arg.boundField.setFlag( config ); @@ -3527,11 +3537,26 @@ namespace Clara { } } catch( std::exception& ex ) { - throw std::runtime_error( std::string( ex.what() ) + "\n- while parsing: (" + arg.commands() + ")" ); + errors.push_back( std::string( ex.what() ) + "\n- while parsing: (" + arg.commands() + ")" ); } } - if( it == itEnd ) - unusedTokens.push_back( token ); + if( it == itEnd ) { + if( token.type == Parser::Token::Positional || !m_throwOnUnrecognisedTokens ) + unusedTokens.push_back( token ); + else if( m_throwOnUnrecognisedTokens ) + errors.push_back( "unrecognised option: " + token.data ); + } + } + if( !errors.empty() ) { + std::ostringstream oss; + for( std::vector::const_iterator it = errors.begin(), itEnd = errors.end(); + it != itEnd; + ++it ) { + if( it != errors.begin() ) + oss << "\n"; + oss << *it; + } + throw std::runtime_error( oss.str() ); } return unusedTokens; } @@ -3570,6 +3595,7 @@ namespace Clara { std::map m_positionalArgs; ArgAutoPtr m_arg; int m_highestSpecifiedArgPosition; + bool m_throwOnUnrecognisedTokens; }; } // end namespace Clara @@ -4812,9 +4838,8 @@ namespace Catch { int applyCommandLine( int argc, char* const argv[], OnUnusedOptions::DoWhat unusedOptionBehaviour = OnUnusedOptions::Fail ) { try { + m_cli.setThrowOnUnrecognisedTokens( unusedOptionBehaviour == OnUnusedOptions::Fail ); m_unusedTokens = m_cli.parseInto( argc, argv, m_configData ); - if( unusedOptionBehaviour == OnUnusedOptions::Fail ) - enforceNoUsedTokens(); if( m_configData.showHelp ) showHelp( m_configData.processName ); m_config.reset(); @@ -4822,7 +4847,7 @@ namespace Catch { catch( std::exception& ex ) { { Colour colourGuard( Colour::Red ); - std::cerr << "\nError in input:\n" + std::cerr << "\nError(s) in input:\n" << Text( ex.what(), TextAttributes().setIndent(2) ) << "\n\n"; } @@ -4837,18 +4862,6 @@ namespace Catch { m_config.reset(); } - void enforceNoUsedTokens() const { - if( !m_unusedTokens.empty() ) { - std::vector::const_iterator - it = m_unusedTokens.begin(), - itEnd = m_unusedTokens.end(); - std::string msg; - for(; it != itEnd; ++it ) - msg += " unrecognised option: " + it->data + "\n"; - throw std::runtime_error( msg.substr( 0, msg.size()-1 ) ); - } - } - int run( int argc, char* const argv[] ) { int returnCode = applyCommandLine( argc, argv ); @@ -6166,7 +6179,7 @@ namespace Catch { namespace Catch { // These numbers are maintained by a script - Version libraryVersion( 1, 0, 20, "master" ); + Version libraryVersion( 1, 0, 23, "master" ); } // #included from: catch_text.hpp