getopt - parsing string as argument
I'm trying to write a program to take a string from the UNIX command line, and perform some operations on it depending on command line switches. The format is ./program -(options) (arguments) string, and there are two switches -a, and -b, which takes an integer argument. I'm using getopt in a while loop with a switch statement, but have no idea how to read the string from the command line. So far I have:
Code:
int main(int argc,char *argv[]){
char sw; //stores switch
char string[100]; //stores string
int n; //stores integer argument for switch b
opterr=0;
while((sw=getopt(argc,argv,"ab:"))!=-1){
switch(sw){
case 'a': //performs a string operation
return -1;
case 'b': //performs a different string operation also using the argument for switch -b
return -1;
case '?': printf("Invalid switch entered, options are -a or -b");
return -1;
}
}
}
for me to be able to perform the string operations I need to somehow read the string, and the argument for b into the variables string and n - I'm familiar with sscanf and fgets, but everything I tried ended up in an error.
Can someone tell me how it's done?
Thanks