Public Types | Public Member Functions | Private Member Functions | Private Attributes | List of all members
gnsstk::CommandOptionParser Class Reference

Detailed Description

This class parses the command line options and modifies the corresponding CommandOptions. By default, any CommandOptions you create will be put on a static vector<CommandOption> which is used by CommandOptionParser. You can make your own as well but that isn't necessary. You can also use addOption() to add individual CommandOptions to the parser, but again this isn't necessary as the default list is usually sufficient.

Call parseOptions() to process the command line, then call hasErrors() to see if there were any problems parsing the string. Errors can occur when a required option isn't found on the command line, when an option requiring an argument doesn't have one, or when an argument appers more than its maxCount number of times among other errors. If so, use dumpErrors() to display the errors to an output stream, then use displayUsage() to display a well formatted list of the correct command line options. Of course, you can just as well ignore any command line errors. After hitting an error (which most often happens when it hits an argument that has no CommandOption), you can use CommandOptionRest to get the unprocessed command line options.

See also
the getopttest.cpp file in the test code for some examples.

Definition at line 86 of file CommandOptionParser.hpp.

#include <CommandOptionParser.hpp>

Public Types

typedef std::map< std::string, gnsstk::CommandOption * > CommandOptionMap
 

Public Member Functions

CommandOptionParseraddOption (gnsstk::CommandOption &co)
 
 CommandOptionParser (const std::string &description, const CommandOptionVec &optList=defaultCommandOptionList)
 
std::ostream & displayUsage (std::ostream &out, bool doPretty=true)
 
std::ostream & displayUsageDoxygen (std::ostream &out)
 
std::ostream & dumpErrors (std::ostream &out)
 Writes the errors to out. More...
 
bool hasErrors ()
 Returns true if any processing errors occurred. More...
 
bool helpRequested ()
 Returns true if any help was requested. More...
 
void parseOptions (int argc, char *argv[])
 Parses the command line. More...
 
std::ostream & printHelp (std::ostream &out, bool doPretty=true, bool firstOnly=true)
 

Private Member Functions

void resizeOptionArray (struct option *&oldArray, unsigned long &oldSize)
 changes the size of the option array for getopt_long. More...
 

Private Attributes

std::vector< std::string > errorStrings
 The vector of error strings for displaying to the user. More...
 
bool hasOptionalArguments
 whether or not this command line has optional options More...
 
bool hasRequiredArguments
 The vector of unprocessed command line arguments. More...
 
std::vector< CommandOptionHelp * > helpOptions
 
CommandOptionVec optionVec
 The vector of CommandOptions for the parser. More...
 
std::string progName
 the name of this program More...
 
std::string text
 the description of this program More...
 

Member Typedef Documentation

◆ CommandOptionMap

Typedef for a map between the command line option (-f) and the associated CommandOption.

Definition at line 91 of file CommandOptionParser.hpp.

Constructor & Destructor Documentation

◆ CommandOptionParser()

gnsstk::CommandOptionParser::CommandOptionParser ( const std::string &  description,
const CommandOptionVec optList = defaultCommandOptionList 
)

Constructor given a text description of the program.

Warning
The CommandOptions in optList must exist for the entire lifetime of this CommandOptionParser.
Parameters
descriptiona short description of this program
optLista CommandOptionVec with the list of CommandOptions for this parser.

Definition at line 62 of file CommandOptionParser.cpp.

Member Function Documentation

◆ addOption()

CommandOptionParser & gnsstk::CommandOptionParser::addOption ( gnsstk::CommandOption co)

Adds the CommandOption to the list for parsing.

Warning
The CommandOption must exist for the entire lifetime of this CommandOptionParser.

Definition at line 80 of file CommandOptionParser.cpp.

◆ displayUsage()

ostream & gnsstk::CommandOptionParser::displayUsage ( std::ostream &  out,
bool  doPretty = true 
)

Writes the arguments nicely to the output.

Parameters
outostream on which to write
doPrettyif true (the default), 'pretty print' descriptions

Definition at line 375 of file CommandOptionParser.cpp.

◆ displayUsageDoxygen()

ostream & gnsstk::CommandOptionParser::displayUsageDoxygen ( std::ostream &  out)

Writes the arguments to the output stream in a format that can by copied and pasted into doxygen comments for the application so that the application documentation matches the code.

Parameters
outostream on which to write
Todo:
add smarts to the synopsis section generation that handles the Mutex and other similar meta-options so the generated synopsis doesn't produce option sets that won't work
Todo:
is this really correct in all cases? Probably not.

Definition at line 440 of file CommandOptionParser.cpp.

◆ dumpErrors()

ostream & gnsstk::CommandOptionParser::dumpErrors ( std::ostream &  out)

Writes the errors to out.

Definition at line 346 of file CommandOptionParser.cpp.

◆ hasErrors()

bool gnsstk::CommandOptionParser::hasErrors ( )
inline

Returns true if any processing errors occurred.

Definition at line 114 of file CommandOptionParser.hpp.

◆ helpRequested()

bool gnsstk::CommandOptionParser::helpRequested ( )
inline

Returns true if any help was requested.

Definition at line 118 of file CommandOptionParser.hpp.

◆ parseOptions()

void gnsstk::CommandOptionParser::parseOptions ( int  argc,
char *  argv[] 
)

Parses the command line.

Definition at line 115 of file CommandOptionParser.cpp.

◆ printHelp()

std::ostream & gnsstk::CommandOptionParser::printHelp ( std::ostream &  out,
bool  doPretty = true,
bool  firstOnly = true 
)

Print the requested help information.

Parameters
[in]outThe stream to which the help text will be printed.
[in]doPrettyIf true, use "pretty print" as appropriate (dependent on child class implementation as to how it's used).
[in]firstOnlyIf true, only print the help for the first processed command-line option requesting help. If false, each specified help-like command-line option will print its help text.

Definition at line 354 of file CommandOptionParser.cpp.

◆ resizeOptionArray()

void gnsstk::CommandOptionParser::resizeOptionArray ( struct option *&  oldArray,
unsigned long &  oldSize 
)
private

changes the size of the option array for getopt_long.

Definition at line 556 of file CommandOptionParser.cpp.

Member Data Documentation

◆ errorStrings

std::vector<std::string> gnsstk::CommandOptionParser::errorStrings
private

The vector of error strings for displaying to the user.

Definition at line 152 of file CommandOptionParser.hpp.

◆ hasOptionalArguments

bool gnsstk::CommandOptionParser::hasOptionalArguments
private

whether or not this command line has optional options

Definition at line 159 of file CommandOptionParser.hpp.

◆ hasRequiredArguments

bool gnsstk::CommandOptionParser::hasRequiredArguments
private

The vector of unprocessed command line arguments.

whether or not this command line has any rrequired options

Definition at line 157 of file CommandOptionParser.hpp.

◆ helpOptions

std::vector<CommandOptionHelp*> gnsstk::CommandOptionParser::helpOptions
private

After calling parseOptions, this will contain all of the help-like options that were specified on the command line.

Definition at line 170 of file CommandOptionParser.hpp.

◆ optionVec

CommandOptionVec gnsstk::CommandOptionParser::optionVec
private

The vector of CommandOptions for the parser.

Definition at line 150 of file CommandOptionParser.hpp.

◆ progName

std::string gnsstk::CommandOptionParser::progName
private

the name of this program

Definition at line 165 of file CommandOptionParser.hpp.

◆ text

std::string gnsstk::CommandOptionParser::text
private

the description of this program

Definition at line 162 of file CommandOptionParser.hpp.


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


gnsstk
Author(s):
autogenerated on Wed Oct 25 2023 02:40:44