|
| cmnCommandLineOptions (void) |
|
| ~cmnCommandLineOptions () |
|
bool | AddOptionNoValue (const std::string &shortOption, const std::string &longOption, const std::string &description, RequiredType required=OPTIONAL_OPTION) |
|
template<typename _elementType > |
bool | AddOptionOneValue (const std::string &shortOption, const std::string &longOption, const std::string &description, RequiredType required, _elementType *value) |
|
template<typename _elementType > |
bool | AddOptionMultipleValues (const std::string &shortOption, const std::string &longOption, const std::string &description, RequiredType required, std::list< _elementType > *value) |
|
bool | Parse (int argc, const char *argv[], std::string &errorMessage) |
|
bool | Parse (int argc, char *argv[], std::string &errorMessage) |
|
void | PrintUsage (std::ostream &outputStream) |
|
bool | IsSet (const std::string &option) |
|
void | PrintParsedArguments (std::string &parsedArguments) const |
|
virtual | ~cmnGenericObject (void) |
|
virtual const
cmnClassServicesBase * | Services (void) const =0 |
|
bool | ReconstructFrom (const cmnGenericObject &other) |
|
std::string | ToString (void) const |
|
virtual void | ToStream (std::ostream &outputStream) const |
|
virtual void | ToStreamRaw (std::ostream &outputStream, const char delimiter= ' ', bool headerOnly=false, const std::string &headerPrefix="") const |
|
virtual bool | FromStreamRaw (std::istream &inputStream, const char delimiter= ' ') |
|
virtual void | SerializeRaw (std::ostream &outputStream) const |
|
virtual void | DeSerializeRaw (std::istream &inputStream) |
|
virtual cmnLogger::StreamBufType * | GetLogMultiplexer (void) const |
|
virtual size_t | ScalarNumber (void) const |
|
virtual bool | ScalarNumberIsFixed (void) const |
|
virtual double | Scalar (const size_t CMN_UNUSED(index)) const throw (std::out_of_range) |
|
virtual std::string | ScalarDescription (const size_t CMN_UNUSED(index), const std::string &CMN_UNUSED(userDescription)) const |
|
Set command line options and parse command line arguments.
This class should facilitate the development of command line tools requiring arguments. For example, it can be used to parse parameters such as "my_program -v --file path/file.h --iterations 2".
bool verbose;
std::string filename;
int iterations;
std::list<double> someNumbers;
std::string errorMessage;
if (!options.
Parse(argc, argv, errorMessage)) {
std::cerr << "Error: " << errorMessage << std::endl;
return -1;
}
verbose = options.
IsSet(
"v");
In this example, note that the first option added doesn't require a value, i.e. the caller should just use the short option (-v) or the long one (–verbose).
The second and third options are required, i.e. the "Parse" call will return false if one or more required option is not found. Both options are added as "option with one value" which means that the parser expect a value and this option can not be use twice (such as "my_program -i 2 -i 3). The last parameter of "AddOptionOneValue" can be a string, an integer, a double or anything the C++ operator
can stream in using a single argument of the command line.
The "Parse" method takes the argc and argv of the "main" function. It also requires an empty string passed by reference to generate an error message in case the parse fails. If parsing the command line options fails, one can use PrintUsage to display all the valid options.