Reputation: 4733
i have a program that needs command line arguments in the form :
./my_program -m256M -tm -t some_other_file
The "some_other_file" argument is not bound to -t (-t it's just another functionality) so i can't take it as the optarg of any of the flags, neither can i assume it is the last argument in the list.
How can i do this?
Thanks
Upvotes: 1
Views: 224
Reputation: 11804
getopt(_long) permutes the arguments in argv in such a way, that when there are no arguments it understands left (when it returns -1) all the parsed arguments are before the unparsed ones. So you can use the global variable optind, which getopt sets to the index of the first argument in argv, which it did not parse in order to find any additional arguments to your program. Supposing that except the arguments known by getopt there is one such some_other_file, the pseudocode would be:
while ((ret = getopt_long(argc, argv, ...)) != -1) {
/* do something with ret */
}
if (optind >= argc) {
/* error, no some_other_file */
} else {
file_str = argv[optind];
/* do something else */
}
This method can be extended to an arbitrary number of no-hyphen arguments, which are guaranteed to be all left in argv in order they were passed to the program, and all of them after any arguments understood by getopt, so a simple loop from optind to argc-1 can be used to list these unparsed arguments.
Upvotes: 4
Reputation: 12534
Is that what you want?
int main(int argc, char* argv[]){
//...
int i=1;
for(; i<argc; i++){
if(argv[i][0] != '-'){
printf("%s\n", argv[i]);
//break; //if you dont want all arguments that don't start with - or --
}
}
//...
return 0;
}
$ gcc dsad.c && ./a.out -m256M -tm -t some_other_file more_file
some_other_file
more_file
$ gcc dsad.c && ./a.out -m256M -tm -t
$ gcc dsad.c && ./a.out -m256M -tm -t some_other_file --another
some_other_file
Upvotes: 2