std::pair<ArgList::iterator, bool> Engine::parseFirstIfKnown(ArgList& list, ArgList::iterator begin) { std::string& opt = *begin; if (opt[1] != '-') { // Short option char c = opt[1]; // Loopup the option engine map<char, Option*>::const_iterator engine = m_short.find(c); if (engine == m_short.end()) return make_pair(begin, false); // Parse the arguments, if any ArgList::iterator next = begin; ++next; engine->second->parse(list, next); // Dispose of the parsed argument if (opt[2] == 0) { // Remove what's left of the switch cluster as well list.eraseAndAdvance(begin); } else { // Else only remove the character from the switch opt.erase(opt.begin() + 1); } } else { // Long option // Split option and argument from "--foo=bar" size_t sep = opt.find('='); string name, arg; bool has_arg; if (sep == string::npos) { // No argument name = opt.substr(2); has_arg = false; } else { name = opt.substr(2, sep - 2); arg = opt.substr(sep + 1); has_arg = true; } map<string, Option*>::const_iterator engine = m_long.find(name); if (engine == m_long.end()) return make_pair(begin, false); if (has_arg) engine->second->parse(arg); else engine->second->parse_noarg(); // Remove the parsed element list.eraseAndAdvance(begin); } return make_pair(begin, true); }
ArgList::iterator Engine::parse(ArgList& list, ArgList::iterator begin) { rebuild(); bool foundNonSwitches = false; ArgList::iterator firstNonSwitch; while (begin != list.end()) { // Parse a row of switches begin = parseConsecutiveSwitches(list, begin); // End of switches? if (begin == list.end()) break; // If the end is the "--" marker, take it out of the list as well if (*begin == "--") { list.eraseAndAdvance(begin); break; } if (!foundNonSwitches) { // Mark the start of non-switches if we haven't done it already firstNonSwitch = begin; foundNonSwitches = true; } // Skip past the non switches while (begin != list.end() && !list.isSwitch(begin)) ++begin; } return foundNonSwitches ? firstNonSwitch : begin; }