Limbo
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Limbo.ProgramOptions

Table of Contents

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");
// add user-defined options
desc.add_option(Value<bool>("-help", &help, "print help message").toggle(true).default_value(false).toggle_value(true).help(true)) // specify help option
.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)) // the floating point option is a required option, so user must provide it
.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";
}
// print help message
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
3 # test 1: help message
4 ./a.out -help
5 # test 2: integer and floating point number
6 ./a.out -i 20 -f 1.5
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

1 parsing succeeded
2 help = true
3 i = 100
4 fp = 0
5 vInteger =

Output 2

1 parsing succeeded
2 help = false
3 i = 20
4 fp = 1.5
5 vInteger =

Output 3

1 parsing succeeded
2 help = false
3 i = 20
4 fp = 1.5
5 vInteger = 10 30 50

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)
{
using namespace limbo;
using namespace limbo::programoptions;
bool help = false;
int i = 0;
double fp = 0;
std::string str;
std::vector<int> vInteger;
std::vector<std::string> vString;
ProgramOptions po ("My options");
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::string>("-s", &str, "a string").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;
assert_msg(help == false, "help turned to true");
return 0;
}

All Examples

References