I have the following C struct long options
static struct option long_options[] = {
{"argument1", required_argument, 0, 0 },
{"argument2", required_argument, 0, 0 },
{"argument3", required_argument, 0, 0 },
{0, 0, 0, 0}
};
And i want to use them in this format when i run the program
./program -argument1=1 -argument2=test -argument3=test1
I don't want ot use the alias of that arguments (if any) for example -a etc. I want to use the full string for that argument like in th example above and take back the value of the argument.
In all examples that i found on the internet they are using the alias of the argument which is only one char
EDIT : Of course the below program is wrong but i want to have in the switch the full argument name not an alias.
while ((opt = getopt_long_only(argc, argv,"", long_options, &long_index )) != -1)
{
switch (opt) {
case "argument1":
printf("\n\n%s",optarg);
break;
}
}
How can i do that?
Thanks
To get at the struct option
corresponding to the matched argument, use the long_index
variable you gave getopt_long()
to index into the long_options
array. The name string is in there to match.
If what you really want is just a symbolic name for every option then you can just set that in the val
field of struct option
. That value is returned from the getopt_long()
call.
enum {
OPT_ARGUMENT1 = 1000,
OPT_ARGUMENT2,
OPT_ARGUMENT3
};
static struct option long_options[] = {
{"argument1", required_argument, 0, OPT_ARGUMENT1 },
{"argument2", required_argument, 0, OPT_ARGUMENT2 },
{"argument3", required_argument, 0, OPT_ARGUMENT3 },
{0, 0, 0, 0}
};
It is better to start the value from beyond the ASCII range so as not to clash with short options (which you don't have in this case) or special characters getopt_long()
returns.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.