/* This program is distributed with version 3.0 of the Gopt library. It's purpose is to demonstrate the function of the library. It is in the public domain. Use it at your own risk. Gopt is distributed under the GNU GPL, see: http://gopt.sourceforge.net/ */ #include #include #include "gopt.h" int main(int argc, char *argv[]){ char input,shortopt,longopt[64],*strend; const char *ptr; unsigned int i; gopt_t goptreturn; void *options; #ifdef GOPT_PASSMALLOC options=gopts("abc",&argc,&argv,&malloc); #else options=gopts("abc",&argc,&argv); #endif if(options==NULL){ fprintf(stderr,"gopt-ex: out of memory\n"); exit(EXIT_FAILURE); } printf("For the purposes of this demonstration, the short options -a -b and -c take\n"); printf("arguments, and all others don't. Any long option may take an argument.\n"); printf("\nAre we looking for a long (l) option, short (s) or either (e)?\n"); printf("Enter l, s, or e:"); do scanf(" %c",&input); while(input!='l'&&input!='s'&&input!='e'&&input!='L'&&input!='S'&&input!='E'); if(input=='l'||input=='e'||input=='L'||input=='E'){ printf("Enter the long option:"); scanf(" "); if(NULL==fgets(longopt,64,stdin)) if(NULL==fgets(longopt,64,stdin)){ /* try twice */ fprintf(stderr,"gopt-ex: could not read input\n"); exit(EXIT_FAILURE); } if(NULL!=(strend=strchr(longopt,'\n'))) *strend=0; }else longopt[0]=0; if(input=='s'||input=='e'||input=='S'||input=='E'){ printf("Enter the short option:"); if(!scanf(" %c",&shortopt)) if(!scanf(" %c",&shortopt)){ /* try twice */ fprintf(stderr,"gopt-ex: could not read input\n"); exit(EXIT_FAILURE); } }else shortopt=0; goptreturn=gopt(options,shortopt,longopt,&ptr); switch(goptreturn){ case GOPT_NOTPRESENT: printf("\nThe option was not present.\n\n"); break; case GOPT_PRESENT_NOARG: printf("\nThe option was present without an argument.\n\n"); break; case GOPT_PRESENT_WITHARG: printf("\nThe option was present, it's argument was %s.\n\n",ptr); } free(options); printf("The command was '%s'.\n",argv[0]); printf("Excluding options and their arguments, there were %d command line arguments",argc-1); if(argc>1){ printf(", which were:\n"); for(i=1;i