2010-11-10 00:24:00 +01:00
|
|
|
/*
|
|
|
|
* Created by Phil on 02/11/2010.
|
|
|
|
* Copyright 2010 Two Blue Cubes Ltd. All rights reserved.
|
|
|
|
*
|
|
|
|
* Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
* file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
*/
|
|
|
|
#ifndef TWOBLUECUBES_CATCH_COMMANDLINE_HPP_INCLUDED
|
|
|
|
#define TWOBLUECUBES_CATCH_COMMANDLINE_HPP_INCLUDED
|
|
|
|
|
2011-01-01 01:29:58 +01:00
|
|
|
#include "catch_config.hpp"
|
2010-11-10 00:24:00 +01:00
|
|
|
#include "catch_runner_impl.hpp"
|
|
|
|
|
2012-05-16 00:58:23 +02:00
|
|
|
namespace Catch {
|
|
|
|
|
2012-05-31 20:40:26 +02:00
|
|
|
class Command {
|
2010-11-10 00:24:00 +01:00
|
|
|
public:
|
2012-05-31 20:40:26 +02:00
|
|
|
Command(){}
|
2010-11-10 00:24:00 +01:00
|
|
|
|
2012-06-03 00:08:07 +02:00
|
|
|
explicit Command( const std::string& name ) : m_name( name ) {}
|
2012-05-31 20:40:26 +02:00
|
|
|
|
|
|
|
Command& operator += ( const std::string& arg ) {
|
|
|
|
m_args.push_back( arg );
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
Command& operator += ( const Command& other ) {
|
|
|
|
std::copy( other.m_args.begin(), other.m_args.end(), std::back_inserter( m_args ) );
|
|
|
|
if( m_name.empty() )
|
|
|
|
m_name = other.m_name;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
Command operator + ( const Command& other ) {
|
|
|
|
Command newCommand( *this );
|
|
|
|
newCommand += other;
|
|
|
|
return newCommand;
|
2010-11-10 00:24:00 +01:00
|
|
|
}
|
|
|
|
|
2012-05-31 20:40:26 +02:00
|
|
|
operator SafeBool::type() const {
|
|
|
|
return SafeBool::makeSafe( !m_name.empty() );
|
2010-11-10 00:24:00 +01:00
|
|
|
}
|
|
|
|
|
2012-05-31 20:40:26 +02:00
|
|
|
std::string name() const { return m_name; }
|
|
|
|
std::string operator[]( std::size_t i ) const { return m_args[i]; }
|
|
|
|
std::size_t argsCount() const { return m_args.size(); }
|
|
|
|
|
|
|
|
void raiseError( const std::string& message ) const {
|
|
|
|
std::ostringstream oss;
|
|
|
|
oss << "Error while parsing " << m_name << ". " << message << ".";
|
|
|
|
if( m_args.size() > 0 )
|
|
|
|
oss << " Arguments where:";
|
|
|
|
for( std::size_t i = 0; i < m_args.size(); ++i )
|
|
|
|
oss << " " << m_args[i];
|
|
|
|
throw std::domain_error( oss.str() );
|
2010-11-10 00:24:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
2012-05-31 20:40:26 +02:00
|
|
|
std::string m_name;
|
2010-11-10 00:24:00 +01:00
|
|
|
std::vector<std::string> m_args;
|
|
|
|
};
|
|
|
|
|
2012-05-31 20:40:26 +02:00
|
|
|
class CommandParser {
|
|
|
|
public:
|
2012-06-03 00:08:07 +02:00
|
|
|
CommandParser( int argc, char const * const * argv ) : m_argc( static_cast<std::size_t>( argc ) ), m_argv( argv ) {}
|
2012-05-31 20:40:26 +02:00
|
|
|
|
|
|
|
Command find( const std::string& arg1, const std::string& arg2, const std::string& arg3 ) const {
|
|
|
|
return find( arg1 ) + find( arg2 ) + find( arg3 );
|
|
|
|
}
|
|
|
|
|
|
|
|
Command find( const std::string& shortArg, const std::string& longArg ) const {
|
|
|
|
return find( shortArg ) + find( longArg );
|
|
|
|
}
|
|
|
|
Command find( const std::string& arg ) const {
|
|
|
|
for( std::size_t i = 0; i < m_argc; ++i )
|
|
|
|
if( m_argv[i] == arg )
|
|
|
|
return getArgs( i );
|
|
|
|
return Command();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
Command getArgs( std::size_t from ) const {
|
|
|
|
Command command( m_argv[from] );
|
|
|
|
for( std::size_t i = from+1; i < m_argc && m_argv[i][0] != '-'; ++i )
|
|
|
|
command += m_argv[i];
|
|
|
|
return command;
|
|
|
|
}
|
|
|
|
|
2012-06-03 00:08:07 +02:00
|
|
|
std::size_t m_argc;
|
2012-05-31 20:40:26 +02:00
|
|
|
char const * const * m_argv;
|
|
|
|
};
|
|
|
|
|
|
|
|
inline bool parseIntoConfig( const CommandParser& parser, Config& config ) {
|
|
|
|
|
|
|
|
try {
|
|
|
|
if( Command cmd = parser.find( "-l", "--list" ) ) {
|
|
|
|
if( cmd.argsCount() > 2 )
|
2012-06-03 20:03:17 +02:00
|
|
|
cmd.raiseError( "Expected upto 2 arguments" );
|
2012-05-31 20:40:26 +02:00
|
|
|
|
|
|
|
List::What listSpec = List::All;
|
|
|
|
if( cmd.argsCount() >= 1 ) {
|
|
|
|
if( cmd[0] == "tests" )
|
|
|
|
listSpec = List::Tests;
|
|
|
|
else if( cmd[0] == "reporters" )
|
|
|
|
listSpec = List::Reports;
|
|
|
|
else
|
2012-06-03 20:03:17 +02:00
|
|
|
cmd.raiseError( "Expected [tests] or [reporters]" );
|
2012-05-31 20:40:26 +02:00
|
|
|
}
|
|
|
|
if( cmd.argsCount() >= 2 ) {
|
|
|
|
if( cmd[1] == "xml" )
|
|
|
|
listSpec = static_cast<List::What>( listSpec | List::AsXml );
|
|
|
|
else if( cmd[1] == "text" )
|
|
|
|
listSpec = static_cast<List::What>( listSpec | List::AsText );
|
|
|
|
else
|
2012-06-03 20:03:17 +02:00
|
|
|
cmd.raiseError( "Expected [xml] or [text]" );
|
2012-05-31 20:40:26 +02:00
|
|
|
}
|
|
|
|
config.setListSpec( static_cast<List::What>( config.getListSpec() | listSpec ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( Command cmd = parser.find( "-t", "--test" ) ) {
|
|
|
|
if( cmd.argsCount() == 0 )
|
2012-06-03 20:03:17 +02:00
|
|
|
cmd.raiseError( "Expected at least one argument" );
|
2012-05-31 20:40:26 +02:00
|
|
|
for( std::size_t i = 0; i < cmd.argsCount(); ++i )
|
|
|
|
config.addTestSpec( cmd[i] );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( Command cmd = parser.find( "-r", "--reporter" ) ) {
|
|
|
|
if( cmd.argsCount() != 1 )
|
|
|
|
cmd.raiseError( "Expected one argument" );
|
|
|
|
config.setReporter( cmd[0] );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( Command cmd = parser.find( "-o", "--out" ) ) {
|
|
|
|
if( cmd.argsCount() == 0 )
|
2012-06-03 20:03:17 +02:00
|
|
|
cmd.raiseError( "Expected filename" );
|
2012-05-31 20:40:26 +02:00
|
|
|
if( cmd[0][0] == '%' )
|
|
|
|
config.useStream( cmd[0].substr( 1 ) );
|
|
|
|
else
|
|
|
|
config.setFilename( cmd[0] );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( Command cmd = parser.find( "-s", "--success" ) ) {
|
|
|
|
if( cmd.argsCount() != 0 )
|
2012-06-03 20:03:17 +02:00
|
|
|
cmd.raiseError( "Does not accept arguments" );
|
2012-05-31 20:40:26 +02:00
|
|
|
config.setIncludeWhichResults( Include::SuccessfulResults );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( Command cmd = parser.find( "-b", "--break" ) ) {
|
|
|
|
if( cmd.argsCount() != 0 )
|
2012-06-03 20:03:17 +02:00
|
|
|
cmd.raiseError( "Does not accept arguments" );
|
2012-05-31 20:40:26 +02:00
|
|
|
config.setShouldDebugBreak( true );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( Command cmd = parser.find( "-n", "--name" ) ) {
|
|
|
|
if( cmd.argsCount() != 1 )
|
2012-06-03 20:03:17 +02:00
|
|
|
cmd.raiseError( "Expected a name" );
|
2012-05-31 20:40:26 +02:00
|
|
|
config.setName( cmd[0] );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( Command cmd = parser.find( "-h", "-?", "--help" ) ) {
|
|
|
|
if( cmd.argsCount() != 0 )
|
2012-06-03 20:03:17 +02:00
|
|
|
cmd.raiseError( "Does not accept arguments" );
|
2012-05-31 20:40:26 +02:00
|
|
|
config.setShowHelp( true );
|
|
|
|
}
|
2012-06-01 20:40:27 +02:00
|
|
|
|
2012-06-03 00:26:32 +02:00
|
|
|
if( Command cmd = parser.find( "-a", "--abort" ) ) {
|
2012-06-01 20:40:27 +02:00
|
|
|
if( cmd.argsCount() > 1 )
|
2012-06-03 20:03:17 +02:00
|
|
|
cmd.raiseError( "Only accepts 0-1 arguments" );
|
2012-06-01 20:40:27 +02:00
|
|
|
int threshold = 1;
|
|
|
|
if( cmd.argsCount() == 1 )
|
|
|
|
{
|
|
|
|
std::stringstream ss;
|
|
|
|
ss << cmd[0];
|
|
|
|
ss >> threshold;
|
|
|
|
}
|
|
|
|
config.setCutoff( threshold );
|
|
|
|
}
|
2012-06-05 21:50:47 +02:00
|
|
|
|
|
|
|
if( Command cmd = parser.find( "-nt", "--nothrow" ) ) {
|
|
|
|
if( cmd.argsCount() != 0 )
|
|
|
|
cmd.raiseError( "Does not accept arguments" );
|
|
|
|
config.setAllowThrows( false );
|
|
|
|
}
|
|
|
|
|
2012-05-31 20:40:26 +02:00
|
|
|
}
|
|
|
|
catch( std::exception& ex ) {
|
|
|
|
config.setError( ex.what() );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2010-11-10 00:24:00 +01:00
|
|
|
|
|
|
|
} // end namespace Catch
|
|
|
|
|
2010-12-28 15:42:46 +01:00
|
|
|
#endif // TWOBLUECUBES_CATCH_COMMANDLINE_HPP_INCLUDED
|