static void print_help(char * progName)
{
printf("Usage: %s [options]\n\
-h \t\t\tprint this help message\n\
-i \t\tset the ineteger\n\
-f \tset the floating point number\n\
-s \t\tset the string\n\
-u \tset the unsigned integer\n\
-v \t\t\ttoggle verbose\n", progName);
}
char optionStr[] = "hf:i:s:u:v";
#if defined(_MSC_VER)
#define DIR_SEP '\\'
#else
#define DIR_SEP '/'
#endif
int main(int argc, char * argv[])
{
int c;
int integer = 0;
double floating_point = 0.0;
char * str = 0;
unsigned int unsigned_integer = 0;
int verbose = 0;
progName = strrchr(argv[0], DIR_SEP);
progName = (progName) ? progName+1 : argv[0];
while ((c = getopt(argc, argv, optionStr)) != EOF) {
switch (c) {
case 'h': print_help(progName); return 1;
case 'i': integer = atoi(optarg); break;
case 'f': floating_point = atof(optarg); break;
case 's': str = optarg; break;
case 'u': unsigned_integer = strtoul(optarg, 0, 0); break;
case 'v': verbose = !verbose; break;
default:
fprintf(stderr, "Try `%s -h' for more information.\n", progName);
return -1;
}
}
if (verbose) {
printf("%17s %d\n", "integer:", integer);
printf("%17s %f\n", "floating point:", floating_point);
printf("%17s 0x%x\n", "unsigned integer:", unsigned_integer);
printf("%17s %s\n", "string:", (str) ? str : "");
printf("\noptind: %d\n", optind);
}
return 0;
}
|