Some Clara/ command line clean-up and tweaks

This commit is contained in:
Phil Nash 2013-08-16 18:57:57 +01:00
parent f41fad7e20
commit 1870ca8455
4 changed files with 35 additions and 34 deletions

View File

@ -150,11 +150,12 @@ namespace Catch {
m_config.reset(); m_config.reset();
} }
catch( std::exception& ex ) { catch( std::exception& ex ) {
{
Colour colourGuard( Colour::Red );
std::cerr << "\nError in input:\n" std::cerr << "\nError in input:\n"
<< Text( ex.what(), TextAttributes() << Text( ex.what(), TextAttributes().setIndent(2) )
.setInitialIndent(2)
.setIndent(4) )
<< "\n\n"; << "\n\n";
}
m_cli.usage( std::cout, m_configData.processName ); m_cli.usage( std::cout, m_configData.processName );
return (std::numeric_limits<int>::max)(); return (std::numeric_limits<int>::max)();
} }

View File

@ -85,20 +85,20 @@ namespace Catch {
.describe( "output filename" ) .describe( "output filename" )
.shortOpt( "o") .shortOpt( "o")
.longOpt( "out" ) .longOpt( "out" )
.argName( "filename" ); .hint( "filename" );
cli.bind( &ConfigData::reporterName ) cli.bind( &ConfigData::reporterName )
.describe( "reporter to use - defaults to console" ) .describe( "reporter to use - defaults to console" )
.shortOpt( "r") .shortOpt( "r")
.longOpt( "reporter" ) .longOpt( "reporter" )
// .argName( "name[:filename]" ); // .hint( "name[:filename]" );
.argName( "name" ); .hint( "name" );
cli.bind( &ConfigData::name ) cli.bind( &ConfigData::name )
.describe( "suite name" ) .describe( "suite name" )
.shortOpt( "n") .shortOpt( "n")
.longOpt( "name" ) .longOpt( "name" )
.argName( "name" ); .hint( "name" );
cli.bind( &abortAfterFirst ) cli.bind( &abortAfterFirst )
.describe( "abort at first failure" ) .describe( "abort at first failure" )
@ -109,29 +109,29 @@ namespace Catch {
.describe( "abort after x failures" ) .describe( "abort after x failures" )
.shortOpt( "x") .shortOpt( "x")
.longOpt( "abortx" ) .longOpt( "abortx" )
.argName( "number of failures" ); .hint( "number of failures" );
cli.bind( &addWarning ) cli.bind( &addWarning )
.describe( "enable warnings" ) .describe( "enable warnings" )
.shortOpt( "w") .shortOpt( "w")
.longOpt( "warn" ) .longOpt( "warn" )
.argName( "warning name" ); .hint( "warning name" );
// cli.bind( &setVerbosity ) // cli.bind( &setVerbosity )
// .describe( "level of verbosity (0=no output)" ) // .describe( "level of verbosity (0=no output)" )
// .shortOpt( "v") // .shortOpt( "v")
// .longOpt( "verbosity" ) // .longOpt( "verbosity" )
// .argName( "level" ); // .hint( "level" );
cli.bind( &addTestOrTags ) cli.bind( &addTestOrTags )
.describe( "which test or tests to use" ) .describe( "which test or tests to use" )
.argName( "test name, pattern or tags" ); .hint( "test name, pattern or tags" );
cli.bind( &setShowDurations ) cli.bind( &setShowDurations )
.describe( "show test durations" ) .describe( "show test durations" )
.shortOpt( "d") .shortOpt( "d")
.longOpt( "durations" ) .longOpt( "durations" )
.argName( "durations" ); .hint( "yes/no" );
return cli; return cli;
} }

View File

