UI::Util::GetOpt Class Reference

Abstraction class for GNU getopt_long(3). More...

`#include <GetOpt.hpp>`

Collaboration diagram for UI::Util::GetOpt:

## Public Types

enum | Type { NoArg_ , Arg_ , OptArg_ }
| All three possible types of CLOption* Classes. More...

## Public Member Functions

| GetOpt (int argc, char *const *const argv)
| Standard constructor.
| ~GetOpt ()
| Standard destructor.
GetOpt & | set (std::string const &nameLong, char nameShort, Type type, std::string const &doc="No documentation for this option.", std::string const &defaultArg="")
| Introduce a new option.
CLOption * | get (char nameShort)
| Get an option object from short name.
CLOption * | get (std::string const &nameLong)
| Get an option object from long name.
void | printUsage (std::string const &title="\nUsage:\n\n") const
| Prints out Usage information to cout (stdout).
int | wrongUsage (std::string const &reason, std::string const &title="\nUsage:\n\n") const
| Helper.
bool | isValid ()
| Check if given command line arguments are "well-formed".

## Detailed Description

Abstraction class for GNU getopt_long(3).

Use this rather than the CLOption*-Classes directly.

Deprecated
  Leagcy - don't use this for new code – use boost::program_options instead.

Examples
  GetOpt.cpp.

## Member Enumeration Documentation

## Type

enum UI::Util::GetOpt::Type

All three possible types of CLOption* Classes.

In normal use, you would use this in set(), who then choses what type of object to create. Use "NOARG" for CLOption, "ARG" for CLOptionArg and "OPTARG" for CLOptionOptArg.

See also
  set()

## Constructor & Destructor Documentation

## GetOpt()

UI::Util::GetOpt::GetOpt | ( | int | argc,
|  | char *const *const | argv )

Standard constructor.

Parameters

  argc | Number of arguments (first argument of main() function).
  argv | Array of arguments (second argument of main() function).

Referenced by set().

## Member Function Documentation

## get() [1/2]

CLOption * UI::Util::GetOpt::get | ( | char | nameShort | ) |

Get an option object from short name.

The pointer is valid as long as the GetOpt object stays alive. This will implicitely parse the command line options, if not already done.

Parameters

  nameShort | Short name (one character) of the option; e.g. 'v'.

Examples
  GetOpt.cpp.

References UI::Util::CLOption::getNameShort().

## get() [2/2]

CLOption * UI::Util::GetOpt::get | ( | std::string const & | nameLong | ) |

Get an option object from long name.

Parameters

  nameLong | Long name of the option; e.g. "version".

References UI::Util::CLOption::getNameLong().

## isValid()

bool UI::Util::GetOpt::isValid | ( |  | ) |

Check if given command line arguments are "well-formed".

Note
  GNU getopt_long(3) will print error messages to stderr, if the given arguments are not valid.

Returns
  true if valid, else false.

Examples
  GetOpt.cpp.

## printUsage()

void UI::Util::GetOpt::printUsage | ( | std::string const & | title = "\nUsage:\n\n" | ) | const

Prints out Usage information to cout (stdout).

Parameters

  title | Print this text before usage information.

Examples
  GetOpt.cpp.

References UI::Util::CLOption::printUsage().

Referenced by wrongUsage().

## set()

GetOpt & UI::Util::GetOpt::set | ( | std::string const & | nameLong,
|  | char | nameShort,
|  | Type | type,
|  | std::string const & | doc = "No documentation for this option.",
|  | std::string const & | defaultArg = "" )

Introduce a new option.

Parameters

  nameLong | Long name of the option; e.g. "version".
  nameShort | Short name (one character) of the option; e.g. 'v'.
  type | Type of the option (NOARG, ARG, OPTARG); e.g. "NOARG".
  doc | Documentaion for this option; e.g. "Show version information".
  defaultArg | Default value for that option.

Examples
  GetOpt.cpp.

References GetOpt().

## wrongUsage()

int UI::Util::GetOpt::wrongUsage | ( | std::string const & | reason,
|  | std::string const & | title = "\nUsage:\n\n" ) const

Helper.

Prints out an error text to cerr, prints usage to cout and always returns 1 (suitable as program exit code).

Parameters

  reason | Explanatory error text.
  title | Print this text before usage information.

Examples
  GetOpt.cpp.

References printUsage().

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

- GetOpt.hpp
- GetOpt.cpp