ui-utilcpp  1.8.3
Public Types | Public Member Functions
UI::Util::GetOpt Class Reference

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

#include <GetOpt.hpp>

Collaboration diagram for UI::Util::GetOpt:
Collaboration graph

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. More...
 
 ~GetOpt ()
 Standard destructor.
 
GetOptset (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. More...
 
CLOptionget (char nameShort)
 Get an option object from short name. More...
 
CLOptionget (std::string const &nameLong)
 Get an option object from long name. More...
 
void printUsage (std::string const &title="\nUsage:\n\n") const
 Prints out Usage information to cout (stdout). More...
 
int wrongUsage (std::string const &reason, std::string const &title="\nUsage:\n\n") const
 Helper. More...
 
bool isValid ()
 Check if given command line arguments are "well-formed". More...
 

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

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

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

Standard constructor.

Parameters
argcNumber of arguments (first argument of main() function).
argvArray of arguments (second argument of main() function).

Member Function Documentation

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
nameShortShort name (one character) of the option; e.g. 'v'.
Examples:
GetOpt.cpp.

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

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

Get an option object from long name.

Parameters
nameLongLong name of the option; e.g. "version".

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

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.
void UI::Util::GetOpt::printUsage ( std::string const &  title = "\nUsage:\n\n") const

Prints out Usage information to cout (stdout).

Parameters
titlePrint this text before usage information.
Examples:
GetOpt.cpp.

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

Referenced by wrongUsage().

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
nameLongLong name of the option; e.g. "version".
nameShortShort name (one character) of the option; e.g. 'v'.
typeType of the option (NOARG, ARG, OPTARG); e.g. "NOARG".
docDocumentaion for this option; e.g. "Show version information".
defaultArgDefault value for that option.
Examples:
GetOpt.cpp.
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
reasonExplanatory error text.
titlePrint this text before usage information.
Examples:
GetOpt.cpp.

References printUsage().


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