1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
| #include <stdio.h>
#include <getopt.h>
void estPresent(int flag){
if(flag)
printf("present\n");
else
printf("absent\n");
}
int main(int argc, char **argv){
const struct option long_options[] = {
{ "optA", 0, NULL, 'a' },
{ "optB", 0, NULL, 'b' },
{ "optC", 0, NULL, 'c' },
{ "optD", 0, NULL, 'd' },
{ NULL, 0, NULL, 0 }
};
int next_option, flag_a=0, flag_b=0, compteur=1;
char *argument1="argument 1 par defaut", *argument2="argument 2 par defaut";
do {
next_option=getopt_long (argc, argv, "abc:d:", long_options, NULL);
switch(next_option){
case 'a':
flag_a = 1;
compteur++;
break;
case 'b':
flag_b = 1;
compteur++;
break;
case 'c':
argument1 = optarg;
compteur+=2;
break;
case 'd':
argument2 = optarg;
compteur+=2;
break;
}
}while(next_option!=-1);
printf("Flag a ");
estPresent(flag_a);
printf("Flag b ");
estPresent(flag_b);
printf("%s\n", argument1);
printf("%s\n", argument2);
if(compteur<argc){
printf("fichier 1 : %s\n", argv[compteur]);
if(compteur+1<argc)
printf("fichier 2 : %s\n", argv[compteur+1]);
}
} |
Partager