E-MailRelay
|
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 Options & | options () const |
Returns a reference to the option specification sub-object. More... | |
const OptionMap & | map () 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 |
A command-line option parser.
Usage:
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.
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.
A constructor overload taking an Options object.
Definition at line 40 of file ggetopt.cpp.
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.
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.
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.
G::Arg G::GetOpt::args | ( | ) | const |
Returns the G::Arg command-line, excluding options.
Definition at line 154 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 128 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 133 of file ggetopt.cpp.
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.
G::StringArray G::GetOpt::errorList | ( | ) | const |
Returns the list of errors.
Definition at line 123 of file ggetopt.cpp.
bool G::GetOpt::hasErrors | ( | ) | const |
Returns true if there are errors.
Definition at line 159 of file ggetopt.cpp.
const G::OptionMap & G::GetOpt::map | ( | ) | const |
Returns a reference to the OptionMap sub-object.
Definition at line 118 of file ggetopt.cpp.
const G::Options & G::GetOpt::options | ( | ) | const |
Returns a reference to the option specification sub-object.
Definition at line 113 of file ggetopt.cpp.
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.
void G::GetOpt::showErrors | ( | std::ostream & | stream | ) | const |
An overload which has a sensible prefix.
Definition at line 164 of file ggetopt.cpp.
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.
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.
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.