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 54 55 56 57 58 59 60 61
| #include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
int main(int argc, char ** argv){
int vitesse_lecture = 0;
if(argc < 2){ //Trop peu d'arguments
fprintf(stderr, "Trop peu d'arguments\n");
return 1;
}
char * optstring = "d:f:";
struct option longopts[] = {
/* name has_arg flag val */
{ "debut", 1, NULL, 'd' },
{ "fin", 1, NULL, 'f' },
{ "rapide", 0, &vitesse_lecture, 1 },
{ "lent", 0, &vitesse_lecture, -1},
/* Le dernier élément doit etre nul */
{ NULL, 0, NULL, 0 },
};
int longindex;
int option;
int debut = 0;
int fin = 999;
while((option = getopt_long(argc, argv, optstring,
longopts, &longindex)) != -1){
switch(option){
case 'd':
fprintf(stdout, "optarg = %d\n", *optarg);
//debut = *optarg;
sscanf(optarg, "%d", &debut);
break;
case 'f':
fprintf(stdout, "optarg = %d\n", *optarg);
//fin = *optarg;
sscanf(optarg, "%d", &fin);
break;
}
}
fprintf(stdout, "Vitesse %d, debut %d, fin %d\n",
vitesse_lecture, debut, fin);
return 0;
} |
Partager