1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
|
#include <iostream>
#include <string>
#include <map>
using namespace std;
int main(int argc, char *argv[]){
/***************************
* Petit parseur decommande
* G. Marcou
***************************/
typedef map<string,int> StrInt;
StrInt coms;
string com;//Inutile, pour montrer que c'est bien une variable string
// Definition de la map
coms["-to"]=0;
coms["-ti"]=1;
//Boucle sur les options
while ((argc > 1) && (argv[1][0] == '-')) {
// Trouver l'entree de la map qui correspond au string
com=argv[1];//On peut utiliser argv directement
map<string,int>::const_iterator i = coms.find( com );
// Blinder le programme
if(i == coms.end()){
test(argv,argc);
cout << "Unknown parameter" << endl;
return(1);
};
// Le fameux switch...
switch (i->second){
case 0://-to
--argc;
++argv;
cout << "toto" <<endl;
break;
case 1://-ti
--argc;
++argv;
cout << "titi" << endl;
break;
};
};
return(0);
} |
Partager