b2api
B2000++ API Reference Manual, VERSION 4.6
 
Loading...
Searching...
No Matches
b2000::cmdline Namespace Reference

Classes

class  CmdArg
 
class  CmdArgBase
 
class  CmdArgList
 
class  CmdArgListBase
 
class  CmdLine
 
class  CmdOpt
 
class  CmdOptBase
 
class  CmdOptDictionary
 
class  CmdOptList
 
class  CmdOptRTable
 

Detailed Description

Implementation of a C++ command line parser to replace the getopt functionality. These classes can be used in stand-alone programs that are executed on the command-line.

A command-line consists of the program name, the command-line options, followed by the command-line arguments. Command-line options (or flags) are optional, and are given by for instance in the form "-optionname=optionvalue". Options may be specified in any order, but must be specified before the arguments. Command-line arguments are given in a pre-defined sequence, after the options.

Example

A utility that extracts solution and gradient data from a B2000++ database.

In the first step, for each command-line option, a b2000::cmdline::CmdOpt instance must be created, and for each mandatory argument, a b2000::cmdline::CmdArg instance must be created.

The options:

int
main(int argc, char* argv[]) {
"h", "", "Print a one-line help message and exit.");
"help", "", "Print this help message and exit.");
"version", "", "Print the program version and exit.");
"case", "CASE", "Only exctract fields for load case CASE.", -1);
"set", "", "Set a (key,value) pair of the form key=value or "
"key='value1 value2 ...'.");
"exclude", "FIELD_NAME", "Exclude a field. May be specified "
"multiple times.");
Definition b2cmdline.H:286
Definition b2cmdline.H:214
Definition b2cmdline.H:185

The program has a single command-line argument:

"DBNAME", "The B2000++ database to extract from.");
Definition b2cmdline.H:530

In the second step, the option and argument instances are used to initialize, together with the arguments argc and argv, to initialize the Cmdline object:

b2000::cmdline::CmdLine cmdline(argc, argv);
cmdline <<
short_help << long_help << load_case
set << field_list << db_name;
Definition b2cmdline.H:641

In the third step, the command-line options and arguments are parsed, and the CmdOpt and CmdArg instances are set. This is done by means of the CmdLine::parse() function.

try {
cmdline.parse(true);
} catch (b2000::Exception& e) {
std::cerr << e << std::endl;
std::cerr << "usage: ";
cmdline.print_short_desc(std::cerr);
std::cerr << std::endl;
std::cerr << "Try '" << cmdline.get_command_name() <<
" --help' for more information." << std::endl;
std::exit(1);
}
Definition b2exception.H:131

In the final step, the individual options and arguments are checked.

if (short_help) {
std::cout << "usage: ";
cmdline.print_short_desc(std::cout);
std::cout << std::endl;
std::exit(0);
}
if (long_help) {
std::cout << std::endl << "Usage: ";
cmdline.print_short_desc(std::cout);
std::cout << std::endl << std::endl;
cmdline.print_long_desc(std::cout);
std::exit(0);
}
if (load_case != -1) {
std::cerr << "The load case is " << load_case << ".";
}
{
const int max_cycle = dict.get_int("MAX_CYCLE", -1);
std::cerr << "Maximum cycle is" << max_cycle << std::endl;
}
for (size_t i = 0; i != field_list.size(); ++i) {
std::cerr << "Excluding field " << field_list[i] << std::endl;
}