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 [...]\n" - << "\t-r, --reporter \n" - << "\t-o, --out |<%stream name>\n" - << "\t-s, --success\n" - << "\t-b, --break\n" - << "\t-n, --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 { public: virtual ~OptionParser() {} - void parseIntoConfig( CommandParser parser, ConfigData& config ) { + Command find( const CommandParser& parser ) const { Command cmd; for( std::vector::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::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 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 ""; } 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 "|<%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 ""; } 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(); - add(); - add(); - add(); - add(); - add(); - add(); - add(); + AllOptions() { add(); + add(); + add(); + add(); + add(); + add(); + add(); + add(); + add(); + add(); } void parseIntoConfig( const CommandParser& parser, ConfigData& config ) { @@ -382,7 +424,7 @@ namespace Catch { template void add() { - m_parsers.push_back( new SharedImpl() ); + m_parsers.push_back( new T() ); } Parsers m_parsers;