Skelett eines Programms dass mit Optionen gestartet wird:
// skeleton eines Optionparser / richard@borwinius.de //gcc optionparser.c -o optionparser #include <stdio.h> #include <getopt.h> #include <string.h> #include <stdlib.h> void usage(char *prog) { printf("usage of %s with arguments :\n",prog); printf("%s -a xxx -b yyyy -c -h\nor\n",prog); printf("%s --arg_a xxx --arg_b yyy --arg_c --help\n",prog); return ; } int main (int argc, char** argv) { int i; static char* arg_a = NULL; static char* arg_b = NULL; static char* arg_c = NULL; static char* help = NULL; static struct option long_options[] = { {"arg_a", required_argument, 0,'a'}, {"arg_b", required_argument, 0,'b'}, {"arg_c", no_argument, 0,'c'}, {"help", no_argument, 0,'h'}, {0,0,0,0} }; while ((i = getopt_long(argc,argv,"a:b:ch",long_options,NULL)) != -1) { switch (i) { case 'a': arg_a = strdup(optarg); printf("argument von a: %s\n",arg_a);break; case 'b': arg_b = strdup(optarg); printf("argument von b: %s\n",arg_b);break; case 'c': printf("c ausgewählt: \n");break; case 'h': usage(argv[0]);break; default: usage(argv[0]);break; //fprintf(stderr,"falsche option %c\n",i);exit (1); } } return 0; }