Limbo 3.5.4
Loading...
Searching...
No Matches
Limbo.ProgramOptions

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;
}
Top API for Limbo.ProgramOptions.
top API to parse program options
a generic class for various data values
int main()

Compiling and running commands (assuming LIMBO_DIR is valid and limbo library has been properly installed)

# linkage is necessary for Limbo.ProgramOptions
g++ test_ProgramOptions_simple.cpp -I $LIMBO_DIR/include -L $LIMBO_DIR/lib -lprogramoptions
# test 1: help message
./a.out -help
# test 2: integer and floating point number
./a.out -i 20 -f 1.5
# test 3: vector of integers; the numbers are appending to the container
./a.out -i 20 -f 1.5 -vi 10 -vi 30 -vi 50

Output 1

parsing succeeded
help = true
i = 100
fp = 0
vInteger =

Output 2

parsing succeeded
help = false
i = 20
fp = 1.5
vInteger =

Output 3

parsing succeeded
help = false
i = 20
fp = 1.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;
limboAssertMsg(help == false, "help turned to true");
return 0;
}
#define limboAssertMsg(condition, args...)
custom assertion with message
Definition AssertMsg.h:24
namespace for Limbo.ProgramOptions
namespace for Limbo

All Examples

References