getopt_long()和不是标志的参数?

时间:2010-09-09 19:40:42

标签: c command-line-arguments getopt

我第一次尝试使用getopt_long()函数只是我遇到了不是标志的参数问题。例如,在我的代码中,当给出一个未知参数时,我想将它用作输入文件。当我只使用文件名运行它时,它不会被打印,如果我首先使用一个标志,任何标志,那么我可以打印它。

我该如何解决这个问题?

 #include <stdio.h>
 #include <getopt.h>

 static struct option long_options[] = {
  {"help",  no_argument,       0, 'h'},
  {"input",  required_argument, 0, 'i'},
  {"output",  required_argument, 0, 'o'},
  {"algorithm",  required_argument, 0, 'a'},
  {0, 0, 0, 0}
 };

 int main(int argc, char *argv[]) {
  int c;
  int option_index = 0;

  while(42) {
   c = getopt_long(argc, argv, "hi:o:a:", long_options, 
     &option_index);
   if(c == -1)
    break;

   switch(c) {
    case 'h': /* --help */
     printf("--help flag\n");
     break;
    case 'i': /* --input */
     printf("--input flag\n");
     break;
    case 'o': /* --output */
     printf("--output flag\n");
     break;
    case 'a': /* --algorithm */
     printf("--algorithm flag \n");
     break;
    default: /* ??? */
     fprintf(stderr, "Invalid option");
     return 1;
   }

   if(optind < argc) {
    printf("other arguments: ");

    while(optind < argc) {
     printf ("%s ", argv[optind]);
     optind++;
    }

    printf("\n");
   }
  }

  return 0;
 }

2 个答案:

答案 0 :(得分:5)

循环应该只包含开关。在单独的(非嵌套的)循环中处理剩余参数:

#include <stdio.h>
#include <getopt.h>

static struct option long_options[] =
{
    {"help",  no_argument,       0, 'h'},
    {"input",  required_argument, 0, 'i'},
    {"output",  required_argument, 0, 'o'},
    {"algorithm",  required_argument, 0, 'a'},
    {0, 0, 0, 0}
};

int main(int argc, char *argv[])
{
    int opt;
    int option_index = 0;
    int i;

    while ((opt = getopt_long(argc, argv, "hi:o:a:", long_options, &option_index)) != -1)
    {
        switch(opt)
        {
            case 'h': /* --help */
                printf("--help flag\n");
                break;
            case 'i': /* --input */
                printf("--input flag (%s)\n", optarg);
                break;
            case 'o': /* --output */
                printf("--output flag (%s)\n", optarg);
                break;
            case 'a': /* --algorithm */
                printf("--algorithm flag (%s)\n", optarg);
                break;
            default: /* ??? */
                fprintf(stderr, "Invalid option %c\n", opt);
                return 1;
        }
    }

    for (i = optind; i < argc; i++)
        printf("Process: %s\n", argv[i]);

    return 0;
}

有一种方法可以让GNU getopt()getopt_long()返回文件名参数,就好像它们是带有'letter'的选项^ A'\ 1';使用“-”作为短选项字符串的第一个字符,并在切换中捕获“\1”; optarg的值是文件的名称。

#include <stdio.h>
#include <getopt.h>

static struct option long_options[] =
{
    {"help",  no_argument,       0, 'h'},
    {"input",  required_argument, 0, 'i'},
    {"output",  required_argument, 0, 'o'},
    {"algorithm",  required_argument, 0, 'a'},
    {0, 0, 0, 0}
};

int main(int argc, char *argv[])
{
    int opt;
    int option_index = 0;
    int i;

    while ((opt = getopt_long(argc, argv, "-hi:o:a:", long_options, &option_index)) != -1)
    {
        switch(opt)
        {
            case 'h': /* --help */
                printf("--help flag\n");
                break;
            case 'i': /* --input */
                printf("--input flag (%s)\n", optarg);
                break;
            case 'o': /* --output */
                printf("--output flag (%s)\n", optarg);
                break;
            case 'a': /* --algorithm */
                printf("--algorithm flag (%s)\n", optarg);
                break;
            case '\1':
                printf("File: %s\n", optarg);
                break;
            default: /* ??? */
                fprintf(stderr, "Invalid option %c\n", opt);
                return 1;
        }
    }

    for (i = optind; i < argc; i++)
        printf("Process: %s\n", argv[i]);

    return 0;
}

但是,如果你的cantankerous用户输入类型,你需要在参数处理循环之后进行循环:

program -- abc def

'--'终止while()循环而不处理文件名参数。

答案 1 :(得分:3)

它不起作用,因为当while返回-1时(这表示没有更多选项),你会突破外部getopt_long循环。

您需要将if (optind < argc)块移出外部while循环;反正它不属于那里。如果您将外部while循环写为:

,这可能会更清楚
while ((c = getopt_long(...)) != -1)
{
    switch (c)
    {
        /* Deal with flags. */
    }
}