C++ argv[] switch
Posted: May 26, 2010 Filed under: c++ Leave a comment »int main(int argc, char *argv[]) {
char usage[] =
"\n"
" %s - (c) 2009-2010 Name Surname\n"
" http://your.homepage.com\n"
"\n"
" usage: %s [optional values]\n"
"\n";
if(argc <= 1) { // show usage if no parameter is specified
printf(usage, "1.0.0", argv[0]);
return 0;
}
for(int i=1; i<argc; i++) {
if(argv[i][0] == '-') { // if value starts with "-" it should be the command
switch(argv[i][1]) {
case 'a':
printf("you selected -a\n");
printf("a was: %s", argv[i+1]);
break;
case 'b':
printf("you selected -b");
printf("b was: %s", argv[i+1]);
break;
default:
printf(usage, "1.0.0", argv[0]);
}
}
}
return 0;
}
Protams visi ienākošie dati ir jāpārbauda vai viņi atbilst prasībām & stuff.
Advertisement