Arg< T > Class Template Reference

#include <Pt/Arg.h>

Command-line option extracted from argc and argv. More...

Public Member Functions

 Arg (const T &def=T())
 Constructs with a default value.
 Arg (int &argc, char *argv[], char ch, const T &def=T())
 Extracts a short option from argc and argv.
 Arg (int &argc, char *argv[], const char *str, const T &def=T())
 Extracts a long option from argc and argv.
 Arg (int &argc, char *argv[])
 Extracts the next positional argument from argv.
bool set (int &argc, char *argv[], char ch)
 Extracts a short option from argc and argv.
bool set (int &argc, char *argv[], const char *str)
 Extracts a long option from argc and argv.
bool set (int &argc, char *argv[])
 Extracts the next positional argument from argv.
const T & get () const
 Returns the value.
bool isSet () const
 Returns true if the option is set, false if default is used.

(Note that these are not member symbols.)

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 reads one option out of the argument vector of main and removes it, so later parameters are whatever is left. Construct it with argc, argv, the option name, and a default value. After construction, get() is the extracted value or the default. isSet() is true only when the option was present. Streaming an Arg writes get().

T is the value type. Extraction uses operator>> on an istringstream, so T must be default-constructible and extractable. int, unsigned, std::string and const char* are the usual cases. const char* and std::string copy the argument text without parsing.

A short option is a hyphen and one character: -n. The value may follow in the same argument (-n42) or in the next (-n 42). A long option is any prefix string the caller passes, commonly --name or /NAME. The value may follow after whitespace (--name 42) or after an equals sign (--name=42). Passing the character 'n' and passing the string "-n" select the same short option.

Each successful extraction deletes that option, and its value if it occupied a separate argument, from argv and shrinks argc. Unrecognized arguments stay in place. After every Arg of interest has been constructed, argv still holds the program name and any leftover operands.

int main(int argc, char* argv[])
{
Pt::Arg<int> n(argc, argv, 'n', 0);
std::cout << "value for -n: " << n << std::endl;
}
Command-line option extracted from argc and argv.
Definition Arg.h:211

Arg<bool> is a switch, not a parsed value. Presence of the flag sets it to true. There is no option argument. Short switches group: -abc is -a, -b and -c. A short switch can be turned off explicitly with -x-.

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

Constructing without argc and argv leaves the default. Call set() later to extract. set() does nothing if isSet() is already true, so the first match wins. A constructor that takes only argc and argv extracts the next positional argument, not a named option.

Constructor & Destructor Documentation

◆ Arg() [1/2]

template<typename T>
Arg ( int & argc,
char * argv[],
char ch,
const T & def = T() )
Parameters
argcArgument count from main.
argvArgument vector from main.
chShort option character.
defValue used when the option is absent.

◆ Arg() [2/2]

template<typename T>
Arg ( int & argc,
char * argv[],
const char * str,
const T & def = T() )
Parameters
argcArgument count from main.
argvArgument vector from main.
strOption prefix, such as "--number".
defValue used when the option is absent.

Member Function Documentation

◆ set() [1/2]

template<typename T>
bool set ( int & argc,
char * argv[],
char ch )
Parameters
argcArgument count from main.
argvArgument vector from main.
chShort option character.

◆ set() [2/2]

template<typename T>
bool set ( int & argc,
char * argv[],
const char * str )
Parameters
argcArgument count from main.
argvArgument vector from main.
strOption prefix, such as "--number".