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.
#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; }
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.
The following are executions of the application, with different command lines. Note that for clarity, some output has been edited, when irrelevant.
% ./Minimal
b = 1 d = 3.1415 s = plethora f = 2.818 i = 7 v = (14, 28, 56, 112)
% ./Minimal --help
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)
% ./Minimal --b
b = 1 d = 3.1415 s = plethora f = 2.818 i = 7 v = (14, 28, 56, 112)
% ./Minimal --nob
----------------------- values after processing ----------------------- b = 0 d = 3.1415 s = plethora f = 2.818 i = 7 v = (14, 28, 56, 112)
% ./Minimal --d 6.02E23
----------------------- values after processing ----------------------- b = 1 d = 6.02e+23 s = plethora f = 2.818 i = 7 v = (14, 28, 56, 112)
% ./Minimal --f
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)
% ./Minimal --f --d 6.02E23
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)
% ./Minimal --v 33 66 99 --end-of-v
b = 1 d = 3.1415 s = plethora f = 2.818 i = 7 v = (33, 66, 99)
% ./Minimal --v 33 66 99 --d --end-of-v
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)
% ./Minimal --v 33 66 99
b = 1 d = 3.1415 s = plethora f = 2.818 i = 7 v = (33, 66, 99)
% ./Minimal --s oneword
b = 1 d = 3.1415 s = oneword f = 2.818 i = 7 v = (14, 28, 56, 112)
% ./Minimal --s "multiple words here"
b = 1 d = 3.1415 s = multiple words here f = 2.818 i = 7 v = (14, 28, 56, 112)
% ./Minimal --s 'multiple words here'
b = 1 d = 3.1415 s = multiple words here f = 2.818 i = 7 v = (14, 28, 56, 112)
% ./Minimal --s 'multiple words here'
b = 1 d = 3.1415 s = multiple words here f = 2.818 i = 7 v = (14, 28, 56, 112)