C++ Option HandlingΒΆ

For C++, we recommend the use of Boost.Program_options. A basic example of how to use this library can be found in binary.cpp in the example project:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <iostream>

#include <stdlib.h>
#include <math.h>

#include <boost/program_options.hpp>

#include <rsb/Informer.h>
#include <rsb/Factory.h>

#include "library/MagicNumberTransformer.h"

using namespace std;
using namespace rsb;
using namespace buildsystemessentials::library;
namespace po = boost::program_options;

string outScope = "/default/scope";
int value = 0;

void handleCommandline(int argc, char *argv[]) {

    po::options_description options("Allowed options");
    options.add_options()("help,h", "Display a help message.")("scope,s",
            po::value < string > (&outScope), "Scope to output the result on")(
            "value,v", po::value<int>(&value)->required(),
            "The value to process.");

    // allow to give the value as a positional argument
    po::positional_options_description p;
    p.add("value", 1);

    po::variables_map vm;
    po::store(
            po::command_line_parser(argc, argv).options(options).positional(p).run(),
            vm);

    // first, process the help option
    if (vm.count("help")) {
        cout << options << "\n";
        exit(1);
    }

    // afterwards, let program options handle argument errors
    po::notify(vm);

    // you can do some additional validity checks here

}

int main(int argc, char *argv[]) {

    handleCommandline(argc, argv);

    // first get a factory instance that is used to create RSB domain objects
    Factory& factory = rsb::getFactory();

    // create an informer that is capable of sending events containing string
    // data on the scope "/example/informer".
    Informer<int>::Ptr informer = factory.createInformer<int>(Scope(outScope));

    // create data to send over the informer. Data is always maintained in
    // shared_ptr instances. Informer provides a typedef DataPtr of the
    // appropriate type according to its template parameter
    Informer<int>::DataPtr s(new int(MagicNumberTransformer().transform(value)));

    return EXIT_SUCCESS;

}