Arg< T > Class Template Reference

#include <Pt/Arg.h>

Read and extract command-line options. More...

Inherits ArgBaseT< T >.

Public Member Functions

 Arg (const T &def=T())
 Constructor with initial value.
 
 Arg (int &argc, char *argv[], char ch, const T &def=T())
 Extract short option. More...
 
 Arg (int &argc, char *argv[], const char *str, const T &def=T())
 Extract long option. More...
 
 Arg (int &argc, char *argv[])
 Extracts the next parameter.
 
const T & get () const
 Returns the value.
 
bool isSet () const
 Returns true if the option is set, false if default is used.
 
bool set (int &argc, char *argv[], char ch)
 Extract short option. More...
 
bool set (int &argc, char *argv[], const char *str)
 Extract long option. More...
 
bool set (int &argc, char *argv[])
 Reads next parameter and removes it.
 

Related Functions

template<typename T >
std::ostream & operator<< (std::ostream &out, const Arg< T > &arg)
 Write Arg to an std::ostream.
 

Detailed Description

template<typename T>
class Pt::Arg< T >

Arg objects can be used to process command line options passed to the main function of the program. A syntax for short-named and long-named options is supported. Short-named options start with a single hypen followd by a character (-O). Optionally, a value follows directly (-Ofoo) or separated by whitespace(-O foo). Alternatively, option names can be consist of any character sequence to support unix style options(–option) and windows style options (/OPTION). An optional value follows either separated by whitespace (–option yes) or an equal character (/OPTION=yes). Note that constructing an Arg from with the character 'n' or with the string "-n" are equivalent.

The template parameter of the Arg class is the argument value type, which must be streamable, i.e. the operator >> (std::istream&, T&) must be defined for the type T. When an Arg is constructed, the operator will be used to extract the value from the command-line string. the next example demonstrates this:

int main(int argc, char* argv[])
{
Pt::Arg<int> option_n(argc, argv, 'n', 0);
std::cout << "value for -n: " << option_n << endl;
}

Options are removed from the option-list, so programs can easily check, if there are parameters left, after all options were extracted. A specialization exists for boolean parameters. This implements a switch, which is on, if the option is present and off, if it is missing. The option consists, in this case, only of a command line flag without a value. Boolean parameters can also be grouped, so -abc is processed like -a -b -c.

Pt::Arg<bool> debug(argc, argv, "--debug");
if (debug)
std::cout << "debug flag is set" << std::endl;

The example shown above not only shows a boolean parameter, but also how long-named options are handled, in this case "--debug".

Constructor & Destructor Documentation

Arg ( int &  argc,
char *  argv[],
char  ch,
const T &  def = T() 
)
Parameters
argc1. parameter of main
argv2. of main
choptioncharacter
defdefault-value

Example:

Pt::Arg<unsigned> offset(argc, argv, 'o', 0);
unsigned value = offset.getValue();
Arg ( int &  argc,
char *  argv[],
const char *  str,
const T &  def = T() 
)

Long option names starting with "--". This (and more) is supported here. Instead of giving a single option-character, you specify a string.

Example:

Pt::Arg<int> option_number(argc, argv, "--number", 0);
std::cout << "number =" << option_number.getValue() << std::endl;

Member Function Documentation

bool set ( int &  argc,
char *  argv[],
char  ch 
)
Parameters
argc1. parameter of main
argv2. of main
choptioncharacter

Example:

offset.set(argc, argv, 'o');
unsigned value = offset.getValue();
bool set ( int &  argc,
char *  argv[],
const char *  str 
)

GNU defines long options starting with "--". This (and more) is supported here. Instead of giving a single option-character, you specify a string.

Example:

Pt::Arg<int> option_number;
number.set(argc, argv, "--number");
std::cout << "number =" << option_number.getValue() << std::endl;