That's a very unusual way to do argument parsing, and it may cause problems when the parameter to an argument is also a valid option itself (such as in your --loginname "--password"
example.
A more idiomatic C++ approach (either directly in your code, or buried in an option-parsing library) iterates over argv[]
, considering each word in turn:
#include <iostream>
#include <string>
#include <vector>
int main(int argc, char **argv)
{
std::string username;
std::string password;
std::vector<std::string> values;
for (auto i = 1; i < argc; ++i) {
std::string arg = argv[i];
if (arg == "--username") {
++i;
if (i >= argc) {
std::cerr << argv[0] << ": --username requires a value" << std::endl;
return 1;
}
username = argv[i];
} else if (arg == "--password") {
++i;
if (i >= argc) {
std::cerr << argv[0] << ": --password requires a value" << std::endl;
return 1;
}
password = argv[i];
} else {
values.emplace_back(argv[i]);
}
}
std::cout << "Username = " << username << '\n';
std::cout << "Password = " << password << '\n';
for (auto i = 0u; i < values.size(); ++i)
std::cout << "Argument " << i << " = " << values[i] << '\n';
}
Obviously there's a lot of duplication above, but the principle applies: test each argument in turn, and if it's an option that requires a value, advance the argument index and read that value.
Once you get past a small number of options, you'll end up creating a mapping from option name to a representation of how to process that option, and either use or re-invent an option processing library.