diff options
| author | Leitwolf | 2026-10-24 19:03:40 +0300 |
|---|---|---|
| committer | GitHub | 2026-10-24 19:03:40 +0300 |
| commit | 4f1c759cffcb14bd6511e1d7c5fbace682e24f2d (patch) | |
| tree | 415fe5d48a5b48650bba3747beaf245d727c4f61 /examples/232-Cfg-CustomMain.cpp | |
| parent | 83c6fc3ac13abe83fa39f7b5f8559ccaf4412233 (diff) | |
| parent | 622188f9c08e4af35941c146979883ed4882410e (diff) | |
| download | catch3-4f1c759cffcb14bd6511e1d7c5fbace682e24f2d.tar.gz | |
Merge pull request #15 from Progmasoft/feature/catch3-runner
Replace Catch2's runner with the Catch3 runner
Diffstat (limited to 'examples/232-Cfg-CustomMain.cpp')
| -rw-r--r-- | examples/232-Cfg-CustomMain.cpp | 40 |
1 files changed, 22 insertions, 18 deletions
diff --git a/examples/232-Cfg-CustomMain.cpp b/examples/232-Cfg-CustomMain.cpp index 27040993..6ef05fe7 100644 --- a/examples/232-Cfg-CustomMain.cpp +++ b/examples/232-Cfg-CustomMain.cpp @@ -7,35 +7,39 @@ // SPDX-License-Identifier: BSL-1.0 // 232-Cfg-CustomMain.cpp -// Show how to use custom main and add a custom option to the CLI parser +// Show how to use custom main and read an option of your own before the +// rest of the command line goes to the Catch3 runner. +// +// The runner starts this binary again for its worker processes, with the +// same `main`: what happens before `run` happens in every worker too, which +// is where global set-up belongs. #include <catch2/catch_session.hpp> +#include <cstdlib> #include <iostream> +#include <string> +#include <vector> int main(int argc, char** argv) { Catch::Session session; // There must be exactly one instance int height = 0; // Some user variable you want to be able to set - // Build a new parser on top of Catch2's - using namespace Catch::Clara; - auto cli - = session.cli() // Get Catch2's command line parser - | Opt( height, "height" ) // bind variable to a new option, with a hint string - ["--height"] // the option names it will respond to - ("how high?"); // description string for the help output + // Take our own option out of the command line; the runner reads the rest. + // Workers are started with the runner's options only, so a value they + // need too belongs in the environment, which they inherit. + std::vector<char const*> arguments; + for ( int i = 0; i < argc; ++i ) { + std::string const argument = argv[i]; + if ( argument.rfind( "--height=", 0 ) == 0 ) { + height = std::atoi( argument.c_str() + 9 ); + } else { + arguments.push_back( argv[i] ); + } + } - // Now pass the new composite back to Catch2 so it uses that - session.cli( cli ); - - // Let Catch2 (using Clara) parse the command line - int returnCode = session.applyCommandLine( argc, argv ); - if( returnCode != 0 ) // Indicates a command line error - return returnCode; - - // if set on the command line then 'height' is now set at this point std::cout << "height: " << height << '\n'; - return session.run(); + return session.run( static_cast<int>( arguments.size() ), arguments.data() ); } |