Created
January 13, 2013 13:05
-
-
Save yinyin/4524001 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <getopt.h> | |
int main(int argc, char *argv[]) | |
{ | |
int enable_dog; | |
int enable_egg; | |
int enable_farm; | |
struct option cmd_long_options[] = { | |
{"apple", required_argument, NULL, (int)('A')}, | |
{"boy", required_argument, NULL, (int)('b')}, | |
{"cat", no_argument, NULL, 3}, | |
{"dog", no_argument, &enable_dog, 4}, | |
{"egg", required_argument, &enable_egg, 5}, | |
{"farm", optional_argument, &enable_farm, 6}, | |
{(char *)(0), 0, (int *)(0), 0} | |
}; | |
int i; | |
enable_dog = -1; | |
enable_egg = -2; | |
enable_farm = -3; | |
optind = 1; | |
for(i = 1; i < (2*argc); i++) { | |
char *current_opt; | |
int parsed_opt; | |
int option_index; | |
char *option_value; | |
if(optind >= argc) | |
{ | |
printf("INFO: (optind:%d >= argc:%d). @[%s:%d]\n", optind, argc, __FILE__, __LINE__); | |
break; | |
} | |
current_opt = argv[optind]; | |
option_index = -1; | |
printf("(round=%d/%d)>> option: [%s]\n", i, optind, current_opt); | |
parsed_opt = getopt_long(argc, argv, "aBG:hk:012", cmd_long_options, &option_index); | |
option_value = (NULL == optarg) ? "(N/A)" : optarg; | |
printf("parsed: %d (%c): %s\n", parsed_opt, (char)(parsed_opt), option_value); | |
printf("enable-value: dog=%d, egg=%d, farm=%d.\n", enable_dog, enable_egg, enable_farm); | |
} | |
return 0; | |
} | |
/* | |
vim: ts=4 sw=4 ai nowrap | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment