C++ Utilities  4.7.0
Common C++ classes and routines used by my applications such as argument parser, IO and conversion utilities
Public Member Functions | List of all members
ApplicationUtilities::ArgumentParser Class Reference

The ArgumentParser class provides a means for handling command line arguments. More...

#include <argumentparser.h>

Public Member Functions

 ArgumentParser ()
 Constructs a new ArgumentParser. More...
 
const ArgumentVectormainArguments () const
 Returns the main arguments. More...
 
void setMainArguments (const ArgumentInitializerList &mainArguments)
 Sets the main arguments for the parser. More...
 
void addMainArgument (Argument *argument)
 Adds the specified argument to the main argument. More...
 
void printHelp (std::ostream &os) const
 Prints help text for all assigned arguments. More...
 
void parseArgs (int argc, const char *const *argv)
 Parses the specified command line arguments. More...
 
void readArgs (int argc, const char *const *argv)
 Parses the specified command line arguments. More...
 
void resetArgs ()
 Resets all Argument instances assigned as mainArguments() and sub arguments. More...
 
unsigned int actualArgumentCount () const
 Returns the actual number of arguments that could be found when parsing. More...
 
const char * executable () const
 Returns the name of the current executable. More...
 
UnknownArgumentBehavior unknownArgumentBehavior () const
 Returns how unknown arguments are treated. More...
 
void setUnknownArgumentBehavior (UnknownArgumentBehavior behavior)
 Sets how unknown arguments are treated. More...
 
ArgumentdefaultArgument () const
 Returns the default argument. More...
 
void setDefaultArgument (Argument *argument)
 Sets the default argument. More...
 
void checkConstraints ()
 Checks whether contraints are violated. More...
 
void invokeCallbacks ()
 Invokes all assigned callbacks. More...
 
bool isUncombinableMainArgPresent () const
 Checks whether at least one uncombinable main argument is present. More...
 

Detailed Description

The ArgumentParser class provides a means for handling command line arguments.

To setup the parser create instances of ApplicationUtilities::Argument to define a set of known arguments and assign these to the parser using setMainArguments().

To invoke parsing call parseArgs(). The parser will verify the previously assigned definitions (and might throw std::invalid_argument) and then parse the given command line arguments according the definitions (and might throw ApplicationUtilities::Failure).

Definition at line 232 of file argumentparser.h.

Constructor & Destructor Documentation

◆ ArgumentParser()

ApplicationUtilities::ArgumentParser::ArgumentParser ( )

Constructs a new ArgumentParser.

Definition at line 545 of file argumentparser.cpp.

Member Function Documentation

◆ actualArgumentCount()

unsigned int ApplicationUtilities::ArgumentParser::actualArgumentCount ( ) const
inline

Returns the actual number of arguments that could be found when parsing.

Definition at line 751 of file argumentparser.h.

◆ addMainArgument()

void ApplicationUtilities::ArgumentParser::addMainArgument ( Argument argument)

Adds the specified argument to the main argument.

Remarks
The parser does not take ownership. Do not destroy the argument as long as it is used as main argument.

Definition at line 594 of file argumentparser.cpp.

◆ checkConstraints()

void ApplicationUtilities::ArgumentParser::checkConstraints ( )
inline

Checks whether contraints are violated.

Remarks
Automatically called by parseArgs().
Exceptions
ThrowsFailure if constraints are violated.

Definition at line 807 of file argumentparser.h.

◆ defaultArgument()

Argument * ApplicationUtilities::ArgumentParser::defaultArgument ( ) const
inline

Returns the default argument.

Remarks
The default argument is assumed to be present if no other arguments have been specified.

Definition at line 788 of file argumentparser.h.

◆ executable()

const char * ApplicationUtilities::ArgumentParser::executable ( ) const
inline

Returns the name of the current executable.

Definition at line 759 of file argumentparser.h.

◆ invokeCallbacks()

void ApplicationUtilities::ArgumentParser::invokeCallbacks ( )
inline

Invokes all assigned callbacks.

Remarks
Automatically called by parseArgs().

Definition at line 816 of file argumentparser.h.

◆ isUncombinableMainArgPresent()

bool ApplicationUtilities::ArgumentParser::isUncombinableMainArgPresent ( ) const

Checks whether at least one uncombinable main argument is present.

Definition at line 736 of file argumentparser.cpp.

◆ mainArguments()

const ArgumentVector & ApplicationUtilities::ArgumentParser::mainArguments ( ) const
inline

Returns the main arguments.

See also
setMainArguments()

Definition at line 743 of file argumentparser.h.

◆ parseArgs()

void ApplicationUtilities::ArgumentParser::parseArgs ( int  argc,
const char *const *  argv 
)

Parses the specified command line arguments.

Remarks
  • The results are stored in the Argument instances assigned as main arguments and sub arguments.
  • Calls the assigned callbacks if no constraints are violated.
Exceptions
ThrowsFailure if the specified arguments are invalid or violate the constraints defined by the Argument instances.
See also
readArgs()

Definition at line 651 of file argumentparser.cpp.

◆ printHelp()

void ApplicationUtilities::ArgumentParser::printHelp ( std::ostream &  os) const

Prints help text for all assigned arguments.

Definition at line 603 of file argumentparser.cpp.

◆ readArgs()

void ApplicationUtilities::ArgumentParser::readArgs ( int  argc,
const char *const *  argv 
)

Parses the specified command line arguments.

Remarks
  • The results are stored in the Argument instances assigned as main arguments and sub arguments.
  • In contrast to parseArgs() this method does not check whether constraints are violated and it does not call any callbacks.
Exceptions
ThrowsFailure if the specified arguments are invalid.
See also
readArgs()

Definition at line 669 of file argumentparser.cpp.

◆ resetArgs()

void ApplicationUtilities::ArgumentParser::resetArgs ( )

Resets all Argument instances assigned as mainArguments() and sub arguments.

See also
Argument::reset()

Definition at line 725 of file argumentparser.cpp.

◆ setDefaultArgument()

void ApplicationUtilities::ArgumentParser::setDefaultArgument ( Argument argument)
inline

Sets the default argument.

Remarks
The default argument is assumed to be present if no other arguments have been specified.

Definition at line 797 of file argumentparser.h.

◆ setMainArguments()

void ApplicationUtilities::ArgumentParser::setMainArguments ( const ArgumentInitializerList mainArguments)

Sets the main arguments for the parser.

The parser will use these argument definitions to when parsing the command line arguments and when printing help information.

Remarks
  • The parser does not take ownership. Do not destroy the arguments as long as they are used as main arguments.
  • Sets the first specified argument as default argument if none has been assigned yet and the first argument does not require any values or has no mandatory sub arguments.

Definition at line 562 of file argumentparser.cpp.

◆ setUnknownArgumentBehavior()

void ApplicationUtilities::ArgumentParser::setUnknownArgumentBehavior ( UnknownArgumentBehavior  behavior)
inline

Sets how unknown arguments are treated.

The default value is UnknownArgumentBehavior::Fail.

Definition at line 779 of file argumentparser.h.

◆ unknownArgumentBehavior()

UnknownArgumentBehavior ApplicationUtilities::ArgumentParser::unknownArgumentBehavior ( ) const
inline

Returns how unknown arguments are treated.

The default value is UnknownArgumentBehavior::Fail.

Definition at line 769 of file argumentparser.h.


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