libcommonc++  0.7
Application Class Reference

Base functionality for an application, including command-line argument parsing and a shutdown handler. More...

#include <Application.h++>

Inheritance diagram for Application:

Public Member Functions

 Application (int argc, char **argv, const String &version="0.0", const String &compileDate="", const String &compileTime="")
 Construct a new Application. More...
 
virtual ~Application ()
 Destructor. More...
 

Protected Member Functions

virtual void shutdown ()
 Shutdown method. More...
 
bool registerOption (char opt, const String &longOpt, const String &argName, const String &desc)
 Register a command-line option. More...
 
bool parseOptions (int argc, char **argv, int &index)
 Parse the command line options. More...
 
virtual bool processOption (char opt, const String &longOpt, const String &arg=String::null)
 Process a command-line option. More...
 
virtual void printUsage ()
 Print the usage text. More...
 
void printError (const char *message,...)
 Print an error message. More...
 

Detailed Description

Base functionality for an application, including command-line argument parsing and a shutdown handler.

This class is implicitly a singleton; there can be only one instantiation of the class per process. The class should be subclassed and instantiated within the program's main() function.

Author
Mark Lindner

Constructor & Destructor Documentation

◆ Application()

Application ( int  argc,
char **  argv,
const String version = "0.0",
const String compileDate = "",
const String compileTime = "" 
)

Construct a new Application.

Parameters
argcThe argument count (from main()).
argvThe argument list (from main()).
versionThe optional version of the program; if not supplied, it defaults to "0.0".
compileDateThe optional compile date of the program; typically provided by the compiler-defined macro __DATE__.
compileTimeThe optional compile time of the program; typically provided by the compiler-defined macro __TIME__.

◆ ~Application()

~Application ( )
virtual

Destructor.

Member Function Documentation

◆ parseOptions()

bool parseOptions ( int  argc,
char **  argv,
int &  index 
)
protected

Parse the command line options.

This method should be called from the constructor at the appropriate time, after any application-specific options have been registered and any other necessary initialization has been completed.

Parameters
argcThe argument count.
argvThe argument list.
indexUpon return, the index of the first non-option argument.
Returns
true if the options were parsed successfully, or false if there was a parsing error. (Parsing errors are reported to stderr as a side-effect.)

◆ printError()

void printError ( const char *  message,
  ... 
)
protected

Print an error message.

The program name is automatically prepended to the message.

◆ printUsage()

void printUsage ( )
protectedvirtual

Print the usage text.

◆ processOption()

bool processOption ( char  opt,
const String longOpt,
const String arg = String::null 
)
protectedvirtual

Process a command-line option.

This method is called for each registered option that is encountered on the command line during the call to parseOptions().

The Application class registers the "--help" and "--version" command line options automatically. Therefore a subclass should override processOption() such that if it does not recognize the option, it will call Application::processOption() to give it a chance to process it.

Parameters
optThe one-character switch, or NUL if not applicable.
longOptThe long name for the switch, or String::null if not applicable.
argThe argument for the switch, or NULL if not applicable.
Returns
true if the option was processed successfully, false otherwise.

Reimplemented in Service.

◆ registerOption()

bool registerOption ( char  opt,
const String longOpt,
const String argName,
const String desc 
)
protected

Register a command-line option.

The option may have a short (one character) form and/or a long form. A typical invocation might look like this:

registerOption('o', "output", "file", "Specify @@ as the output file.");

The corresponding usage help text for this switch would appear as:

-o <file>
--output=<file>    - Specify <file> as the output file.
Parameters
optThe one-character switch (e.g., 'v' for "-v"), or NUL if not applicable.
longOptThe long name for the switch (e.g., "verbose" for "--verbose"), or String::null if not applicable.
argNameA token describing the argument for the switch, or the empty string "" if the switch does not accept an argument.
descThe description of the option, used in generating the usage text. Any occurrence of the token "@@" within the description will be replaced with a stylized form of the argument name, either by underlining the token or by enclosing it within angle brackets.
Returns
true if the option was registered successfully, false otherwise. Attempts to register an existing option will fail.

◆ shutdown()

void shutdown ( )
protectedvirtual

Shutdown method.

This method is called automatically when the application is terminated via a keyboard interrupt (typically Control-C). Subclasses may provide application-specific cleanup logic in this method. Since this method gets called from a signal handler, it should only perform minimal processing. It is not necessary for this method to call exit(), as that will be done automatically by the Application class.

The default implementation does nothing.

Reimplemented in Service.


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