diff --git a/include/clara.hpp b/include/clara.hpp index 51be3e3..38ab228 100644 --- a/include/clara.hpp +++ b/include/clara.hpp @@ -730,6 +730,19 @@ namespace detail { } }; + struct Version : Opt { + Version( bool &showVersionFlag ) + : Opt([&]( bool flag ) { + showVersionFlag = flag; + return ParserResult::ok( ParseResultType::ShortCircuitAll ); + }) + { + static_cast( *this ) + ("display version") + ["-v"]["--version"] + .optional(); + } + }; struct Parser : ParserBase { @@ -913,6 +926,9 @@ using detail::ExeName; // Convenience wrapper for option parser that specifies the help option using detail::Help; +// Convenience wrapper for option parser that specifies the version option +using detail::Version; + // enum of result types from a parse using detail::ParseResultType; diff --git a/src/ClaraTests.cpp b/src/ClaraTests.cpp index 1251e9b..81f0fd5 100644 --- a/src/ClaraTests.cpp +++ b/src/ClaraTests.cpp @@ -96,8 +96,10 @@ TEST_CASE( "Combined parser" ) { Config config; bool showHelp = false; + bool showVersion = false; auto parser = Help( showHelp ) + | Version(showVersion) | Opt( config.m_rngSeed, "time|value" ) ["--rng-seed"]["-r"] ("set a specific seed for random numbers" ) @@ -121,6 +123,7 @@ TEST_CASE( "Combined parser" ) { "\n" "where options are:\n" " -?, -h, --help display usage information\n" + " -v, --version display version\n" " --rng-seed, -r set a specific seed for random numbers\n" " -n, --name the name to use\n" " -f, --flag a flag to set\n" @@ -136,6 +139,7 @@ TEST_CASE( "Combined parser" ) { REQUIRE( config.m_value == 123.45 ); REQUIRE( config.m_tests == std::vector { "test1", "test2" } ); CHECK( showHelp == false ); + CHECK( showVersion == false); } SECTION( "help" ) { auto result = parser.parse( Args{ "TestApp", "-?", "-n:NotSet" } ); @@ -143,6 +147,15 @@ TEST_CASE( "Combined parser" ) { CHECK( result.value().type() == ParseResultType::ShortCircuitAll ); CHECK( config.m_name == "" ); // We should never have processed -n:NotSet CHECK( showHelp == true ); + CHECK( showVersion == false); + } + SECTION( "version" ) { + auto result = parser.parse( Args{ "TestApp", "-v", "-n:NotSet" } ); + CHECK( result ); + CHECK( result.value().type() == ParseResultType::ShortCircuitAll ); + CHECK( config.m_name == "" ); // We should never have processed -n:NotSet + CHECK( showHelp == false ); + CHECK( showVersion == true ); } }