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.
Definition at line 86 of file CommandOptionParser.hpp.
#include <CommandOptionParser.hpp>
Public Types | |
typedef std::map< std::string, gnsstk::CommandOption * > | CommandOptionMap |
Public Member Functions | |
CommandOptionParser & | addOption (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... | |
typedef std::map<std::string, gnsstk::CommandOption*> gnsstk::CommandOptionParser::CommandOptionMap |
Typedef for a map between the command line option (-f) and the associated CommandOption.
Definition at line 91 of file CommandOptionParser.hpp.
gnsstk::CommandOptionParser::CommandOptionParser | ( | const std::string & | description, |
const CommandOptionVec & | optList = defaultCommandOptionList |
||
) |
Constructor given a text description of the program.
description | a short description of this program |
optList | a CommandOptionVec with the list of CommandOptions for this parser. |
Definition at line 62 of file CommandOptionParser.cpp.
CommandOptionParser & gnsstk::CommandOptionParser::addOption | ( | gnsstk::CommandOption & | co | ) |
Adds the CommandOption to the list for parsing.
Definition at line 80 of file CommandOptionParser.cpp.
ostream & gnsstk::CommandOptionParser::displayUsage | ( | std::ostream & | out, |
bool | doPretty = true |
||
) |
Writes the arguments nicely to the output.
out | ostream on which to write |
doPretty | if true (the default), 'pretty print' descriptions |
Definition at line 375 of file CommandOptionParser.cpp.
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.
out | ostream on which to write |
Definition at line 440 of file CommandOptionParser.cpp.
ostream & gnsstk::CommandOptionParser::dumpErrors | ( | std::ostream & | out | ) |
Writes the errors to out
.
Definition at line 346 of file CommandOptionParser.cpp.
|
inline |
Returns true if any processing errors occurred.
Definition at line 114 of file CommandOptionParser.hpp.
|
inline |
Returns true if any help was requested.
Definition at line 118 of file CommandOptionParser.hpp.
void gnsstk::CommandOptionParser::parseOptions | ( | int | argc, |
char * | argv[] | ||
) |
Parses the command line.
Definition at line 115 of file CommandOptionParser.cpp.
std::ostream & gnsstk::CommandOptionParser::printHelp | ( | std::ostream & | out, |
bool | doPretty = true , |
||
bool | firstOnly = true |
||
) |
Print the requested help information.
[in] | out | The stream to which the help text will be printed. |
[in] | doPretty | If true, use "pretty print" as appropriate (dependent on child class implementation as to how it's used). |
[in] | firstOnly | If 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.
|
private |
changes the size of the option array for getopt_long.
Definition at line 556 of file CommandOptionParser.cpp.
|
private |
The vector of error strings for displaying to the user.
Definition at line 152 of file CommandOptionParser.hpp.
|
private |
whether or not this command line has optional options
Definition at line 159 of file CommandOptionParser.hpp.
|
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.
|
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.
|
private |
The vector of CommandOptions for the parser.
Definition at line 150 of file CommandOptionParser.hpp.
|
private |
the name of this program
Definition at line 165 of file CommandOptionParser.hpp.
|
private |
the description of this program
Definition at line 162 of file CommandOptionParser.hpp.