tags:

views:

52

answers:

2

My argument is like this

./a.out -i file1 file2 file3

How can I utilize getopt() to get 3 (or more ) input files? I'm doing something like this:

while ((opt = getopt(argc, argv, "i:xyz.."))!= -1){
  case 'i':
     input = optarg; 
     break;
  ...
}

I get jusst the file1, how to get file2, file 3 ??

Thanks in advance

+1  A: 

If you must, you could start at argv[optind] and increment optind yourself. However, I would recommend against this since I consider that syntax to be poor form. (How would you know when you've reached the end of the list? What if someone has a file named with a - as the first character?)

I think that it would be better yet to change your syntax to either:

/a.out -i file1 -i file2 -i file3

Or to treat the list of files as positional parameters:

/a.out file1 file2 file3
jamesdlin
Thanks jamesdlin I will look at [optind] as I have quite many arguments other than -i
iKid
+1  A: 

Note that glibc's nonconformant argument permutation extension will break any attempt to use multiple arguments to -i in this manner. And on non-GNU systems, the "second argument to -i" will be interpreted as the first non-option argument, halting any further option parsing. With these issues in mind, I would drop getopt and write your own command line parser if you want to use this syntax, since it's not a syntax supported by getopt.

R..