Skip to content

Instantly share code, notes, and snippets.

@mengqingzhong
Last active August 29, 2015 14:26
Show Gist options
  • Save mengqingzhong/8b202ca3e8e61b2fdde9 to your computer and use it in GitHub Desktop.
Save mengqingzhong/8b202ca3e8e61b2fdde9 to your computer and use it in GitHub Desktop.
option
#include<stdio.h>
#include <getopt.h>
int flag;
struct option long_options[] = {
{ "deamon", no_argument, NULL, 'D' },
{ "loglevel", required_argument, NULL, 'L' },
{ "ip", required_argument,&flag, 1 },
{ "port", required_argument, &flag, 2 },
};
int main(int argc, char**argv) {
int option_index = 0;
while (1) {
int c = getopt_long(argc, argv, "DL:", long_options, &option_index);
if (c == -1) {
break;
}
switch (c) {
case 'D':
printf("D\n");
break;
case 'L':
printf("L %s\n", optarg);
break;
case 0:
switch (flag) {
case 1:
printf("flag 1 %s\n", optarg);
break;
case 2:
printf("flag 2 %s\n", optarg);
break;
}
}
}
return 0;
}
@mengqingzhong
Copy link
Author

./a.out -D -L 8 --ip ljljk --port 23 --port 46

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment