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;
}
klefevre
  • 8,595
  • 7
  • 42
  • 71

1 Answers1

0

Try using quotes:

./a.out -t 42 -n "toto titi tata" -a address
alk
  • 69,737
  • 10
  • 105
  • 255