2017-08-24 15:21:36 +02:00
< a id = "top" > < / a >
2013-06-08 00:06:54 +02:00
# Supplying main() yourself
2013-06-07 20:09:38 +02:00
2013-06-08 00:06:54 +02:00
The easiest way to use Catch is to let it supply ```main()``` for you and handle configuring itself from the command line.
This is achieved by writing ```#define CATCH_CONFIG_MAIN``` before the ```#include "catch.hpp"``` in *exactly one* source file.
Sometimes, though, you need to write your own version of main(). You can do this by writing ```#define CATCH_CONFIG_RUNNER``` instead. Now you are free to write ```main()``` as normal and call into Catch yourself manually.
You now have a lot of flexibility - but here are three recipes to get your started:
## Let Catch take full control of args and config
If you just need to have code that executes before and/ or after Catch this is the simplest option.
```c++
#define CATCH_CONFIG_RUNNER
#include "catch.hpp"
2017-06-04 21:39:27 +02:00
int main( int argc, char* argv[] ) {
2013-06-08 00:06:54 +02:00
// global setup...
int result = Catch::Session().run( argc, argv );
// global clean-up...
2017-06-27 12:37:42 +02:00
return result;
2013-06-08 00:06:54 +02:00
}
```
## Amending the config
2018-03-07 10:08:35 +01:00
If you still want Catch to process the command line, but you want to programmatically tweak the config, you can do so in one of two ways:
2013-06-08 00:06:54 +02:00
```c++
#define CATCH_CONFIG_RUNNER
#include "catch.hpp"
2017-01-19 17:05:01 +01:00
int main( int argc, char* argv[] )
2013-06-08 00:06:54 +02:00
{
2016-06-15 21:01:55 +02:00
Catch::Session session; // There must be exactly one instance
2017-11-02 19:01:09 +01:00
2013-06-08 00:06:54 +02:00
// writing to session.configData() here sets defaults
2013-07-02 20:42:21 +02:00
// this is the preferred way to set them
2017-11-02 19:01:09 +01:00
2013-06-08 00:06:54 +02:00
int returnCode = session.applyCommandLine( argc, argv );
if( returnCode != 0 ) // Indicates a command line error
2018-08-28 16:17:37 +02:00
return returnCode;
2017-11-02 19:01:09 +01:00
2013-06-08 00:06:54 +02:00
// writing to session.configData() or session.Config() here
// overrides command line args
2013-07-02 20:42:21 +02:00
// only do this if you know you need to
2013-06-08 00:06:54 +02:00
2017-01-31 20:48:14 +01:00
int numFailed = session.run();
2017-11-02 19:01:09 +01:00
2017-06-27 12:37:42 +02:00
// numFailed is clamped to 255 as some unices only use the lower 8 bits.
// This clamping has already been applied, so just return it here
// You can also do any post run clean-up here
return numFailed;
2013-06-08 00:06:54 +02:00
}
```
Take a look at the definitions of Config and ConfigData to see what you can do with them.
To take full control of the config simply omit the call to ```applyCommandLine()```.
## Adding your own command line options
2017-11-02 18:14:32 +01:00
Catch embeds a powerful command line parser called [Clara ](https://github.com/philsquared/Clara ).
As of Catch2 (and Clara 1.0) Clara allows you to write _composable_ option and argument parsers,
so extending Catch's own command line options is now easy.
```c++
#define CATCH_CONFIG_RUNNER
#include "catch.hpp"
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 Catch's
2018-01-23 08:44:09 +01:00
using namespace Catch::clara;
2017-11-02 18:14:32 +01:00
auto cli
= session.cli() // Get Catch's composite command line parser
| Opt( height, "height" ) // bind variable to a new option, with a hint string
["-g"]["--height"] // the option names it will respond to
("how high?"); // description string for the help output
// Now pass the new composite back to Catch so it uses that
session.cli( cli );
// Let Catch (using Clara) parse the command line
int returnCode = session.applyCommandLine( argc, argv );
if( returnCode != 0 ) // Indicates a command line error
2018-08-28 16:17:37 +02:00
return returnCode;
2017-11-02 18:14:32 +01:00
// if set on the command line then 'height' is now set at this point
if( height > 0 )
std::cout < < "height: " < < height < < std::endl ;
return session.run();
}
```
See the [Clara documentation ](https://github.com/philsquared/Clara/blob/master/README.md ) for more details.
2013-06-07 22:31:28 +02:00
2018-01-26 20:27:19 +01:00
## Version detection
Catch provides a triplet of macros providing the header's version,
* `CATCH_VERSION_MAJOR`
* `CATCH_VERSION_MINOR`
* `CATCH_VERSION_PATCH`
these macros expand into a single number, that corresponds to the appropriate
part of the version. As an example, given single header version v2.3.4,
the macros would expand into `2` , `3` , and `4` respectively.
2013-06-07 20:09:38 +02:00
---
2017-08-24 15:33:38 +02:00
[Home ](Readme.md#top )