I'm trying to figure out how to parse options such as -h out of the arguments passed on the command line when the program is called. Here is my code so far. I compiled it with gcc -Wall -o options options.c and got no warnings or errors. Although, when I run it, it doesn't do anything. I have tried ./options -h as well as ./options -help and ./options help and it seems to find nothing. Oh and I'm running Ubuntu linux on an x86.
/**************************************************************************
*
* Purpose: To practice programming options
*
*************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[])
{
int count;
char * strhold; // Used to hold option values
int len = 0; // Used to hold arguments' string lengths
for(count=1; count == argc; count++)
{
strcpy(strhold, argv[count]);
len = strlen(strhold);
if((strhold[0] == '-') && (strhold[1] == 'h'))
{
printf("\nPurpose: To practice programming options\n");
exit(0);
} else if((strhold[0] == '-') && (strhold[1] != 'h'))
{
printf("Error: Invalid option - %s\n", strhold);
exit(1);
}
}
return 0;
}
Anybody know what's going on?
Thanks
diode