Récupérer les arguments de la ligne de commande avec getopt_long

Voici un extrait du code (actuel) de BIDON qui j'espère pourra illustrer un peu l'utilisation de la fonction getopt_long.
Pour les informations sur les informations sur cette fonction :
* man 3 getopt
* http://www.gnu.org/software/libtool/manual/libc/Getopt.html
* http://www.opengroup.org/onlinepubs/000095399/functions/getopt.html
* Le C en action (aux éditions O'Reilly)
#include <stdio.h> #include <stdlib.h> #include <getopt.h> /* The name of this program */ const char* program_name; /* Print infos about program version and authors */ void print_versionInfo() { printf("you are running version %.3lf\n", VERSION); printf("this program was developped by %s\n",AUTHORS); printf("you can find some information on bidon's project page at %s\n",WEBSITE); exit(EXIT_SUCCESS); } /* Print help an exit with exit code exit_msg */ void print_help(FILE *stream, int exit_msg) { fprintf(stream,"usage : %s [ options ]\n",program_name); printf("valid options are :\n"); fprintf(stream, " -h --help\t print this message\n" " -s --server server_name\t the name of the server you want to connect to\n" " -P --port port_number\t this not a needed option, you may used it if you want to connecter on port\ #port_number instead of default 5222\n" " -l --login gid\t login into server_name with gid as your login. You need your full gid.\ i.e. if you are toto and your account is on jabber.clubnix.esiee.fr you need to do :\n \ %s -s jabber.clubnix.esiee.fr -l toto@jabber.clubnix.esiee.fr\n" " -p --passwd password\t it is your password\n" " -r --recipient recipient\t your messages will be sent to recipient" " -v --version\t print version and exit" " -S --SINGLE\t use this if you want to start a user to user chat.\ by default the chat_mode is set to MUC to allow to connect to a Multi Users Channel\n" ,program_name); exit(exit_msg); } int main( int argc, char **argv) { int next_option; /* A string listing the valid short options letters */ const char *short_options = "s:P::l:p:r:hvS"; /* An array listing valid long options */ static const struct option long_options[]= { {"server",required_argument,NULL,'s'}, {"port",optional_argument,NULL,'P'}, {"login",required_argument,NULL,'l'}, {"passwd",required_argument,NULL,'p'}, {"recipient",required_argument,NULL,'r'}, {"help",no_argument,NULL,'h'}, {"version",no_argument,NULL,'v'}, {"Single", no_argument,NULL,'S'}, {NULL, 0, NULL, 0} /* End of array need by getopt_long do not delete it*/ }; /* You can connect to a serveur in 2 modes : * SINGLE start a conversation with an other user already connected to the server * MUC Multi Users Channel to connect to a chat room */ typedef enum chat_mode chat_mode; enum chat_mode { SINGLE, MUC }; char *server_name; int port; char *login_name; char *passwd; char *recipient; /* By default chat_mode is set to MUC since we want to managed a chat room */ chat_mode mode = MUC; /* Remember the program name to incorporate it in messages. */ program_name = argv[0]; do { next_option = getopt_long(argc, argv, short_options, long_options, NULL ); switch(next_option) { case 's': server_name = optarg; printf("you will be connected to %s\n",server_name); break; case 'P': if (NULL != optarg) { port = atoi(optarg); printf("you be connected on %s on port %d\n",server_name,port); } break; case 'l': login_name = optarg; printf("your will be logged as %s\n",login_name); break; case 'p': passwd = optarg; printf("use %s as password\n",passwd); break; case 'r': recipient = optarg; printf("your messages will be sent to %s\n",recipient); break; case 'S': mode = SINGLE; printf("entering in SINGLE chat mode\n"); break; case 'h': print_help(stdout, EXIT_SUCCESS); break; case 'v': print_versionInfo(); break; case '?': /* An invalide option has been used, print help an exit with code EXIT_FAILURE */ print_help(stderr, EXIT_FAILURE); break; } } while(next_option !=-1); if (optind < argc) { printf ("this arguments are not reconized: "); while (optind < argc) printf ("%s ", argv[optind++]); printf ("\n"); } exit(EXIT_SUCCESS); }
Normalement avec ces informations vous devriez être en mesure de coder votre fonction getopt_long. Il ne reste plus qu'à parser les variables utilisées pour stocker les résultats de l'appel de main aux autres fonctions.
A noter : On peut aussi utiliser des librairies autres que celles fournies par le projet GNU, comme la Glib qui a l'avantage d'être disponible sur GNU/Linux *-BSD et MS Windows. Voici un article intéressant :
http://www.unixgarden.com/index.php/programmation/dissection-de-glib-l%E...