如何使用libc中的getopt为一个选项检索多个参数?
./ a.out -t 42 -n toto titi tata -a address
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
int main(int ac, char **av)
{
int opt;
while ((opt = getopt(ac, av, "t:n:a:")) != -1) {
switch (opt) {
case 't':
printf("time = %s\n", optarg);
break;
case 'n':
/* HOW RETRIEVE HERE ALL OTHER NAME ?*/
printf("name = %s\n", optarg);
break;
case 'a':
printf("address = %s\n", optarg);
break;
default:
fprintf(stderr, "Usage : %s [-t time] [-a name1 name2 ...] [-s address]", av[0]);
return EXIT_FAILURE;
}
}
return EXIT_SUCCESS;
}
答案 0 :(得分:0)
尝试使用引号:
./a.out -t 42 -n "toto titi tata" -a address