C++ Library containing a lot of needful things: Stack Trace, Command Line Parser, Resource Handling, Configuration Files, Unix Command Execution, Directories, Regular Expressions, Tokenizer, Function Trace, Standard Extensions.
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
96 lines
4.2 KiB
96 lines
4.2 KiB
11 years ago
|
/*! @file
|
||
|
|
||
|
@id $Id$
|
||
|
*/
|
||
|
// 1 2 3 4 5 6 7 8
|
||
|
// 45678901234567890123456789012345678901234567890123456789012345678901234567890
|
||
|
|
||
11 years ago
|
#include <mrw/args.hxx>
|
||
11 years ago
|
#ifdef MRW__OLD_PRE11_COMPILER
|
||
|
#include <mrw/vector.hxx>
|
||
|
#endif
|
||
11 years ago
|
|
||
|
void test_func() {
|
||
|
std::cout<<"test parameter was given on command line"<<std::endl;
|
||
|
}
|
||
|
|
||
|
int main(int argc, char** argv) try {
|
||
|
|
||
|
// option variables
|
||
|
bool flag(false);
|
||
|
int r(5);
|
||
|
std::string txt("Hello World");
|
||
|
int x(0), y(0);
|
||
|
|
||
11 years ago
|
std::string description("This is an example for argument processing. If the "
|
||
|
"description is very long, it is automatically "
|
||
|
"splitted over several lines and indented on each "
|
||
|
"new line. Arguments may have several parameters. "
|
||
|
"Help is generated automatically from the argument "
|
||
|
"declaration. The argument declaration contains long "
|
||
|
"and short options and a list of parameters.");
|
||
|
std::string returns("Returns a status of 0 on success and 1 on any error.");
|
||
|
|
||
11 years ago
|
// bind and evaluate options
|
||
11 years ago
|
#ifdef MRW__OLD_PRE11_COMPILER
|
||
11 years ago
|
mrw::args::parse(argc, argv, description, mrw::args::defaults()
|
||
11 years ago
|
<<mrw::args::decl("h", "help", "show this help",
|
||
|
mrw::args::decl::param_list()
|
||
|
<<mrw::args::help()
|
||
|
<<mrw::args::exit())
|
||
|
<<mrw::args::decl("r", "repeat", "number of repetitions",
|
||
|
mrw::args::decl::param_list()
|
||
|
<<mrw::args::param(r, "number"))
|
||
|
<<mrw::args::decl("f", "flag", "check a flag",
|
||
|
mrw::args::decl::param_list()
|
||
|
<<mrw::args::param(flag))
|
||
|
<<mrw::args::decl("t", "text", "pass a text",
|
||
|
mrw::args::decl::param_list()
|
||
|
<<mrw::args::param(txt, "text"))
|
||
|
<<mrw::args::decl("c", "coord", "add some 2D coordinates",
|
||
|
mrw::args::decl::param_list()
|
||
|
<<mrw::args::param(x, "x")
|
||
|
<<mrw::args::param(y, "y"))
|
||
|
<<mrw::args::decl("", "test", "call test_func, no shortcut",
|
||
|
mrw::args::decl::param_list()
|
||
|
<<mrw::args::func(test_func)),
|
||
|
returns);
|
||
11 years ago
|
#else
|
||
11 years ago
|
mrw::args::parse(argc, argv,
|
||
11 years ago
|
description,
|
||
11 years ago
|
{
|
||
|
{"h", "help", "show this help",
|
||
|
{mrw::args::help(), mrw::args::exit()}},
|
||
|
{"r", "repeat", "number of repetitions",
|
||
|
{mrw::args::param(r, "number")}},
|
||
|
{"f", "flag",
|
||
|
"check a flag; this is a boolean, so it is false by "
|
||
|
"default and true if the user specifies the flag on "
|
||
|
"the command line", {mrw::args::param(flag)}},
|
||
|
{"t", "text", "pass a text",
|
||
|
{mrw::args::param(txt, "text")}},
|
||
|
{"c", "coord",
|
||
|
"add some 2D coordinates; this is an example on how "
|
||
|
"you can add more than one parameter",
|
||
|
{mrw::args::param(x, "x"),
|
||
|
mrw::args::param(y, "y")}},
|
||
|
{"", "test", "call test_func, no shortcut",
|
||
|
{mrw::args::func(test_func)}},
|
||
|
},
|
||
11 years ago
|
returns);
|
||
11 years ago
|
#endif
|
||
|
|
||
|
// show the result
|
||
|
std::cout<<std::endl
|
||
|
<<"commandline is parsed, these are the settings now:"<<std::endl
|
||
|
<<"r = "<<r<<std::endl
|
||
|
<<"flag = "<<(flag?"yes":"no")<<std::endl
|
||
|
<<"txt = "<<txt<<std::endl
|
||
|
<<"coordinates = "<<x<<", "<<y<<std::endl;
|
||
|
|
||
|
return 0;
|
||
|
} catch (const std::exception& x) {
|
||
|
std::cerr<<"**** ERROR: "<<x.what()<<std::endl;
|
||
|
return 1;
|
||
|
}
|