From 1b47e11387b3a6c0e1186756c77e1990404466b5 Mon Sep 17 00:00:00 2001 From: Phil Nash Date: Tue, 4 Jun 2013 22:49:14 +0100 Subject: [PATCH] Added processConfig() method --- include/catch_runner.hpp | 30 ++++++++++++++++++++++-------- include/internal/catch_config.hpp | 2 ++ 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/include/catch_runner.hpp b/include/catch_runner.hpp index 11266f7f..5402cd05 100644 --- a/include/catch_runner.hpp +++ b/include/catch_runner.hpp @@ -131,7 +131,8 @@ namespace Catch { return result; } - inline void showHelp( Clara::CommandLine const& cli, std::string const& processName ) { + inline void showHelp( std::string const& processName ) { + Clara::CommandLine cli = makeCommandLineParser(); std::cout << "\nCatch v" << libraryVersion.majorVersion << "." << libraryVersion.minorVersion << " build " @@ -143,16 +144,30 @@ namespace Catch { cli.usage( std::cout, processName ); std::cout << "\nFor more detail usage please see: https://github.com/philsquared/Catch/wiki/Command-line\n" << std::endl; } + inline Ptr processConfig( int argc, char* const argv[], ConfigData configData = ConfigData() ) { + Clara::CommandLine cli = makeCommandLineParser(); + std::vector unused = cli.parseInto( argc, argv, configData ); + if( !unused.empty() ) { + std::vector::const_iterator + it = unused.begin(), + itEnd = unused.end(); + std::string msg; + for(; it != itEnd; ++it ) + msg += " unrecognised option: " + it->data + "\n"; + throw std::runtime_error( msg.substr( 0, msg.size()-1 ) ); + } + Ptr config = new Config( configData ); + return config; + } inline int Main( int argc, char* const argv[], ConfigData configData = ConfigData() ) { - Clara::CommandLine cli = makeCommandLineParser(); + Ptr config; try { - cli.parseInto( argc, argv, configData ); - - if( configData.showHelp ) { - showHelp( cli, argv[0] ); + config = processConfig( argc, argv, configData ); + if( config->showHelp() ) { + showHelp( argv[0] ); Catch::cleanUp(); return 0; } @@ -160,11 +175,10 @@ namespace Catch { catch( std::exception& ex ) { std::cerr << "\nError in input:\n" << " " << ex.what() << "\n\n"; - cli.usage( std::cout, argv[0] ); + makeCommandLineParser().usage( std::cout, argv[0] ); Catch::cleanUp(); return (std::numeric_limits::max)(); } - Ptr config = new Config( configData ); return Main( config ); } diff --git a/include/internal/catch_config.hpp b/include/internal/catch_config.hpp index d294e48a..74089135 100644 --- a/include/internal/catch_config.hpp +++ b/include/internal/catch_config.hpp @@ -156,6 +156,8 @@ namespace Catch { std::vector const& filters() const { return m_filterSets; } + + bool showHelp() const { return m_data.showHelp; } // IConfig interface virtual bool allowThrows() const { return !m_data.noThrow; }