I would just iterate through them. Here's a quick example I cooked up:
Code:
#include <iostream>
#include <string>
int parseParams(int count, char * arg[]);
#define PARSE_SUCCESS 0
#define PARSE_TOOFEW -1
#define PARSE_BADSWITCH -2
int main(int argc, char * argv[])
{
int i = parseParams(argc-1, argv + 1);
if (i == PARSE_SUCCESS) std::cout << "Parse Success" << std::endl;
if (i == PARSE_TOOFEW) std::cout << "Too Few Parameters" << std::endl;
if (i == PARSE_BADSWITCH) std::cout << "Unknown Switch" << std::endl;
}
int parseParams(int count, char * arg[]){
std::string sSwitch;
int c = 0;
while(true){
if (count - c < 1) return PARSE_SUCCESS; // We're out of parameters
sSwitch = arg[c];
c++;
if (sSwitch == "-a"){
if (count - c < 2) return PARSE_TOOFEW;
std::cout << "Switch A: " << arg[c] << ", " << arg[c+1] << std::endl;
c += 2;
}
else if (sSwitch == "-z"){
std::cout << "Switch Z" << std::endl;
}
else if (sSwitch == "-m"){
if (count - c < 1) return PARSE_TOOFEW;
std::cout << "Switch M:" << arg[c] << std::endl;
c++;
}
else return PARSE_BADSWITCH;
}
}
Here, c is the number of parameters we've parsed (so count - c is the number left to parse).
We use argc -1, argv + 1 because we don't want to parse the first entry of argv (which is the executable name).
Sample output:
C:\>test -m 4 -a "Hello there" "My friend" -z
Switch M:4
Switch A: Hello there, My friend
Switch Z
Parse Success