Limbo
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
test_ProgramOptions_simple.cpp
Go to the documentation of this file.
1 
8 #include <iostream>
9 #include <string>
10 #include <vector>
12 
19 int main(int argc, char** argv)
20 {
21  bool help = false;
22  int i = 0;
23  double fp = 0;
24  std::vector<int> vInteger;
25 
28  po_type desc ("My options");
29  // add user-defined options
30  desc.add_option(Value<bool>("-help", &help, "print help message").toggle(true).default_value(false).toggle_value(true).help(true)) // specify help option
31  .add_option(Value<int>("-i", &i, "an integer").default_value(100, "1.0.0"))
32  .add_option(Value<double>("-f", &fp, "a floating point").required(true)) // the floating point option is a required option, so user must provide it
33  .add_option(Value<std::vector<int> >("-vi", &vInteger, "vector of integers"))
34  ;
35 
36  try
37  {
38  bool flag = desc.parse(argc, argv);
39  if (flag)
40  std::cout << "parsing succeeded\n";
41  }
42  catch (std::exception& e)
43  {
44  std::cout << "parsing failed\n";
45  std::cout << e.what() << "\n";
46  }
47 
48  // print help message
49  if (help)
50  {
51  std::cout << desc << "\n";
52  return 1;
53  }
54 
55  std::cout << "help = " << ((help)? "true" : "false") << std::endl;
56  std::cout << "i = " << i << std::endl;
57  std::cout << "fp = " << fp << std::endl;
58  std::cout << "vInteger = ";
59  for (std::vector<int>::const_iterator it = vInteger.begin(); it != vInteger.end(); ++it)
60  std::cout << *it << " ";
61  std::cout << std::endl;
62 
63  return 0;
64 }
Top API for Limbo.ProgramOptions.
int main(int argc, char **argv)
test simple usage of program options, such as help, boolean, integer, floating point number...
a generic class for various data values
top API to parse program options