A command line switch parser. More...
#include <ggetopt.h>
Classes | |
struct | Level |
Used by G::GetOpt for extra type safety. More... | |
struct | SwitchSpec |
A private implementation structure used by G::GetOpt. More... | |
Public Types | |
typedef std::string::size_type | size_type |
Public Member Functions | |
GetOpt (const Arg &arg, const std::string &spec, char sep_major= '|', char sep_minor= '/', char escape= '\\') | |
Constructor taking a Arg reference and a specification string. More... | |
Arg | args () const |
Returns all the non-switch command-line arguments. More... | |
Strings | errorList () const |
Returns the list of errors. More... | |
std::string | usageSummary (const std::string &exe, const std::string &args, const std::string &introducer=introducerDefault(), Level level=levelDefault(), size_type wrap_width=wrapDefault()) const |
Returns a one-line usage summary, as "usage: <exe> <usageSummarySwitches()> <args>". More... | |
std::string | usageSummarySwitches (Level level=levelDefault()) const |
Returns the one-line summary of switches. More... | |
std::string | usageHelp (Level level=levelDefault(), size_type tab_stop=tabDefault(), size_type wrap_width=wrapDefault(), bool level_exact=false, bool extra=true) const |
Returns a multi-line string giving help on each switch. More... | |
void | showUsage (std::ostream &stream, const std::string &exe, const std::string &args, const std::string &introducer=introducerDefault(), Level level=levelDefault(), size_type tab_stop=tabDefault(), size_type wrap_width=wrapDefault(), bool extra=true) const |
Streams out multi-line usage text using usageSummary() and usageHelp(). More... | |
void | showUsage (std::ostream &stream, const std::string &args, bool verbose) const |
Streams out multi-line usage text using usageSummary() and usageHelp(). More... | |
bool | hasErrors () const |
Returns true if there are errors. More... | |
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 uses Arg::prefix() as 'prefix_1'. More... | |
void | show (std::ostream &stream, std::string prefix) const |
For debugging. More... | |
bool | contains (char switch_letter) const |
Returns true if the command line contains the given switch. More... | |
bool | contains (const std::string &switch_name) const |
Returns true if the command line contains the given switch. More... | |
std::string | value (const std::string &switch_name) const |
Returns the value related to the given value-based switch. More... | |
std::string | value (char switch_letter) const |
Returns the value related to the given value-based switch. More... | |
Static Public Member Functions | |
static size_type | wrapDefault () |
Returns a default word-wrapping width. More... | |
static size_type | tabDefault () |
Returns a default tab-stop. More... | |
static Level | levelDefault () |
Returns the default level. More... | |
static std::string | introducerDefault () |
Returns "usage: ". More... | |
A command line switch parser.
Usage:
typedef std::string::size_type G::GetOpt::size_type |
G::GetOpt::GetOpt | ( | const Arg & | arg, |
const std::string & | spec, | ||
char | sep_major = '|' , |
||
char | sep_minor = '/' , |
||
char | escape = '\\' |
||
) |
Constructor taking a Arg reference and a specification string.
Uses specifications like "p/port/defines the port number//1/port/1|v/verbose/shows more logging//0//1". made up of the following parts: 'single-character-switch-letter' 'multi-character-switch-name' 'switch-description' 'switch-description-extra' 'value-type' (0 is none, and 1 is a string) 'value-description' 'level'
By convention mainstream switches should have a level of 1, and obscure ones level 2 and above. If the switch-description field is empty or if the level is zero then the switch is hidden.
Definition at line 46 of file ggetopt.cpp.
G::Arg G::GetOpt::args | ( | ) | const |
Returns all the non-switch command-line arguments.
Definition at line 490 of file ggetopt.cpp.
bool G::GetOpt::contains | ( | char | switch_letter | ) | const |
Returns true if the command line contains the given switch.
Definition at line 463 of file ggetopt.cpp.
bool G::GetOpt::contains | ( | const std::string & | switch_name | ) | const |
Returns true if the command line contains the given switch.
Definition at line 469 of file ggetopt.cpp.
G::Strings G::GetOpt::errorList | ( | ) | const |
Returns the list of errors.
Definition at line 458 of file ggetopt.cpp.
bool G::GetOpt::hasErrors | ( | ) | const |
Returns true if there are errors.
Definition at line 517 of file ggetopt.cpp.
|
static |
Returns "usage: ".
Definition at line 129 of file ggetopt.cpp.
Referenced by Main::CommandLineImp::showUsage().
|
static |
Returns the default level.
Definition at line 134 of file ggetopt.cpp.
Referenced by Main::CommandLineImp::showUsage().
void G::GetOpt::show | ( | std::ostream & | stream, |
std::string | prefix | ||
) | const |
For debugging.
Definition at line 495 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 527 of file ggetopt.cpp.
void G::GetOpt::showErrors | ( | std::ostream & | stream | ) | const |
An overload which uses Arg::prefix() as 'prefix_1'.
Definition at line 522 of file ggetopt.cpp.
void G::GetOpt::showUsage | ( | std::ostream & | stream, |
const std::string & | exe, | ||
const std::string & | args, | ||
const std::string & | introducer = introducerDefault() , |
||
Level | level = levelDefault() , |
||
size_type | tab_stop = tabDefault() , |
||
size_type | wrap_width = wrapDefault() , |
||
bool | extra = true |
||
) | const |
Streams out multi-line usage text using usageSummary() and usageHelp().
The 'args' parameter should represent the non-switch arguments (with a leading space), like " <foo> [<bar>]".
Definition at line 150 of file ggetopt.cpp.
void G::GetOpt::showUsage | ( | std::ostream & | stream, |
const std::string & | args, | ||
bool | verbose | ||
) | const |
Streams out multi-line usage text using usageSummary() and usageHelp().
Shows only level one switches if 'verbose' is false.
Definition at line 144 of file ggetopt.cpp.
|
static |
Returns a default tab-stop.
Definition at line 124 of file ggetopt.cpp.
std::string G::GetOpt::usageHelp | ( | Level | level = levelDefault() , |
size_type | tab_stop = tabDefault() , |
||
size_type | wrap_width = wrapDefault() , |
||
bool | level_exact = false , |
||
bool | extra = true |
||
) | const |
Returns a multi-line string giving help on each switch.
Definition at line 236 of file ggetopt.cpp.
std::string G::GetOpt::usageSummary | ( | const std::string & | exe, |
const std::string & | args, | ||
const std::string & | introducer = introducerDefault() , |
||
Level | level = levelDefault() , |
||
size_type | wrap_width = wrapDefault() |
||
) | const |
Returns a one-line usage summary, as "usage: <exe> <usageSummarySwitches()> <args>".
Definition at line 158 of file ggetopt.cpp.
References G::Str::wrap().
std::string G::GetOpt::usageSummarySwitches | ( | Level | level = levelDefault() | ) | const |
Returns the one-line summary of switches.
Does not include the usual "usage: <exe>" prefix or non-switch arguments.
Definition at line 172 of file ggetopt.cpp.
std::string G::GetOpt::value | ( | const std::string & | switch_name | ) | const |
Returns the value related to the given value-based switch.
Precondition: contains(switch_name)
Definition at line 484 of file ggetopt.cpp.
References G_ASSERT.
std::string G::GetOpt::value | ( | char | switch_letter | ) | const |
Returns the value related to the given value-based switch.
Precondition: contains(switch_letter)
Definition at line 476 of file ggetopt.cpp.
References G_ASSERT.
|
static |
Returns a default word-wrapping width.
Definition at line 113 of file ggetopt.cpp.
References G::Environment::get(), and G::Str::toUInt().
Referenced by Main::CommandLineImp::showUsage().