E-MailRelay
Public Types | Public Member Functions | List of all members
G::GetOpt Class Reference

A command-line option parser. More...

#include <ggetopt.h>

Public Types

using size_type = std::string::size_type
 

Public Member Functions

 GetOpt (const Arg &arg, const std::string &spec, std::size_t ignore_non_options=0U)
 Constructor taking a Arg object and a G::Options specification string. More...
 
 GetOpt (const StringArray &arg, const std::string &spec, std::size_t ignore_non_options=0U)
 An overload taking a vector of command-line arguments. More...
 
 GetOpt (const Arg &arg, const Options &spec, std::size_t ignore_non_options=0U)
 A constructor overload taking an Options object. More...
 
 GetOpt (const StringArray &arg, const Options &spec, std::size_t ignore_non_options=0U)
 A constructor overload taking an Options object. More...
 
void reload (const StringArray &arg, std::size_t ignore_non_options=0U)
 Reinitialises the object with the given command-line arguments. More...
 
void addOptionsFromFile (size_type n=1U, const std::string &varkey=std::string(), const std::string &varvalue=std::string())
 Adds options from the config file named by the n'th non-option command-line argument (zero-based and 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 option specification sub-object. More...
 
const OptionMapmap () const
 Returns a reference to the OptionMap sub-object. More...
 
Arg args () const
 Returns the G::Arg command-line, excluding options. More...
 
StringArray errorList () const
 Returns the list of errors. More...
 
bool hasErrors () const
 Returns true if there are errors. More...
 
void showErrors (std::ostream &stream, const std::string &prefix_1, const 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. More...
 
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...
 
std::size_t count (const std::string &option_name) const
 Returns the option's repeat count. More...
 
std::string value (const std::string &option_name, const std::string &default_=std::string()) const
 Returns the value for the option identified by its long-form name. More...
 
std::string value (char option_letter, const std::string &default_=std::string()) const
 An overload that returns the value of the option identified by its short-form letter. More...
 
 GetOpt (const GetOpt &)=delete
 
 GetOpt (GetOpt &&)=delete
 
void operator= (const GetOpt &)=delete
 
void operator= (GetOpt &&)=delete
 

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() ) ;
A class which holds a represention of the argc/argv command line array, and supports simple command-l...
Definition: garg.h:44
A command-line option parser.
Definition: ggetopt.h:56
See also
G::Arg, G::Options, G::OptionMap, G::OptionParser

Definition at line 55 of file ggetopt.h.

Member Typedef Documentation

◆ size_type

using G::GetOpt::size_type = std::string::size_type

Definition at line 58 of file ggetopt.h.

Constructor & Destructor Documentation

◆ GetOpt() [1/4]

G::GetOpt::GetOpt ( const Arg arg,
const std::string &  spec,
std::size_t  ignore_non_options = 0U 
)

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

Parsing errors are reported via errorList().

Definition at line 33 of file ggetopt.cpp.

◆ GetOpt() [2/4]

G::GetOpt::GetOpt ( const StringArray arg,
const std::string &  spec,
std::size_t  ignore_non_options = 0U 
)

An overload taking a vector of command-line arguments.

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

Definition at line 47 of file ggetopt.cpp.

◆ GetOpt() [3/4]

G::GetOpt::GetOpt ( const Arg arg,
const Options spec,
std::size_t  ignore_non_options = 0U 
)

A constructor overload taking an Options object.

Definition at line 40 of file ggetopt.cpp.

◆ GetOpt() [4/4]

G::GetOpt::GetOpt ( const StringArray arg,
const Options spec,
std::size_t  ignore_non_options = 0U 
)

A constructor overload taking an Options object.

Definition at line 54 of file ggetopt.cpp.

Member Function Documentation

◆ addOptionsFromFile() [1/2]

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 108 of file ggetopt.cpp.

◆ addOptionsFromFile() [2/2]

void G::GetOpt::addOptionsFromFile ( size_type  n = 1U,
const std::string &  varkey = std::string(),
const std::string &  varvalue = std::string() 
)

Adds options from the config file named by the n'th non-option command-line argument (zero-based and 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(). The optional trailing string parameters are used to perform leading sub-string substitution on the filename.

◆ args()

G::Arg G::GetOpt::args ( ) const

Returns the G::Arg command-line, excluding options.

Definition at line 154 of file ggetopt.cpp.

◆ contains() [1/2]

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 128 of file ggetopt.cpp.

◆ contains() [2/2]

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 133 of file ggetopt.cpp.

◆ count()

std::size_t G::GetOpt::count ( const std::string &  option_name) const

Returns the option's repeat count.

Definition at line 138 of file ggetopt.cpp.

◆ errorList()

G::StringArray G::GetOpt::errorList ( ) const

Returns the list of errors.

Definition at line 123 of file ggetopt.cpp.

◆ hasErrors()

bool G::GetOpt::hasErrors ( ) const

Returns true if there are errors.

Definition at line 159 of file ggetopt.cpp.

◆ map()

const G::OptionMap & G::GetOpt::map ( ) const

Returns a reference to the OptionMap sub-object.

Definition at line 118 of file ggetopt.cpp.

◆ options()

const G::Options & G::GetOpt::options ( ) const

Returns a reference to the option specification sub-object.

Definition at line 113 of file ggetopt.cpp.

◆ reload()

void G::GetOpt::reload ( const StringArray arg,
std::size_t  ignore_non_options = 0U 
)

Reinitialises the object with the given command-line arguments.

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

Definition at line 61 of file ggetopt.cpp.

◆ showErrors() [1/2]

void G::GetOpt::showErrors ( std::ostream &  stream) const

An overload which has a sensible prefix.

Definition at line 164 of file ggetopt.cpp.

◆ showErrors() [2/2]

void G::GetOpt::showErrors ( std::ostream &  stream,
const std::string &  prefix_1,
const 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 169 of file ggetopt.cpp.

◆ value() [1/2]

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

An overload that returns the value of the option identified by its short-form letter.

Precondition: contains(option_letter)

Definition at line 143 of file ggetopt.cpp.

◆ value() [2/2]

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

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

If the option is multi-valued then the returned value is a comma-separated list. If the option-value is 'on' then Str::positive() is returned; if the option-value is 'off' then the given default is returned.

Definition at line 149 of file ggetopt.cpp.


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