Read-only mirrors. Development, issues and pull requests are on GitHub.

summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorPhil Nash2012-08-27 12:19:07 +0100
committerPhil Nash2012-08-27 12:19:07 +0100
commite7db40034cd11bc8b3b04a24f244dfdafc9feb29 (patch)
treed4a78087ceddd7ff790e54f2b841c94146f4dd14
parenteb775aa7af1a0734d4588bdaa688aa1cd1605a14 (diff)
downloadcatch3-e7db40034cd11bc8b3b04a24f244dfdafc9feb29.tar.gz
Generate help from options objects
-rw-r--r--include/catch_runner.hpp25
-rw-r--r--include/internal/catch_commandline.hpp80
2 files changed, 71 insertions, 34 deletions
diff --git a/include/catch_runner.hpp b/include/catch_runner.hpp
index 0b997129..eb679d14 100644
--- a/include/catch_runner.hpp
+++ b/include/catch_runner.hpp
@@ -147,17 +147,12 @@ namespace Catch {
}
inline void showUsage( std::ostream& os ) {
- os << "\t-?, -h, --help\n"
- << "\t-l, --list [all | tests | reporters [xml]]\n"
- << "\t-t, --test <testspec> [<testspec>...]\n"
- << "\t-r, --reporter <reporter name>\n"
- << "\t-o, --out <file name>|<%stream name>\n"
- << "\t-s, --success\n"
- << "\t-b, --break\n"
- << "\t-n, --name <name>\n"
- << "\t-a, --abort [#]\n"
- << "\t-nt, --nothrow\n\n"
- << "For more detail usage please see: https://github.com/philsquared/Catch/wiki/Command-line" << std::endl;
+ AllOptions options;
+ for( AllOptions::const_iterator it = options.begin(); it != options.end(); ++it ) {
+ OptionParser& opt = **it;
+ os << " " << opt.optionNames() << " " << opt.argsSynopsis() << "\n";
+ }
+ os << "For more detail usage please see: https://github.com/philsquared/Catch/wiki/Command-line" << std::endl;
}
inline void showHelp( std::string exeName ) {
std::string::size_type pos = exeName.find_last_of( "/\\" );
@@ -174,9 +169,7 @@ namespace Catch {
try {
CommandParser parser( argc, argv );
- AllOptions options;
-
- if( Command cmd = parser.find( "-h", "-?", "--help" ) ) {
+ if( Command cmd = Options::HelpOptionParser().find( parser ) ) {
if( cmd.argsCount() != 0 )
cmd.raiseError( "Does not accept arguments" );
@@ -184,7 +177,9 @@ namespace Catch {
Catch::cleanUp();
return 0;
}
-
+
+ AllOptions options;
+
options.parseIntoConfig( parser, config.data() );
}
catch( std::exception& ex ) {
diff --git a/include/internal/catch_commandline.hpp b/include/internal/catch_commandline.hpp
index 5879176f..0ed4b8d4 100644
--- a/include/internal/catch_commandline.hpp
+++ b/include/internal/catch_commandline.hpp
@@ -96,24 +96,46 @@ namespace Catch {
char const * const * m_argv;
};
- class OptionParser : public IShared {
+ class OptionParser : public SharedImpl<IShared> {
public:
virtual ~OptionParser() {}
- void parseIntoConfig( CommandParser parser, ConfigData& config ) {
+ Command find( const CommandParser& parser ) const {
Command cmd;
for( std::vector<std::string>::const_iterator it = m_optionNames.begin();
it != m_optionNames.end();
++it )
cmd += parser.find( *it );
+ return cmd;
+ }
- if( cmd )
+ void parseIntoConfig( const CommandParser& parser, ConfigData& config ) {
+ if( Command cmd = find( parser ) )
parseIntoConfig( cmd, config );
}
virtual void parseIntoConfig( const Command& cmd, ConfigData& config ) = 0;
virtual std::string argsSynopsis() const = 0;
virtual std::string optionSummary() const = 0;
+
+ std::string optionNames() const {
+ std::string names;
+ for( std::vector<std::string>::const_iterator it = m_optionNames.begin();
+ it != m_optionNames.end();
+ ++it ) {
+ if( !it->empty() ) {
+ if( !names.empty() )
+ names += ", ";
+ names += *it;
+ }
+ else {
+ names = "[" + names;
+ }
+ }
+ if( names[0] == '[' )
+ names += "]";
+ return names;
+ }
protected:
std::vector<std::string> m_optionNames;
@@ -121,6 +143,25 @@ namespace Catch {
namespace Options {
+ class HelpOptionParser : public OptionParser {
+ public:
+ HelpOptionParser() {
+ m_optionNames.push_back( "-?" );
+ m_optionNames.push_back( "-h" );
+ m_optionNames.push_back( "--help" );
+ }
+ virtual std::string argsSynopsis() const {
+ return "";
+ }
+ virtual std::string optionSummary() const {
+ return "Shows this usage summary";
+ }
+
+ virtual void parseIntoConfig( const Command&, ConfigData& ) {
+ // Does not affect config
+ }
+ };
+
class TestCaseOptionParser : public OptionParser {
public:
TestCaseOptionParser() {
@@ -164,7 +205,7 @@ namespace Catch {
m_optionNames.push_back( "--list" );
}
virtual std::string argsSynopsis() const {
- return "!TBD";
+ return "[all | tests | reporters [xml]]";
}
virtual std::string optionSummary() const {
return "!TBD";
@@ -203,7 +244,7 @@ namespace Catch {
m_optionNames.push_back( "--reporter" );
}
virtual std::string argsSynopsis() const {
- return "!TBD";
+ return "<reporter name>";
}
virtual std::string optionSummary() const {
return "!TBD";
@@ -223,7 +264,7 @@ namespace Catch {
m_optionNames.push_back( "--out" );
}
virtual std::string argsSynopsis() const {
- return "!TBD";
+ return "<file name>|<%stream name>";
}
virtual std::string optionSummary() const {
return "!TBD";
@@ -246,7 +287,7 @@ namespace Catch {
m_optionNames.push_back( "--success" );
}
virtual std::string argsSynopsis() const {
- return "!TBD";
+ return "";
}
virtual std::string optionSummary() const {
return "!TBD";
@@ -266,7 +307,7 @@ namespace Catch {
m_optionNames.push_back( "--break" );
}
virtual std::string argsSynopsis() const {
- return "!TBD";
+ return "";
}
virtual std::string optionSummary() const {
return "!TBD";
@@ -286,7 +327,7 @@ namespace Catch {
m_optionNames.push_back( "--name" );
}
virtual std::string argsSynopsis() const {
- return "!TBD";
+ return "<name>";
}
virtual std::string optionSummary() const {
return "!TBD";
@@ -306,7 +347,7 @@ namespace Catch {
m_optionNames.push_back( "--abort" );
}
virtual std::string argsSynopsis() const {
- return "!TBD";
+ return "[#]";
}
virtual std::string optionSummary() const {
return "!TBD";
@@ -334,7 +375,7 @@ namespace Catch {
m_optionNames.push_back( "--nothrow" );
}
virtual std::string argsSynopsis() const {
- return "!TBD";
+ return "";
}
virtual std::string optionSummary() const {
return "!TBD";
@@ -355,16 +396,17 @@ namespace Catch {
typedef Parsers::const_iterator const_iterator;
typedef Parsers::const_iterator iterator;
- AllOptions() {
- add<Options::AbortOptionParser>();
- add<Options::DebugBreakOptionParser>();
+ AllOptions() {
+ add<Options::TestCaseOptionParser>();
add<Options::ListOptionParser>();
- add<Options::NameOptionParser>();
- add<Options::NoThrowOptionParser>();
- add<Options::OutputOptionParser>();
add<Options::ReporterOptionParser>();
+ add<Options::OutputOptionParser>();
add<Options::SuccesssOptionParser>();
- add<Options::TestCaseOptionParser>();
+ add<Options::DebugBreakOptionParser>();
+ add<Options::NameOptionParser>();
+ add<Options::AbortOptionParser>();
+ add<Options::NoThrowOptionParser>();
+ add<Options::HelpOptionParser>();
}
void parseIntoConfig( const CommandParser& parser, ConfigData& config ) {
@@ -382,7 +424,7 @@ namespace Catch {
template<typename T>
void add() {
- m_parsers.push_back( new SharedImpl<T>() );
+ m_parsers.push_back( new T() );
}
Parsers m_parsers;