Quark  0.1
CommandLineParser Class Referenceabstract

An abstract base class for parsing command-line arguments. More...

#include <CommandLineParser.h++>

Public Member Functions

 CommandLineParser (QStringList args)
 Constructs a new CommandLineParser with the given argument list. More...
 
virtual ~CommandLineParser ()
 Destructor. More...
 
bool registerOption (QChar opt, const QString &longOpt, const QString &argName=QString(), const QString &description=QString())
 Registers a command-line option to be recognized by this parser. More...
 
bool parseArguments ()
 Performs the parsing. More...
 
void printUsage ()
 Prints usage information based on the options that were registered with registerOption(). More...
 

Protected Member Functions

virtual void processOption (QChar opt, const QString &longOpt, const QString &arg)=0
 Called to process a valid command line option. More...
 
virtual bool processNonOptionArguments (QStringList args)=0
 Called to process non-option arguments. More...
 
virtual bool handleMissingArgument (QChar opt, const QString &longOpt)
 Called to handle a missing argument error. More...
 
virtual bool handleInvalidOption (QChar opt, const QString &longOpt)
 Called to handle an invalid option. More...
 

Detailed Description

An abstract base class for parsing command-line arguments.

Subclasses must implement processOption() and processNonOptionArguments() and may optionally override handleMissingArgument() and handleInvalidOption().

The registerOption() method should be called once for each option that the parser recognizes. Then, the parseArguments() method may be called to perform the parsing.

Author
Mark Lindner

Constructor & Destructor Documentation

◆ CommandLineParser()

CommandLineParser ( QStringList  args)

Constructs a new CommandLineParser with the given argument list.

Parameters
Theargument list to parse.

◆ ~CommandLineParser()

virtual ~CommandLineParser ( )
virtual

Destructor.

Member Function Documentation

◆ handleInvalidOption()

virtual bool handleInvalidOption ( QChar  opt,
const QString &  longOpt 
)
protectedvirtual

Called to handle an invalid option.

Parameters
optThe single-character option, if any, otherwise a NUL character.
longOptThe "long" option, if any, otherwise a null string.
Returns
true if parsing should continue, false otherwise.

◆ handleMissingArgument()

virtual bool handleMissingArgument ( QChar  opt,
const QString &  longOpt 
)
protectedvirtual

Called to handle a missing argument error.

Parameters
optThe single-character option, if any, otherwise a NUL character.
longOptThe "long" option, if any, otherwise a null string.
Returns
true if parsing should continue, false otherwise.

◆ parseArguments()

bool parseArguments ( )

Performs the parsing.

The processOption() method will be called for each recognized option on the command line. Once all options have been processed, processNonOptionArguments() will be called with any remaining arguments. If an option's argument is missing, handleMissingArgument() will be called. If an unrecognized option is encountered, handleInvalidOption() will be called.

◆ printUsage()

void printUsage ( )

Prints usage information based on the options that were registered with registerOption().

This includes a nicely-formatted summary of the available options and their descriptions.

◆ processNonOptionArguments()

virtual bool processNonOptionArguments ( QStringList  args)
protectedpure virtual

Called to process non-option arguments.

Parameters
argsThe arguments.
Returns
true if the arguments were successfully processed, false otherwise.

◆ processOption()

virtual void processOption ( QChar  opt,
const QString &  longOpt,
const QString &  arg 
)
protectedpure virtual

Called to process a valid command line option.

Parameters
optThe single-character option, if any, otherwise a NUL character.
longOptThe "long" option, if any, otherwise a null string.
Theoption's argument, if any, otherwise a null string.

◆ registerOption()

bool registerOption ( QChar  opt,
const QString &  longOpt,
const QString &  argName = QString(),
const QString &  description = QString() 
)

Registers a command-line option to be recognized by this parser.

Parameters
optThe single-character option, if any, otherwise the NUL character.
longOptThe "long" option, if any, otherwise a null string.
argNameThe name of the option's argument, if it accepts one, otherwise a null string.
descriptionA description of the option, for the usage message.
Returns
true on success, false otherwise.

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