arg  1.2
a C++ command-line parser
arg_ex1.cc

A more complex example with help text

#include <arg.hh>
#include <iostream>
using namespace std;
string const version = "1.0";
int main(int argc, char ** argv)
{
// system parameters
// setup parser
arg::Parser parser;
parser.set_header("arg Testing Program v" + version);
parser.add_help("");
parser.add_help("available options are:");
int n;
parser.add_opt('n', "number").stow(n = 10)
.help("set number of nodes to INT", "INT")
string f;
int f_given = 0;
parser.add_opt('i', "input").stow(f)
.help("read data from FILE", "FILE")
.set(& f_given).once();
parser.add_opt_help();
parser.add_opt_version(version);
// parse command line
try {
parser.parse(argc, argv);
}
catch (arg::Error e) {
cout << "Error parsing command line: " << e.get_msg() << '\n';
return 1;
}
// check for parameter consistency
if (! f.size()) {
cout << "Need to specify the input file!\n";
return 1;
}
// output
cout << "The parameters are:\n"
<< "number = " << n << '\n'
<< "input = " << f << '\n';
return 0;
}
arg::Option::show_default
Option & show_default(bool do_show=true)
show default value in help
Definition: arg.cc:112
arg::Option::help
Option & help(std::string const &text, std::string const &var="")
help text
Definition: arg.cc:99
arg::Option::set
Option & set(int *var, int value=- 1)
set "* var" to "value"
Definition: arg.cc:78
arg::Parser
The command-line parser.
Definition: arg.hh:129
arg::Option::once
Option & once(int init=0)
can only be set once, with distinct value, "init"
Definition: arg.cc:85
arg::Parser::add_opt
Option & add_opt(int key, std::string const &name="", bool hide=false)
add an Option
Definition: arg.cc:268
arg::Parser::parse
void parse(int argc, char *argv[], bool ignore_unknown=false)
perform command-line parsing
Definition: arg.cc:292
arg::Parser::set_header
void set_header(std::string const &text)
set the header in help
Definition: arg.cc:355
arg::Option::stow
Option & stow(T &t)
stow value to streamable variable
Definition: arg.hh:271
arg.hh
Header file for arg library.
arg::Parser::add_help
void add_help(std::string const &msg)
add additional help text between option helps
Definition: arg.cc:263