static void print_help(char * prog_name)
{
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", prog_name);
}
char option_str[] = "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;
prog_name = strrchr(argv[0], DIR_SEP);
prog_name = (prog_name) ? prog_name+1 : argv[0];
while ((c = getopt(argc, argv, option_str)) != EOF) {
switch (c) {
case 'h': print_help(prog_name); 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", prog_name);
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;
}
|