Introduction
The package allows programs obtain command line options, such as (name, value) pairs, from user input without writing wordy parsing blocks in the source code. It supports various data types, including integer, floating point number, char, boolean, string, vector, etc. It provides easy API for developers to define command line options to their programs with a highly extendable manner, i.e., only single line of code for each option. No need to write any code for parsing. It also offers detailed error reporting scheme that print incorrect and missing arguments.
Examples
Example 1
See documented version: test/programoptions/test_ProgramOptions_simple.cpp
#include <iostream>
#include <string>
#include <vector>
int main(
int argc,
char** argv)
{
bool help = false;
int i = 0;
double fp = 0;
std::vector<int> vInteger;
po_type desc ("My options");
desc.add_option(Value<bool>("-help", &help, "print help message").toggle(true).default_value(false).toggle_value(true).help(true))
.add_option(Value<int>("-i", &i, "an integer").default_value(100, "1.0.0"))
.add_option(Value<double>("-f", &fp, "a floating point").required(true))
.add_option(Value<std::vector<int> >("-vi", &vInteger, "vector of integers"))
;
try
{
bool flag = desc.parse(argc, argv);
if (flag)
std::cout << "parsing succeeded\n";
}
catch (std::exception& e)
{
std::cout << "parsing failed\n";
std::cout << e.what() << "\n";
}
if (help)
{
std::cout << desc << "\n";
return 1;
}
std::cout << "help = " << ((help)? "true" : "false") << std::endl;
std::cout << "i = " << i << std::endl;
std::cout << "fp = " << fp << std::endl;
std::cout << "vInteger = ";
for (std::vector<int>::const_iterator it = vInteger.begin(); it != vInteger.end(); ++it)
std::cout << *it << " ";
std::cout << std::endl;
return 0;
}
Compiling and running commands (assuming LIMBO_DIR is valid and limbo library has been properly installed)
1 # linkage is necessary for Limbo.ProgramOptions
2 g++ test_ProgramOptions_simple.cpp -I $LIMBO_DIR/include -L $LIMBO_DIR/lib -lprogramoptions
5 # test 2: integer and floating point number
7 # test 3: vector of integers; the numbers are appending to the container
8 ./a.out -i 20 -f 1.5 -vi 10 -vi 30 -vi 50
Output 1
Output 2
Output 3
Example 2
See documented version: test/programoptions/test_ProgramOptions.cpp
#include <iostream>
#include <string>
#include <vector>
using std::cout;
using std::endl;
int main(
int argc,
char** argv)
{
bool help = false;
int i = 0;
double fp = 0;
std::string str;
std::vector<int> vInteger;
std::vector<std::string> vString;
po.add_option(
Value<bool>(
"-help", &help,
"print help message").toggle(
true).default_value(
false).toggle_value(
true).help(
true))
.add_option(
Value<int>(
"-i", &i,
"an integer").default_value(100,
"1.0.0"))
.add_option(
Value<double>(
"-f", &fp,
"a floating point").required(
true))
.add_option(
Value<std::vector<int> >(
"-vi", &vInteger,
"vector of integers"))
.add_option(
Value<std::vector<std::string> >(
"-vs", &vString,
"vector of string"))
;
po.print();
try
{
bool flag = po.parse(argc, argv);
if (flag)
cout << "parsing succeeded\n";
}
catch (std::exception& e)
{
cout << "parsing failed\n";
cout << e.what() << "\n";
}
cout << "help = " << ((help)? "true" : "false") << endl;
cout << "i = " << i << endl;
cout << "fp = " << fp << endl;
cout << "str = " << str << endl;
cout << "vInteger = ";
for (std::vector<int>::const_iterator it = vInteger.begin(); it != vInteger.end(); ++it)
cout << *it << " ";
cout << endl;
cout << "vString = ";
for (std::vector<std::string>::const_iterator it = vString.begin(); it != vString.end(); ++it)
cout << *it << " ";
cout << endl;
return 0;
}
All Examples
References