some-usable-scripts/c/skel_getopts.c

45 lines
814 B
C
Raw Normal View History

2011-09-18 23:56:59 +02:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
2011-09-19 00:00:39 +02:00
#include <unistd.h>
2011-09-18 23:56:59 +02:00
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':
2011-09-19 00:07:32 +02:00
fprintf(stdout,"option %c\n",option);
2011-09-18 23:56:59 +02:00
break;
case '?':
fprintf(stdout,"erreur : %s\n",optopt);
break;
default:
break;
2011-09-18 23:56:59 +02:00
}
2011-09-19 00:00:39 +02:00
}
2011-09-18 23:56:59 +02:00
// 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;
}