#include <OptionsParser.h>
The only public method parses the given list of arguments. It returns false when something failed. This may happen if the syntax of the arguments is invalid, a value is tried to be set several times or an unknown option is tried to be set.
The class assumes all options are unset or using default values only.
Definition at line 53 of file OptionsParser.h.
Static Public Member Functions | |
| static bool | parse (int argc, char **argv) throw (InvalidArgument) |
| Parses the given command line arguments. | |
Static Private Member Functions | |
| static int | check (char *arg1, char *arg2, bool &ok) throw (InvalidArgument) |
| parses the previous arguments | |
| static bool | checkParameter (char *arg1) throw () |
| Returns the whether the given token is an option. | |
| static std::string | convert (char abbr) throw () |
| converts char to string | |
| static std::string | convert (char *arg) throw () |
| Converts char* to string. | |
| static bool | isAbbreviation (char *arg1) throw () |
| returns the whether the given token is an abbreviation | |
| static bool | processNonBooleanSingleSwitch (OptionsCont &oc, char *arg) throw (InvalidArgument) |
| Extracts the parameter directly attached to an option. | |
| int OptionsParser::check | ( | char * | arg1, | |
| char * | arg2, | |||
| bool & | ok | |||
| ) | throw (InvalidArgument) [static, private] |
parses the previous arguments
| [in] | arg1 | The first token to parse |
| [in] | arg2 | The second token to parse, 0 if there is none |
| [in,out] | ok | Whether the parsing was successfull |
| InvalidArgument | If a performed setting of an option failed (see Option::set) |
Definition at line 70 of file OptionsParser.cpp.
References convert(), OptionsCont::getOptions(), OptionsCont::isBool(), and OptionsCont::set().
00070 { 00071 // the first argument should be an option 00072 // (only the second may be a free string) 00073 if (!checkParameter(arg1)) { 00074 ok = false; 00075 return 1; 00076 } 00077 00078 OptionsCont &oc = OptionsCont::getOptions(); 00079 // process not abbreviated switches 00080 if (!isAbbreviation(arg1)) { 00081 std::string tmp(arg1+2); 00082 size_t idx1 = tmp.find('='); 00083 // check whether a parameter was submitted 00084 if (idx1!=std::string::npos) { 00085 ok &= oc.set(tmp.substr(0, idx1), tmp.substr(idx1+1)); 00086 } else { 00087 if (arg2==0||oc.isBool(convert(arg1+2))) { 00088 ok &= oc.set(convert(arg1+2), true); 00089 } else { 00090 ok &= oc.set(convert(arg1+2), convert(arg2)); 00091 return 2; 00092 } 00093 } 00094 return 1; 00095 } 00096 // go through the abbreviated switches 00097 for (int i=1; arg1[i]!=0; i++) { 00098 // set boolean switches 00099 if (oc.isBool(convert(arg1[i]))) { 00100 ok &= oc.set(convert(arg1[i]), true); 00101 // set non-boolean switches 00102 } else { 00103 // check whether the parameter comes directly after the switch 00104 // and process if so 00105 if (arg2==0||arg1[i+1]!=0) { 00106 ok &= processNonBooleanSingleSwitch(oc, arg1+i); 00107 return 1; 00108 // process parameter following after a space 00109 } else { 00110 ok &= oc.set(convert(arg1[i]), convert(arg2)); 00111 // option name and attribute were in two arguments 00112 return 2; 00113 } 00114 } 00115 } 00116 // all switches within the current argument were boolean switches 00117 return 1; 00118 }
| bool OptionsParser::checkParameter | ( | char * | arg1 | ) | throw () [static, private] |
Returns the whether the given token is an option.
The given token is assumed to be an option if it starts with a '-'.
| [in] | arg1 | The token to check |
Definition at line 142 of file OptionsParser.cpp.
References MsgHandler::getErrorInstance(), and MsgHandler::inform().
00142 { 00143 if (arg1[0]!='-') { 00144 MsgHandler::getErrorInstance()->inform("The parameter '" + std::string(arg1) + "' is not allowed in this context.\n Switch or parameter name expected."); 00145 return false; 00146 } 00147 return true; 00148 }
| std::string OptionsParser::convert | ( | char | abbr | ) | throw () [static, private] |
converts char to string
| [in] | abbr | The char to convert |
Definition at line 165 of file OptionsParser.cpp.
00165 { 00166 char buf[2]; 00167 buf[0] = abbr; 00168 buf[1] = 0; 00169 std::string s(buf); 00170 return buf; 00171 }
| std::string OptionsParser::convert | ( | char * | arg | ) | throw () [static, private] |
Converts char* to string.
| [in] | arg | The c-string to convert |
Definition at line 158 of file OptionsParser.cpp.
| bool OptionsParser::isAbbreviation | ( | char * | arg1 | ) | throw () [static, private] |
returns the whether the given token is an abbreviation
The given token is assumed to be an option if it starts with two '-'.
| [in] | arg1 | The token to check |
Definition at line 152 of file OptionsParser.cpp.
| bool OptionsParser::parse | ( | int | argc, | |
| char ** | argv | |||
| ) | throw (InvalidArgument) [static] |
Parses the given command line arguments.
| [in] | oc | The options container to fill |
| [in] | argc | The number of given command line arguments |
| [in] | argv | The command line arguments |
| InvalidArgument | If a performed setting of an option failed (see Option::set) |
Definition at line 47 of file OptionsParser.cpp.
References MsgHandler::getErrorInstance(), and MsgHandler::inform().
Referenced by OptionsIO::getOptions().
00047 { 00048 bool ok = true; 00049 for (int i=1; i<argc;) { 00050 try { 00051 int add; 00052 // try to set the current option 00053 if (i<argc-1) { 00054 add = check(argv[i], argv[i+1], ok); 00055 } else { 00056 add = check(argv[i], 0, ok); 00057 } 00058 i += add; 00059 } catch (InvalidArgument &e) { 00060 MsgHandler::getErrorInstance()->inform("On processing option '" + std::string(argv[i]) + "':\n " + e.what()); 00061 i++; 00062 ok = false; 00063 } 00064 } 00065 return ok; 00066 }
| bool OptionsParser::processNonBooleanSingleSwitch | ( | OptionsCont & | oc, | |
| char * | arg | |||
| ) | throw (InvalidArgument) [static, private] |
Extracts the parameter directly attached to an option.
Parses single tokens which contain an option and the parameter (like -c=myconfig.cfg)
| [in] | oc | The container to store the result into |
| [in] | arg | The token to parse |
| InvalidArgument | If a performed setting of an option failed (see Option::set) |
Definition at line 122 of file OptionsParser.cpp.
References convert(), MsgHandler::getErrorInstance(), and MsgHandler::inform().
00122 { 00123 if (arg[1]=='=') { 00124 if (strlen(arg)<3) { 00125 MsgHandler::getErrorInstance()->inform("Missing value for parameter '" + std::string(arg).substr(0, 1) + "'."); 00126 return false; 00127 } else { 00128 return oc.set(convert(arg[0]), std::string(arg+2)); 00129 } 00130 } else { 00131 if (strlen(arg)<2) { 00132 MsgHandler::getErrorInstance()->inform("Missing value for parameter '" + std::string(arg) + "'."); 00133 return false; 00134 } else { 00135 return oc.set(convert(arg[0]), std::string(arg+1)); 00136 } 00137 } 00138 }
1.5.6