Programmation Avancée en C


getopt.c

00001 #include <getopt.h>
00002 #include <stdio.h>
00003 #include <stdlib.h>
00004 
00005 int main(int argc, char * argv[])
00006 {
00007         extern char *optarg;  // spécifique à getopt
00008         extern int optind;    // idem
00009         char c;
00010 
00011         while ((c = getopt(argc, argv, "gc:o:")) != -1) {
00012                 switch(c) {
00013                 case 'g': //code pour -g spécifié
00014                         break;
00015                 case 'c': //code pour -c spécifié;optarg contient l'argument requis
00016                         break;
00017                 case 'o': //code pour -o spécifié;optarg contient l'argument requis
00018                         break;
00019                 default : // cas d'une option non gérée
00020                         printf("option non reconnue!\n");
00021                         return EXIT_FAILURE;
00022                 }
00023         }
00024         /* Il peut y avoir des arguments non introduits par une option
00025            on les détecte en comparant la valeur de optind à argc :
00026              argc - optind == 0 : rien de plus n'a été ajouté
00027              argc - optind == 1 : un paramètre existe (correspondant à argv[optind])
00028              etc.
00029         */
00030         return 0;
00031 }