0

How to retrieve multi args for one option using getopt in libc ?

./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;
}
4

1 回答 1

0

尝试使用引号:

./a.out -t 42 -n "toto titi tata" -a address
于 2012-06-06T15:39:13.207 回答