I was learning how to accept command line arguments using getopt_long function, I made 2 long options 'filename' (required arg) and 'clear' (no arg) and 2 short args 'a' (with arg) and 'b' (no arg) when i executed:
$ ./a.out -a --filename=test.txt
instead of showing 'a' has no arg it shows the optarg for 'a' is: --filename=text.txt and skips the filename long option Any work around for this?
My code is:
#include <iostream>
#include <getopt.h>
using namespace std;
int main(int argc, char* argv[]){
static struct option long_options[] = {
{"filename",1,0,0},
{"clear",0,0,0},
{NULL,0,0,0}
};
int op,option_index = 0;
string filename;
while((op = getopt_long(argc,argv,"a:b",long_options,&option_index))!=-1){
switch (op){
case 0:
switch(option_index){
case 0:
filename = optarg;
cout<<filename<<endl;
break;
case 1:
cout<<"clear is yes\n";
break;
default:
cout<<"Please enter valid long option\n";
break;
}break;
case 'a':
cout<<"a is set as "<<optarg<<endl;
//cout<<optarg<<endl;
break;
case 'b':
cout<<"b is set"<<endl;
break;
default:
cout<<"Please enter valid Arguments"<<endl;
break;
}
}
cout<<"\n\n";
return 0;
}