I have a program that needs to be called using:
program parameter1 parameter2 -x1 -y
but I feel that it should work if I do:
program -x1 -y parameter1 parameter2
or combinations thereof. How do I get parameter1 and parameter2 without a horrid dirty hack? At the moment I have a
while ((c = getopt (argc, argv, "x:y")) != -1){
/* do stuff */
}
loop for the optional arguments, but what about the others? It would seem wrong to just look at argv[1] and argv[2] because they could be anywhere.
I’m sure there is an established solution to this.
After you exit the
whileloop,optindpoints to the first non-option argument. Take a look at thegetopt(3)man page:So your first non-option argument is
argv[optind]and so forth.getoptwill permute the arguments so that this will still be true even for your first example, where the option arguments are on the command line after the non-option arguments.