Hi,
I'm writing a program that will conditionally take actions based on certain options set when it is called from the command line.
I've decided to store the state of the options in a structure defined as:
Code:
/* Structure to store options */
struct options {
int found;
int overwrite;
int checkfornp;
int stripnp;
};
The idea is to pass the structure to any function that expects it and have the function be able to do things like:
Code:
if(opts.checkfornp) {
/* do checking code */
}
I have a function that I call near the beginning of main() that gets the options, if any, from the first argument sent to the program:
Code:
int OptCheck(char *ops, struct options *opts) {
/*checking for options */
int i, showhelp=0;
char o;
if( *ops == '-' ) {
opts->found=1;
for(i=1;(o=*(ops+i)) !='\0';i++) {
switch(o) {
case 'o':
opts->overwrite=1; /* overwrite source file enabled */
break;
case 'p':
opts->checkfornp=1; /* printable characters check requested */
break;
case 's':
opts->stripnp=1; /* strip non-printable characters requested */
break;
default :
printf("Unknown option: %c.\n",o);
showhelp=1;
break;
}
}
}
return showhelp;
}
I'm wondering, is there a more efficient way to do this?
I don't mean "easier", I mean more efficient.
Also I'm trying not to include anything except stdio.h in the program.
Thanks in advance for any thoughts.