From b853f7469a6283e5d52d0e657f08282015f52734 Mon Sep 17 00:00:00 2001 From: Philippe Pittoli Date: Sun, 18 Sep 2011 23:56:59 +0200 Subject: [PATCH] Add skel_getopts --- C_Language/skel_getopts.c | 40 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 C_Language/skel_getopts.c diff --git a/C_Language/skel_getopts.c b/C_Language/skel_getopts.c new file mode 100644 index 0000000..1a29617 --- /dev/null +++ b/C_Language/skel_getopts.c @@ -0,0 +1,40 @@ +#include +#include +#include + +int main(int argc, char * argv[]) +{ + // Options simples + char * liste_options = "s:e:ol"; + int option; + + opterr = 0; // Pas de message d'erreur + + while((option = getopt(argc,argv,liste_options)) != -1) + { + switch(option) + { + case 's': + fprintf(stdout,"option s %s\n",optarg); + break; + case 'e': + fprintf(stdout,"option e %s\n",optarg); + break; + case 'o': + case 'l': + fprintf(stdout,"option %c",option); + break; + case '?': + fprintf(stdout,"erreur : %s\n",optopt); + break; + } + // s'il reste des options + if(argc != optind) + { + fprintf(stdout, "Arguments restants : \n"); + while(optind != argc) + fprintf(stdout, " %s \n", argv[optind++]); + } + return EXIT_SUCCESS; +} +