G::GetOpt Class Reference

A command line switch parser. More...

#include <ggetopt.h>

List of all members.

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.
Arg args () const
 Returns all the non-switch command-line arguments.
Strings errorList () const
 Returns the list of errors.
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>".
std::string usageSummarySwitches (Level level=levelDefault()) const
 Returns the one-line summary of switches.
std::string usageHelp (Level level=levelDefault(), size_type tab_stop=tabDefault(), size_type wrap_width=wrapDefault(), bool level_exact=false) const
 Returns a multi-line string giving help on each switch.
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()) const
 Streams out multi-line usage text using usageSummary() and usageHelp().
void showUsage (std::ostream &stream, const std::string &args, bool verbose) const
 Streams out multi-line usage text using usageSummary() and usageHelp().
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).
void showErrors (std::ostream &stream) const
 An overload which uses Arg::prefix() as 'prefix_1'.
void show (std::ostream &stream, std::string prefix) const
 For debugging.
bool contains (char switch_letter) const
 Returns true if the command line contains the given switch.
bool contains (const std::string &switch_name) const
 Returns true if the command line contains the given switch.
std::string value (const std::string &switch_name) const
 Returns the value related to the given value-based switch.
std::string value (char switch_letter) const
 Returns the value related to the given value-based switch.

Static Public Member Functions

static size_type wrapDefault ()
 Returns a default word-wrapping width.
static size_type tabDefault ()
 Returns a default tab-stop.
static Level levelDefault ()
 Returns the default level.
static std::string introducerDefault ()
 Returns "usage: ".

Classes

struct  Level
 Used by G::GetOpt for extra type safety. More...
struct  SwitchSpec
 A private implementation structure used by G::GetOpt. More...


Detailed Description

A command line switch parser.

See also:
G::Arg

Definition at line 42 of file ggetopt.h.


Member Typedef Documentation

typedef std::string::size_type G::GetOpt::size_type

Definition at line 48 of file ggetopt.h.


Constructor & Destructor Documentation

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' 'value-type' (0 is none, and 1 is a string) 'value-description' 'level'

If the switch-description field is empty or if the level is zero then the switch is hidden. By convention main-stream switches should have a level of 1, and obscure ones level 2 and above.

Definition at line 46 of file ggetopt.cpp.


Member Function Documentation

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

Returns all the non-switch command-line arguments.

Definition at line 486 of file ggetopt.cpp.

Referenced by Main::CommandLineImp::argc().

G::Strings G::GetOpt::errorList (  )  const

Returns the list of errors.

Definition at line 454 of file ggetopt.cpp.

G::GetOpt::size_type G::GetOpt::wrapDefault (  )  [static]

Returns a default word-wrapping width.

Definition at line 113 of file ggetopt.cpp.

References result, and G::Str::toUInt().

G::GetOpt::size_type G::GetOpt::tabDefault (  )  [static]

Returns a default tab-stop.

Definition at line 124 of file ggetopt.cpp.

G::GetOpt::Level G::GetOpt::levelDefault (  )  [static]

Returns the default level.

Definition at line 134 of file ggetopt.cpp.

Referenced by showUsage(), and Main::CommandLineImp::showUsage().

std::string G::GetOpt::introducerDefault (  )  [static]

Returns "usage: ".

Definition at line 129 of file ggetopt.cpp.

Referenced by showUsage(), and Main::CommandLineImp::showUsage().

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

References s, usageSummarySwitches(), and G::Str::wrap().

Referenced by showUsage().

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

Referenced by usageSummary().

std::string G::GetOpt::usageHelp ( Level  level = levelDefault(),
size_type  tab_stop = tabDefault(),
size_type  wrap_width = wrapDefault(),
bool  level_exact = false 
) const

Returns a multi-line string giving help on each switch.

Definition at line 235 of file ggetopt.cpp.

Referenced by showUsage().

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() 
) 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 149 of file ggetopt.cpp.

References usageHelp(), and usageSummary().

Referenced by showUsage(), and Main::CommandLineImp::showUsage().

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.

References introducerDefault(), levelDefault(), G::Arg::prefix(), and showUsage().

bool G::GetOpt::hasErrors (  )  const

Returns true if there are errors.

Definition at line 513 of file ggetopt.cpp.

Referenced by Main::CommandLineImp::hasUsageErrors().

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

Referenced by showErrors(), and Main::CommandLineImp::showUsageErrors().

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

An overload which uses Arg::prefix() as 'prefix_1'.

Definition at line 518 of file ggetopt.cpp.

References G::Arg::prefix(), and showErrors().

void G::GetOpt::show ( std::ostream &  stream,
std::string  prefix 
) const

For debugging.

Definition at line 491 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 459 of file ggetopt.cpp.

Referenced by Main::CommandLineImp::contains(), Main::CommandLineImp::semanticError(), Main::CommandLineImp::showExtraHelp(), Main::CommandLineImp::showUsage(), and value().

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

Returns true if the command line contains the given switch.

Definition at line 465 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 480 of file ggetopt.cpp.

References contains(), and G_ASSERT.

Referenced by Main::CommandLineImp::value().

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

References contains(), and G_ASSERT.


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

Generated on Fri Apr 18 15:56:13 2008 for E-MailRelay by  doxygen 1.5.5