Clara::CommandLine<Options> CreateCommandLine() { Clara::CommandLine<Options> commandLine; commandLine.setThrowOnUnrecognisedTokens(true); commandLine.bindProcessName(&Options::processName); commandLine["-h"]["--help"] .describe("display usage information") .bind(&Options::showHelp); commandLine["-l"]["--list-types"] .describe("list all types parsed") .bind(&Options::listTypes); commandLine["-v"]["--verbose"] .describe("parse with clang in verbose mode") .bind(&Options::verbose); commandLine["-f"]["--file"] .describe("filename of the workspace to parse") .bind(&Options::filename, "filename"); commandLine["-o"]["--outputdir"] .describe("output directory") .bind(&Options::outputDir, "path"); return commandLine; }
int main(int argc, const char * argv[]) { Clara::CommandLine<Config> cli; cli.bind( &Config::colour ) .describe( "specify a colour" ) .shortOpt( "c") .longOpt( "colour" ) .hint( "widget colour" ); Config cfg = cli.parseInto( argc, argv ); std::cout << "Hello, " << cfg.colour << " World!\n"; }
inline Clara::CommandLine<ConfigData> makeCommandLineParser() { Clara::CommandLine<ConfigData> cli; cli.bindProcessName( &ConfigData::processName ); cli.bind( &ConfigData::showHelp ) .describe( "display usage information" ) .shortOpt( "?") .shortOpt( "h") .longOpt( "help" ); cli.bind( &ConfigData::listTests ) .describe( "list all/matching test cases" ) .shortOpt( "l") .longOpt( "list-tests" ); cli.bind( &ConfigData::listTags ) .describe( "list all/matching tags" ) .shortOpt( "t") .longOpt( "list-tags" ); cli.bind( &ConfigData::showSuccessfulTests ) .describe( "include successful tests in output" ) .shortOpt( "s") .longOpt( "success" ); cli.bind( &ConfigData::shouldDebugBreak ) .describe( "break into debugger on failure" ) .shortOpt( "b") .longOpt( "break" ); cli.bind( &ConfigData::noThrow ) .describe( "skip exception tests" ) .shortOpt( "e") .longOpt( "nothrow" ); cli.bind( &ConfigData::outputFilename ) .describe( "output filename" ) .shortOpt( "o") .longOpt( "out" ) .hint( "filename" ); cli.bind( &ConfigData::reporterName ) .describe( "reporter to use (defaults to console)" ) .shortOpt( "r") .longOpt( "reporter" ) // .hint( "name[:filename]" ); .hint( "name" ); cli.bind( &ConfigData::name ) .describe( "suite name" ) .shortOpt( "n") .longOpt( "name" ) .hint( "name" ); cli.bind( &abortAfterFirst ) .describe( "abort at first failure" ) .shortOpt( "a") .longOpt( "abort" ); cli.bind( &abortAfterX ) .describe( "abort after x failures" ) .shortOpt( "x") .longOpt( "abortx" ) .hint( "number of failures" ); cli.bind( &addWarning ) .describe( "enable warnings" ) .shortOpt( "w") .longOpt( "warn" ) .hint( "warning name" ); // cli.bind( &setVerbosity ) // .describe( "level of verbosity (0=no output)" ) // .shortOpt( "v") // .longOpt( "verbosity" ) // .hint( "level" ); cli.bind( &addTestOrTags ) .describe( "which test or tests to use" ) .hint( "test name, pattern or tags" ); cli.bind( &setShowDurations ) .describe( "show test durations" ) .shortOpt( "d") .longOpt( "durations" ) .hint( "yes/no" ); cli.bind( &loadTestNamesFromFile ) .describe( "load test names to run from a file" ) .shortOpt( "f") .longOpt( "input-file" ) .hint( "filename" ); // Less common commands which don't have a short form cli.bind( &ConfigData::listTestNamesOnly ) .describe( "list all/matching test cases names only" ) .longOpt( "list-test-names-only" ); cli.bind( &ConfigData::listReporters ) .describe( "list all reporters" ) .longOpt( "list-reporters" ); return cli; }
int integrationtest_main(int argc, char *argv[]) { IntegrationTestCLData configData; Clara::CommandLine<IntegrationTestCLData> cli; cli.bind( &IntegrationTestCLData::help ) .describe( "display usage information" ) .shortOpt( "?") .shortOpt( "h") .longOpt( "help" ); cli.bind( &IntegrationTestCLData::license ) .describe( "prints out the license of the tests" ) .shortOpt( "l") .longOpt( "license" ) .longOpt( "licence" ); cli.bind( &IntegrationTestCLData::seed ) .describe( "value to seed the randomizer" ) .shortOpt( "S") .longOpt( "seed" ) .hint("the_seed"); cli.bind( &IntegrationTestCLData::tmp ) .describe( "path to a temporary directory" ) .shortOpt( "t") .longOpt( "tmp" ) .hint( "path" ); cli.bind( &IntegrationTestCLData::wpkg ) .describe( "path to the wpkg executable" ) .shortOpt( "w") .longOpt( "wpkg" ) .hint( "path" ); cli.bind( &IntegrationTestCLData::version ) .describe( "print out the wpkg project version these integration tests pertain to" ) .shortOpt( "V") .longOpt( "version" ); cli.parseInto( argc, argv, configData ); if( configData.help ) { cli.usage( std::cout, argv[0] ); Catch::Session().run(argc, argv); exit(1); } if( configData.version ) { std::cout << debian_packages_version_string() << std::endl; exit(1); } if( configData.license ) { license::license(); exit(1); } std::vector<std::string> arg_list; for( int i = 0; i < argc; ++i ) { arg_list.push_back( argv[i] ); } // by default we get a different seed each time; that really helps // in detecting errors! (I know, I wrote loads of tests before) unsigned int seed(static_cast<unsigned int>(time(NULL))); if( configData.seed != 0 ) { seed = static_cast<unsigned int>(configData.seed); remove_from_args( arg_list, "--seed", "-s" ); } srand(seed); std::cout << "wpkg[" << getpid() << "]:integrationtest: seed is " << seed << std::endl; // we can only have one of those for ALL the tests that directly // access the library... // (because the result is cached and thus cannot change) #if defined(MO_WINDOWS) _putenv_s("WPKG_SUBST", "f=/opt/wpkg|/m2osw/packages:h=usr/local/bin/wpkg"); #else putenv(const_cast<char *>("WPKG_SUBST=f=/opt/wpkg|/m2osw/packages:h=usr/local/bin/wpkg")); #endif if( !configData.tmp.empty() ) { wpkg_tools::set_tmp_dir( configData.tmp ); remove_from_args( arg_list, "--tmp", "-t" ); } if( !configData.wpkg.empty() ) { wpkg_tools::set_wpkg_tool( configData.wpkg ); remove_from_args( arg_list, "--wpkg", "-w" ); } std::vector<char *> new_argv; for( const auto& arg : arg_list ) { new_argv.push_back( const_cast<char *>(arg.c_str()) ); } return Catch::Session().run( static_cast<int>(new_argv.size()), &new_argv[0] ); }
std::vector<Clara::Parser::Token> parseInto( Clara::CommandLine<ConfigT>& cli, char const * (&argv)[size], ConfigT& config ) { return cli.parseInto( size, argv, config ); }