Skip to content

How to Config CDCF

Yujia Li edited this page Aug 11, 2020 · 5 revisions

CDCF configures applications at startup using an cdcf_config or a user-defined subclass of that type. The config objects allow users to add custom types, to load modules, and to fine-tune the behavior of loaded modules with command line options or configuration files.

Define Config Struct in Code

The following code example how to define the config struct in code

class TestConfig :public CDCFConfig{

public:
    uint16_t my_port = 0;
    std::string my_host = "localhost";
    bool my_server_mode = false;

    TestConfig() {
        opt_group{custom_options_, "global"}
                .add(my_port, "port,p", "set port")
                .add(my_host, "host,H", "set node (ignored in server mode)")
                .add(my_server_mode, "server-mode,s", "enable server mode");
    }
};

In this example, my_port, my_host, my_server_mode is user-defined config data. We create a new “global” category in custom_options_}. Each following call to add then appends individual options to the category. The first argument to add is the associated variable. The second argument is the name for the parameter, optionally suffixed with a comma-separated single-character short name. The short name is only considered for CLI parsing and allows users to abbreviate commonly used option names. The third and final argument to add is a help text.

Input Config value by CLI

The following cli example show how to input the config defined in previous chapter via cli

test_program --port=8088 --host=localhost666 --server-mode

if cli parameters have "--server-mode" mean it is true value, otherwise it's false. CAF bool option do not support argument in CLI, so it will complain if you input --server-mode=true or --server-mode=false.

We also can use cli in short parameters as below:

test_program -p 8088 -H localhost666  -s

Input Config Value by INI File

The following ini file content example show how to input the config defined in previous chapter via int file

[global]
port=8089
host="localhost111"
server-mode=true