60 progName_(
""), description_(description) {
87 string name = (*i).first;
94 string name = (*i).first;
114 for (
int i = 1; i < argc; i++) {
133 for (
unsigned int i = 0; i <
options.size(); i++) {
135 string::size_type curPos = 0;
137 string::size_type pos =
options[i].find(
" ", curPos);
138 if (pos != string::npos) {
142 if (
options[i].length() - curPos > 0) {
171 string msg =
"Unknown option: " + name;
172 string method =
"CmdLineParser::findOption()";
185 bool finished =
false;
189 bool checkArguments =
true;
198 string arguments =
"";
201 bool hasArgument =
true;
203 if (!
parseOption(optString, name, arguments, prefix, hasArgument)) {
205 if (optString ==
"--") {
206 checkArguments =
false;
218 if (arguments ==
"" &&
230 bool doneWithParsing = opt->
parseValue(arguments, prefix);
232 if (!doneWithParsing) {
241 for (
unsigned int i = 0; i < arguments.length(); i++) {
250 if (checkArguments && optString[0] ==
'-') {
251 string msg =
"Illegal command line argument: " + optString;
252 string method =
"CmdLineParser::parse()";
278 std::string option, std::string& name, std::string& arguments,
279 std::string& prefix,
bool& hasArgument)
const {
281 bool longOption =
false;
282 if (!
readPrefix(option, prefix, longOption)) {
288 unsigned int pos = 0;
289 while (pos < option.length() && option[pos] !=
'=') {
292 name = option.substr(0, pos);
293 option.erase(0, pos);
296 name = option.substr(0, 1);
301 if (option.length() > 0 && option[0] ==
'=') {
303 string method =
"CmdLineParser::parseOption()";
304 string message =
"Illegal short option: = not allowed.";
311 if (arguments ==
"" || !longOption) {
330 bool& longOption)
const {
332 if (option ==
"--") {
334 }
else if (option.substr(0, 1) !=
"-") {
338 if (option.substr(0, 1) ==
"-") {
345 for (
unsigned int i = 0; i <
prefixes_.size(); i++) {
347 if (option.length() >
prefixes_[i].length() &&
351 option.erase(0, prefix.length());
static MachInfoCmdLineOptions options
virtual OptionValue * copy() const =0
virtual bool parseValue(std::string arguments, std::string prefix)=0
Pure virtual function that parses the value of option.
static const int SHORT_FLAG
Number of characters reserved for printing short version of commandline flag.
std::map< std::string, CmdLineOptionParser * >::const_iterator constMapIter
For traversing const maps.
std::map< std::string, CmdLineOptionParser * > optionLongNames_
Database for holding options with their long names as a key.
virtual void storeOptions(Options &options)
std::string progName_
The name of the program.
std::vector< std::string > prefixes_
Legal prefixes are stored here.
CmdLineParser(std::string description)
std::vector< std::string > commandLine_
Command line is stored here.
std::map< std::string, CmdLineOptionParser * >::iterator mapIter
For traversing non-const maps.
virtual void parse(char *argv[], int argc)
CmdLineOptionParser * findOption(std::string name) const
std::vector< std::string > arguments_
Command line arguments are stored here.
bool parseOption(std::string option, std::string &name, std::string &arguments, std::string &prefix, bool &hasArgument) const
bool readPrefix(std::string &option, std::string &prefix, bool &longOption) const
std::map< std::string, CmdLineOptionParser * > optionShortNames_
Database for holding options with their short names as a key.
static const int LONG_FLAG
Number of characters reserved for printing long version of commandline flag.