Parser for command line options. More...
#include <SgCmdLineOpt.h>
Public Member Functions | |
SgCmdLineOpt () | |
bool | Contains (const char *option) const |
const std::vector< std::string > & | GetArguments () const |
Get a list of the remaining command line arguments that are not an option. | |
double | GetDouble (const char *option) const |
Get value of a floating point option. | |
double | GetDouble (const char *option, double defaultValue) const |
Get value of a floating point option or use default value. | |
int | GetInteger (const char *option) const |
Get value of an integer option. | |
int | GetInteger (const char *option, int defaultValue) const |
Get value of an integer option or use default value. | |
std::string | GetString (const char *option) const |
Get value of a string option. | |
std::string | GetString (const char *option, const std::string &defaultValue) const |
Get value of a string option or use default value. | |
void | Parse (int argc, char *argv[], const std::vector< std::string > &specs) |
Parse options from main(argc, argv). | |
void | Parse (int argc, const char *argv[], const std::vector< std::string > &specs) |
Parse options from user created array. | |
Private Attributes | |
std::vector< std::string > | m_args |
std::map< std::string, std::string > | m_map |
Parser for command line options.
Options start with the character '-'. The end of options can be indicated with "--" to allow arguments beginning with '-'.
Example:
In this example the allowed options are: a required option with integer argument "-optint", a option with string argument "-optstr" and default value "" and a option with no argument "-optbool" and default off.
static bool s_optBool; static int s_optInt; static string s_optStr; static void ParseOptions(int argc, char** argv) { SgCmdLineOpt cmdLineOpt; vector<string> specs; specs.push_back("optint:"); specs.push_back("optstr:"); specs.push_back("optbool"); cmdLineOpt.Parse(argc, argv, specs); if (! cmdLineOpt.Contains("optint")) throw SgException("Requires option -optint"); cmdLineOpt.GetInteger("optint", s_optInt); s_optStr = cmdLineOpt.GetString("optstr", ""); s_optBool = cmdLineOpt.Contains("optbool"); } int main(int argc, char** argv) { try { ParseOptions(argc, argv); // ... } catch (const std::exception& e) { SgDebug() << e.what() << '\n'; return 1; } return 0; }
Definition at line 65 of file SgCmdLineOpt.h.
SgCmdLineOpt::SgCmdLineOpt | ( | ) |
Definition at line 18 of file SgCmdLineOpt.cpp.
bool SgCmdLineOpt::Contains | ( | const char * | option | ) | const |
Definition at line 22 of file SgCmdLineOpt.cpp.
References m_map.
const vector< string > & SgCmdLineOpt::GetArguments | ( | ) | const |
Get a list of the remaining command line arguments that are not an option.
Definition at line 27 of file SgCmdLineOpt.cpp.
References m_args.
double SgCmdLineOpt::GetDouble | ( | const char * | option, | |
double | defaultValue | |||
) | const |
Get value of a floating point option or use default value.
SgException | on error |
Definition at line 37 of file SgCmdLineOpt.cpp.
References m_map.
double SgCmdLineOpt::GetDouble | ( | const char * | option | ) | const |
Get value of a floating point option.
SgException | on error |
Definition at line 32 of file SgCmdLineOpt.cpp.
int SgCmdLineOpt::GetInteger | ( | const char * | option | ) | const |
Get value of an integer option.
SgException | on error |
Definition at line 52 of file SgCmdLineOpt.cpp.
int SgCmdLineOpt::GetInteger | ( | const char * | option, | |
int | defaultValue | |||
) | const |
Get value of an integer option or use default value.
SgException | on error |
Definition at line 57 of file SgCmdLineOpt.cpp.
References m_map.
string SgCmdLineOpt::GetString | ( | const char * | option | ) | const |
Get value of a string option.
Definition at line 72 of file SgCmdLineOpt.cpp.
string SgCmdLineOpt::GetString | ( | const char * | option, | |
const std::string & | defaultValue | |||
) | const |
Get value of a string option or use default value.
Definition at line 77 of file SgCmdLineOpt.cpp.
References m_map.
void SgCmdLineOpt::Parse | ( | int | argc, | |
const char * | argv[], | |||
const std::vector< std::string > & | specs | |||
) |
Parse options from user created array.
Uses const char* arguments. The only portable version of a C++ main() function uses char* arguments, but for testing this class with a user created array, it is better to use const char*, because assigning a string constant to char* causes compiler warnings with some compilers.
Definition at line 132 of file SgCmdLineOpt.cpp.
References Parse().
void SgCmdLineOpt::Parse | ( | int | argc, | |
char * | argv[], | |||
const std::vector< std::string > & | specs | |||
) |
Parse options from main(argc, argv).
argc | Number of elements in argv | |
argv | Argument vector from main(). | |
specs | Array of valid options (not including the leading '-'). Append ':' to options that are allowed to have an argument. |
SgException | on error |
Definition at line 86 of file SgCmdLineOpt.cpp.
Referenced by Parse().
std::vector<std::string> SgCmdLineOpt::m_args [private] |
Definition at line 118 of file SgCmdLineOpt.h.
Referenced by GetArguments(), and Parse().
std::map<std::string, std::string> SgCmdLineOpt::m_map [private] |
Definition at line 120 of file SgCmdLineOpt.h.
Referenced by Contains(), GetDouble(), GetInteger(), GetString(), and Parse().