How-to: set options in samurai#

In this how-to guide, we will show you how to set various options in samurai. Options allow you to customize the behavior of samurai components. We use CLI11 library to handle options. Several options are already defined in samurai, but you can also define your own options.

Setting predefined options#

samurai provides several predefined options. You have to parse them to use them. Here is an example of how to use predefined options:

#include <samurai/samurai.hpp>

int main(int argc, char** argv)
{
    samurai::initialize("Simple example", argc, argv);

    samurai::finalize();
    return 0;
}

To see the available predefined options, you can run the compiled program with the --help flag:

./your_program --help

or

./your_program -h

Defining custom options#

You can also define your own options using the CLI11 library. Here is an example of how to define and use custom options in samurai:

#include <iostream>

#include <samurai/samurai.hpp>

int main(int argc, char** argv)
{
    auto& app = samurai::initialize("Add my options", argc, argv);

    int my_option = 42;
    bool my_flag  = false;

    app.add_option("--my-option", my_option, "An example of custom option")->capture_default_str()->group("Custom");
    app.add_flag("--my-flag", my_flag, "An example of custom flag")->group("Custom");

    SAMURAI_PARSE(argc, argv);

    std::cout << "my-option = " << my_option << std::endl;
    std::cout << "my-flag = " << std::boolalpha << my_flag << std::endl;

    samurai::finalize();
    return 0;
}

For more information on how to use CLI11 to define options, please refer to the CLI11 documentation.

Use a TOML configuration file#

With CLI11, you can also use a configuration file to set options. samurai supports TOML configuration files. Here is an example of how to use a TOML configuration file to set options:

my-option = 2
my-flag = true

In this example, we define two options: my-option and my-flag. The TOML file should be passed to the program using the --config flag:

./your_program --config config.toml