VideoTools
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
G::GetOpt Class Reference

A command line option parser. More...

#include <ggetopt.h>

Public Types

typedef std::string::size_type size_type
 

Public Member Functions

 GetOpt (const Arg &arg, const std::string &spec)
 Constructor taking a Arg reference and a G::Options specification string. More...
 
 GetOpt (const StringArray &arg, const std::string &spec)
 An overload taking a vector of command-line arguments. More...
 
void reload (const StringArray &arg)
 Reinitialises the object with the given command-line arguments. More...
 
void addOptionsFromFile (size_type n=1U)
 Adds options from the config file named by the n'th non-option command-line argument (zero-based but allowing for the program name in argv0). More...
 
void addOptionsFromFile (const Path &file)
 Adds options from the given config file. More...
 
const Optionsoptions () const
 Returns a reference to the internal option specification object.
 
Arg args () const
 Returns all the non-option command-line arguments.
 
StringArray errorList () const
 Returns the list of errors.
 
bool hasErrors () const
 Returns true if there are errors.
 
void showErrors (std::ostream &stream, std::string prefix_1, std::string prefix_2=std::string(": ")) const
 A convenience function which streams out each errorList() item to the given stream, prefixed with the given prefix(es). More...
 
void showErrors (std::ostream &stream) const
 An overload which has a sensible prefix.
 
bool contains (char option_letter) const
 Returns true if the command line contains the option identified by its short-form letter. More...
 
bool contains (const std::string &option_name) const
 Returns true if the command line contains the option identified by its long-form name. More...
 
unsigned int count (const std::string &option_name) const
 Returns the number of times the option was supplied.
 
std::string value (const std::string &option_name, const std::string &default_=std::string()) const
 Returns the value related to the option identified by its long-form name. More...
 
std::string value (char option_letter, const std::string &default_=std::string()) const
 Returns the value related to the option identified by its short-form letter. More...
 

Detailed Description

A command line option parser.

Usage:

G::Arg arg( argc , argv ) ;
G::GetOpt opt( arg , "e!extra!does something! extra!1!something!1" "|" "h!help!shows help!!0!!1" ) ;
if( opt.hasErrors() ) { opt.showErrors( std::cerr ) ; exit( 2 ) ; }
if( opt.contains("help") ) { opt.options().showUsage( std::cout , arg.prefix() , " [<more>]" ) ; exit( 0 ) ; }
run( opt.args() , opt.contains("extra") ? opt.value("extra") : std::string() ) ;
See Also
G::Arg

Definition at line 55 of file ggetopt.h.

Constructor & Destructor Documentation

G::GetOpt::GetOpt ( const Arg arg,
const std::string &  spec 
)

Constructor taking a Arg reference and a G::Options specification string.

Parsing errors are reported via errorList().

Definition at line 33 of file ggetopt.cpp.

G::GetOpt::GetOpt ( const StringArray arg,
const std::string &  spec 
)

An overload taking a vector of command-line arguments.

The program name in the first argument is expected but ignored.

Definition at line 41 of file ggetopt.cpp.

Member Function Documentation

void G::GetOpt::addOptionsFromFile ( size_type  n = 1U)

Adds options from the config file named by the n'th non-option command-line argument (zero-based but allowing for the program name in argv0).

The n'th argument is then removed. Does nothing if the n'th argument does not exists or if it is empty. Throws if the file is specified but cannot be opened. Parsing errors are added to errorList().

void G::GetOpt::addOptionsFromFile ( const Path file)

Adds options from the given config file.

Throws if the file cannot be opened. Parsing errors are added to errorList().

Definition at line 98 of file ggetopt.cpp.

bool G::GetOpt::contains ( char  option_letter) const

Returns true if the command line contains the option identified by its short-form letter.

Definition at line 113 of file ggetopt.cpp.

bool G::GetOpt::contains ( const std::string &  option_name) const

Returns true if the command line contains the option identified by its long-form name.

Definition at line 118 of file ggetopt.cpp.

void G::GetOpt::reload ( const StringArray arg)

Reinitialises the object with the given command-line arguments.

The program name in the first position is expected but ignored.

Definition at line 49 of file ggetopt.cpp.

void G::GetOpt::showErrors ( std::ostream &  stream,
std::string  prefix_1,
std::string  prefix_2 = std::string(": ") 
) const

A convenience function which streams out each errorList() item to the given stream, prefixed with the given prefix(es).

The two prefixes are simply concatenated.

Definition at line 154 of file ggetopt.cpp.

std::string G::GetOpt::value ( const std::string &  option_name,
const std::string &  default_ = std::string() 
) const

Returns the value related to the option identified by its long-form name.

Returns a string that is a comma-separated list if multi-valued.

Definition at line 134 of file ggetopt.cpp.

std::string G::GetOpt::value ( char  option_letter,
const std::string &  default_ = std::string() 
) const

Returns the value related to the option identified by its short-form letter.

Returns a string that is a comma-separated list if multi-valued. Precondition: contains(option_letter)

Definition at line 128 of file ggetopt.cpp.


The documentation for this class was generated from the following files: