| 148 | } |
| 149 | |
| 150 | int CommandLineArguments::Parse() |
| 151 | { |
| 152 | std::vector<std::string>::size_type cc; |
| 153 | std::vector<std::string> matches; |
| 154 | if (this->StoreUnusedArgumentsFlag) { |
| 155 | this->Internals->UnusedArguments.clear(); |
| 156 | } |
| 157 | for (cc = 0; cc < this->Internals->Argv.size(); cc++) { |
| 158 | std::string const& arg = this->Internals->Argv[cc]; |
| 159 | CommandLineArguments_DEBUG("Process argument: " << arg); |
| 160 | this->Internals->LastArgument = cc; |
| 161 | if (this->GetMatchedArguments(&matches, arg)) { |
| 162 | // Ok, we found one or more arguments that match what user specified. |
| 163 | // Let's find the longest one. |
| 164 | CommandLineArguments::Internal::VectorOfStrings::size_type kk; |
| 165 | CommandLineArguments::Internal::VectorOfStrings::size_type maxidx = 0; |
| 166 | CommandLineArguments::Internal::String::size_type maxlen = 0; |
| 167 | for (kk = 0; kk < matches.size(); kk++) { |
| 168 | if (matches[kk].size() > maxlen) { |
| 169 | maxlen = matches[kk].size(); |
| 170 | maxidx = kk; |
| 171 | } |
| 172 | } |
| 173 | // So, the longest one is probably the right one. Now see if it has any |
| 174 | // additional value |
| 175 | CommandLineArgumentsCallbackStructure* cs = |
| 176 | &this->Internals->Callbacks[matches[maxidx]]; |
| 177 | std::string const& sarg = matches[maxidx]; |
| 178 | if (cs->Argument != sarg) { |
| 179 | abort(); |
| 180 | } |
| 181 | switch (cs->ArgumentType) { |
| 182 | case NO_ARGUMENT: |
| 183 | // No value |
| 184 | if (!this->PopulateVariable(cs, nullptr)) { |
| 185 | return 0; |
| 186 | } |
| 187 | break; |
| 188 | case SPACE_ARGUMENT: |
| 189 | if (cc == this->Internals->Argv.size() - 1) { |
| 190 | this->Internals->LastArgument--; |
| 191 | return 0; |
| 192 | } |
| 193 | CommandLineArguments_DEBUG("This is a space argument: " |
| 194 | << arg << " value: " |
| 195 | << this->Internals->Argv[cc + 1]); |
| 196 | // Value is the next argument |
| 197 | if (!this->PopulateVariable(cs, |
| 198 | this->Internals->Argv[cc + 1].c_str())) { |
| 199 | return 0; |
| 200 | } |
| 201 | cc++; |
| 202 | break; |
| 203 | case EQUAL_ARGUMENT: |
| 204 | if (arg.size() == sarg.size() || arg.at(sarg.size()) != '=') { |
| 205 | this->Internals->LastArgument--; |
| 206 | return 0; |
| 207 | } |