0

I'd like to get more optargs after arguments, but I'm not sure how to do it. I want to call my program like

./test -a first second -b third

and I can now get only one value after argument -a. When I try to put there two or more, avalue is null.

My code:

   char *avalue = NULL;
   char *bvalue = NULL;
   while ((c = getopt (argc, argv, "a:b:")) != -1)
     switch (c)
       {
      case 'a':
         avalue = optarg;
         break;
      case 'b':
         bvalue = optarg;
         break;
       case '?':
         if (optopt == 'c')
           fprintf (stderr, "Option -%c requires an argument.\n", optopt);
         else if (isprint (optopt))
           fprintf (stderr, "Unknown option `-%c'.\n", optopt);
         else
           fprintf (stderr,
                    "Unknown option character `\\x%x'.\n",
                    optopt);
         return 1;
       default:
         abort ();
       }

   printf ("avalue = %s\nbvalue = %s\n",avalue, bvalue);
4

1 回答 1

1

这种传统格式最多需要每个标志的单个参数。你不能改变它。

但是,根据您的 shell,您可以使用引号将多个标记“分组”为一个参数:

./test -a "first second" -b third

这种分组将发生在您的 shell 中,即在参数被发送到您的程序之前。

于 2013-03-25T10:48:52.953 回答