@ -34,12 +34,12 @@ namespace Clara {
inline void convertInto( std::string const& _source, bool& _dest ) { inline void convertInto( std::string const& _source, bool& _dest ) {
std::string sourceLC = _source; std::string sourceLC = _source;
std::transform( sourceLC.begin(), sourceLC.end(), sourceLC.begin(), ::tolower ); std::transform( sourceLC.begin(), sourceLC.end(), sourceLC.begin(), ::tolower );
if( sourceLC == "1" || sourceLC == "true" || sourceLC == "yes" || sourceLC == "on" ) if( sourceLC == "y" || sourceLC == "1" || sourceLC == "true" || sourceLC == "yes" || sourceLC == "on" )
_dest = true; _dest = true;
else if( sourceLC == "0" || sourceLC == "false" || sourceLC == "no" || sourceLC == "off" ) else if( sourceLC == "n" || sourceLC == "0" || sourceLC == "false" || sourceLC == "no" || sourceLC == "off" )
_dest = false; _dest = false;
else else
throw std::runtime_error( "Expected a boolean value but did recognise: '" + _source + "'" ); throw std::runtime_error( "Expected a boolean value but did not recognise:\n '" + _source + "'" );
} }
inline void convertInto( bool _source, bool& _dest ) { inline void convertInto( bool _source, bool& _dest ) {
_dest = _source; _dest = _source;
@ -257,7 +257,7 @@ namespace Clara {
return _longName == longName; return _longName == longName;
} }
bool takesArg() const { bool takesArg() const {
return !argName.empty(); return !hint.empty();
} }
bool isFixedPositional() const { bool isFixedPositional() const {
return position != -1; return position != -1;
@ -292,8 +292,8 @@ namespace Clara {
oss << ", "; oss << ", ";
oss << "--" << longName; oss << "--" << longName;
} }
if( !argName.empty() ) if( !hint.empty() )
oss << " <" << argName << ">"; oss << " <" << hint << ">";
return oss.str(); return oss.str();
} }
@ -301,7 +301,7 @@ namespace Clara {
std::vector<std::string> shortNames; std::vector<std::string> shortNames;
std::string longName; std::string longName;
std::string description; std::string description;
std::string argName; std::string hint;
int position; int position;
}; };
@ -347,8 +347,8 @@ namespace Clara {
m_arg.description = description; m_arg.description = description;
return *this; return *this;
} }
ArgBinder& argName( std::string const& argName ) { ArgBinder& hint( std::string const& hint ) {
m_arg.argName = argName; m_arg.hint = hint;
return *this; return *this;
} }
ArgBinder& position( int position ) { ArgBinder& position( int position ) {
@ -423,9 +423,9 @@ namespace Clara {
os << " "; os << " ";
typename std::map<int, Arg>::const_iterator it = m_positionalArgs.find( i ); typename std::map<int, Arg>::const_iterator it = m_positionalArgs.find( i );
if( it != m_positionalArgs.end() ) if( it != m_positionalArgs.end() )
os << "<" << it->second.argName << ">"; os << "<" << it->second.hint << ">";
else if( m_arg.get() ) else if( m_arg.get() )
os << "<" << m_arg->argName << ">"; os << "<" << m_arg->hint << ">";
else else
throw std::logic_error( "non consecutive positional arguments with no floating args" ); throw std::logic_error( "non consecutive positional arguments with no floating args" );
} }
@ -433,7 +433,7 @@ namespace Clara {
if( m_arg.get() ) { if( m_arg.get() ) {
if( m_highestSpecifiedArgPosition > 1 ) if( m_highestSpecifiedArgPosition > 1 )
os << " "; os << " ";
os << "[<" << m_arg->argName << "> ...]"; os << "[<" << m_arg->hint << "> ...]";
} }
} }
std::string argSynopsis() const { std::string argSynopsis() const {
@ -502,7 +502,7 @@ namespace Clara {
} }
} }
catch( std::exception& ex ) { catch( std::exception& ex ) {
throw std::runtime_error( std::string( ex.what() ) + " while parsing: (" + arg.commands() + ")" ); throw std::runtime_error( std::string( ex.what() ) + "\n- while parsing: (" + arg.commands() + ")" );
} }
} }
if( it == itEnd ) if( it == itEnd )

View File

@ -55,7 +55,7 @@ TEST_CASE( "cmdline" ) {
.describe( "specifies output file" ) .describe( "specifies output file" )
.shortOpt( "o" ) .shortOpt( "o" )
.longOpt( "output" ) .longOpt( "output" )
.argName( "filename" ); .hint( "filename" );
SECTION( "process name" ) { SECTION( "process name" ) {
char const * argv[] = { "test", "-o filename.ext" }; char const * argv[] = { "test", "-o filename.ext" };
@ -90,7 +90,7 @@ TEST_CASE( "cmdline" ) {
cli.bind( &TestOpt::number ) cli.bind( &TestOpt::number )
.shortOpt( "n" ) .shortOpt( "n" )
.argName( "an integral value" ); .hint( "an integral value" );
SECTION( "a number" ) { SECTION( "a number" ) {
const char* argv[] = { "test", "-n 42" }; const char* argv[] = { "test", "-n 42" };
@ -115,7 +115,7 @@ TEST_CASE( "cmdline" ) {
.describe( "description" ) .describe( "description" )
.shortOpt( "d" ) .shortOpt( "d" )
.longOpt( "description" ) .longOpt( "description" )
.argName( "some text" ); .hint( "some text" );
const char* argv[] = { "test", "-n 42", "-d some text" }; const char* argv[] = { "test", "-n 42", "-d some text" };
std::vector<Clara::Parser::Token> unusedTokens = parseInto( cli, argv, config1 ); std::vector<Clara::Parser::Token> unusedTokens = parseInto( cli, argv, config1 );
@ -131,7 +131,7 @@ TEST_CASE( "cmdline" ) {
cli.bind( &TestOpt::setValidIndex ) cli.bind( &TestOpt::setValidIndex )
.describe( "An index, which is an integer between 0 and 10, inclusive" ) .describe( "An index, which is an integer between 0 and 10, inclusive" )
.shortOpt( "i" ) .shortOpt( "i" )
.argName( "index" ); .hint( "index" );
SECTION( "in range" ) { SECTION( "in range" ) {
const char* argv[] = { "test", "-i 3" }; const char* argv[] = { "test", "-i 3" };
@ -167,14 +167,14 @@ TEST_CASE( "cmdline" ) {
SECTION( "positional" ) { SECTION( "positional" ) {
cli.bind( &TestOpt::secondPos ) cli.bind( &TestOpt::secondPos )
.describe( "Second position" ) .describe( "Second position" )
.argName( "second arg" ) .hint( "second arg" )
.position( 2 ); .position( 2 );
cli.bind( &TestOpt::unpositional ) cli.bind( &TestOpt::unpositional )
.argName( "any arg" ) .hint( "any arg" )
.describe( "Unpositional" ); .describe( "Unpositional" );
cli.bind( &TestOpt::firstPos ) cli.bind( &TestOpt::firstPos )
.describe( "First position" ) .describe( "First position" )
.argName( "first arg" ) .hint( "first arg" )
.position( 1 ); .position( 1 );
// std::cout << cli.usage( "testApp" ) << std::endl; // std::cout << cli.usage( "testApp" ) << std::endl;