#include <SgCmdLineOpt.h>
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 67 of file 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 |
SgCmdLineOpt::SgCmdLineOpt | ( | ) |
Definition at line 19 of file SgCmdLineOpt.cpp.
bool SgCmdLineOpt::Contains | ( | const char * | option | ) | const |
const vector< string > & SgCmdLineOpt::GetArguments | ( | ) | const |
Get a list of the remaining command line arguments that are not an option.
Definition at line 28 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 38 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 33 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 58 of file SgCmdLineOpt.cpp.
References m_map.
int SgCmdLineOpt::GetInteger | ( | const char * | option | ) | const |
Get value of an integer option.
SgException | on error |
Definition at line 53 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 78 of file SgCmdLineOpt.cpp.
References m_map.
string SgCmdLineOpt::GetString | ( | const char * | option | ) | const |
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 133 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 87 of file SgCmdLineOpt.cpp.
Referenced by Parse().
std::vector<std::string> SgCmdLineOpt::m_args [private] |
std::map<std::string, std::string> SgCmdLineOpt::m_map [private] |
Definition at line 129 of file SgCmdLineOpt.h.
Referenced by Contains(), GetDouble(), GetInteger(), GetString(), and Parse().