yagol was designed to be simple yet powerful, which this example hopes to prove. In this example, the application is making use of only the most basic features, specifically, using it to process only the command line for a few simple types. See Features.t.cpp for many more options, such as environment variables and resource files.

The application

This is Minimal.cpp in its entirety. It can be found in the test directory.

Source code

#ifndef YagolAppOptionSet_h
#include "YagolAppOptionSet.h"
#endif

#include <iostream>
#include <string>
#include <vector>


template <class T>
ostream& operator<<(ostream& os, const vector<T>& vec)
{
    os << "(";
    typename vector<T>::const_iterator stop = vec.end();
    for (typename vector<T>::const_iterator it = vec.begin(); it != stop; ++it) {
        if (it != vec.begin()) {
            os << ", ";
        }
        os << *it;
    }
    os << ")";
    return os;
}


int main(int argc, char** argv)
{
    bool   b = true;
    double d = 3.1415;
    string s = "plethora";
    float  f = 2.818;
    int    i = 7;
    vector<int> v;
    v.push_back(14);
    v.push_back(28);
    v.push_back(56);
    v.push_back(112);
    
    const string appName("Minimal");
    const string appDesc("A minimal application");
    const int appSettings = yagol::AppOptionSet::STRICT_ARGUMENTS | yagol::AppOptionSet::HELP_ON_ERROR;

    yagol::AppOptionSet opts(appSettings, appName, appDesc);
    
    opts.addOption("b", "a boolean option",        &b);
    opts.addOption("d", "a double option",         &d);
    opts.addOption("s", "a string option",         &s);
    opts.addOption("f", "a float option",          &f);
    opts.addOption("i", "an int option",           &i);
    opts.addOption("v", "a vector of ints option", &v);

    opts.process(argc, argv);

    cout << "-----------------------" << endl;
    cout << "values after processing" << endl;
    cout << "-----------------------" << endl;
    cout << "    b = " << b << endl;
    cout << "    d = " << d << endl;
    cout << "    s = " << s << endl;
    cout << "    f = " << f << endl;
    cout << "    i = " << i << endl;
    cout << "    v = " << v << endl;
    
    return 0;
}

Compiling

This application can be compiled with the following command:

c++ -I. -I/usr/local/include/yagol -g -O2 -c Minimal.cpp
c++  -g -O2 -o Minimal Minimal.o -lyagol

Note that libyagol.a is installed by default to /usr/local/lib, and therefore my LD_LIBRARY_PATH is set to /usr/local/lib:..

The code for the application follows.

Execution

The following are executions of the application, with different command lines. Note that for clarity, some output has been edited, when irrelevant.

Input

% ./Minimal

Output

    b = 1
    d = 3.1415
    s = plethora
    f = 2.818
    i = 7
    v = (14, 28, 56, 112)

Input
% ./Minimal --help

Output

Usage: Minimal 
    A minimal application

Options:
    --b    a boolean option (default = true)
    --d    a double option (default = 3.1415)
    --s    a string option (default = "plethora")
    --f    a float option (default = 2.818)
    --i    an int option (default = 7)
    --v    a vector of ints option (default = 14, 28, 56, 112)

Input

% ./Minimal --b

Output

    b = 1
    d = 3.1415
    s = plethora
    f = 2.818
    i = 7
    v = (14, 28, 56, 112)

Input

% ./Minimal --nob

Output

-----------------------
values after processing
-----------------------
    b = 0
    d = 3.1415
    s = plethora
    f = 2.818
    i = 7
    v = (14, 28, 56, 112)

Input

% ./Minimal --d 6.02E23

Output

-----------------------
values after processing
-----------------------
    b = 1
    d = 6.02e+23
    s = plethora
    f = 2.818
    i = 7
    v = (14, 28, 56, 112)

Input

% ./Minimal --f 

Output

ERROR: argument expected for f
Usage: Minimal 
    A minimal application

Options:
    --b    a boolean option (default = true)
    --d    a double option (default = 3.1415)
    --s    a string option (default = "plethora")
    --f    a float option (default = 2.818)
    --i    an int option (default = 7)
    --v    a vector of ints option (default = 14, 28, 56, 112)

Input

% ./Minimal --f --d 6.02E23

Output

ERROR: argument '--d' for option f appears to be a tag
Usage: Minimal 
    A minimal application

Options:
    --b    a boolean option (default = true)
    --d    a double option (default = 3.1415)
    --s    a string option (default = "plethora")
    --f    a float option (default = 2.818)
    --i    an int option (default = 7)
    --v    a vector of ints option (default = 14, 28, 56, 112)

Input

% ./Minimal --v 33 66 99 --end-of-v

Output

    b = 1
    d = 3.1415
    s = plethora
    f = 2.818
    i = 7
    v = (33, 66, 99)

Input

% ./Minimal --v 33 66 99 --d --end-of-v

Output

ERROR: argument '--d' for option v appears to be a tag
Usage: Minimal 
    A minimal application

Options:
    --b    a boolean option (default = true)
    --d    a double option (default = 3.1415)
    --s    a string option (default = "plethora")
    --f    a float option (default = 2.818)
    --i    an int option (default = 7)
    --v    a vector of ints option (default = 14, 28, 56, 112)

Input

% ./Minimal --v 33 66 99

Output

    b = 1
    d = 3.1415
    s = plethora
    f = 2.818
    i = 7
    v = (33, 66, 99)

Input

% ./Minimal --s oneword

Output

    b = 1
    d = 3.1415
    s = oneword
    f = 2.818
    i = 7
    v = (14, 28, 56, 112)

Input

% ./Minimal --s "multiple words here"

Output

    b = 1
    d = 3.1415
    s = multiple words here
    f = 2.818
    i = 7
    v = (14, 28, 56, 112)

Input

% ./Minimal --s 'multiple words here'

Output

    b = 1
    d = 3.1415
    s = multiple words here
    f = 2.818
    i = 7
    v = (14, 28, 56, 112)

Input

% ./Minimal --s 'multiple words here'

Output

    b = 1
    d = 3.1415
    s = multiple words here
    f = 2.818
    i = 7
    v = (14, 28, 56, 112)