You can also use optind
. optint
keeps track of the number of options encountered. optind
points to the next index in argv[]
that is encountered by getopt()
Therefore you can lookup in argv
for whether there is a team. But for this to work you should omit the ":" in the optstring as in the next fragment "p:x:y:nc:t:"
or decrement the value of optint before using it in the loop.
this is just a simple function to determine whether the loop must continue.
int
is_team ( const char* team ) {
if ( team == NULL)
return 0;
else if ( team[0] == '-' ) /*new argument*/
return 0;
else
return 1;
}
And this is what you do when you encounter the 'n' option you can also use the colon in the optstring, but then the encounter option is also counted and then i = optind - 1
might also work
case 'n':
{ /*note this scope is significant*/
int i;
for ( i = optind ; is_team(argv[i]); ++i ) {
printf ( "team = %s\n", argv[i]);
//argument = ++*optarg;
}
}
break;
I hope this helps.