I'm doing assignment.
If I use getopt_long, I can't use both argument like -vo, but -verbose -open
is ok. I want to use both command -vo ~~
and command -verbose -open ~~
WIll you help me? Thanks you.
int main(int argc, char * argv[])
{
using namespace std;
int c;
char *filename;
extern char *optarg;
extern int optind, optopt, opterr;
bool aflag = false, vflag = false, oflag = false;
int option_index = 0;
static struct option long_options[] = {
{"verbose", 1, 0, 'v'},
{"absolute", 1, 0, 'a'},
{"output", 1, 0, 'o'},
{0, 0, 0, 0}
};
while ((c = getopt_long(argc, argv, "v:a:o:", long_options, &option_index)) != -1)
{
switch(c) {
case 'v':
vflag = true;
printf("v is set\n");
break;
case 'a':
aflag = true;
printf("a is set\n");
break;
case 'o':
oflag = true;
//filename = optarg;
//printf("filename is %s\n", filename);
break;
case ':':
printf("-%c without filename\n", optopt);
break;
case '?':
printf("unknown arg %c\n", optopt);
break;
}
}
}