Getopt shift optarg
c, getopt
Solution
`bar` is not an option argument in the eyes of `getopt`. Rather, GNU `getopt` rearranges the positional arguments so that at the end of the processing, you have `argv[3]` being "hello" and `argv[4]` being "bar". Basically, when you're done getopting, you still have positional arguments `[optind, argc)` to process:
int main(int argc, char * argv[])
{
{
int c;
while ((c = getopt(argc, argv, ...)) != -1) { /* ... */ }
}
for (int i = optind; i != argc; ++i)
{
// have positional argument argv[i]
}
}
Problem
I need to call my program like this: ``` ./program hello -r foo bar ``` I take hello out of argv[1], but i am having trouble with value bar, also should i change "r:" to something else? ``` while((c = getopt(argc, argv, "r:")) != -1){ switch(i){ ... case 'r': var_foo = optarg; //shell like argument shift here? var_bar = optarg; break; ...} ``` I know I could do this with passing through argv, but is there a way to do it with getopt similar way as in bash? Thanks.