【问题标题】:getopt_long() and parameters that aren't flags?getopt_long() 和不是标志的参数?
【发布时间】:2011-04-10 10:46:17
【问题描述】:

我第一次尝试使用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;
 }

【问题讨论】:

    标签: c command-line-arguments getopt


    【解决方案1】:

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

    #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() 返回文件名参数,就好像它们是带有“字母”的选项 ^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;
    }
    

    但是,您确实需要在参数处理循环之后进行循环,以防您的脾气暴躁的用户类型:

    program -- abc def
    

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

    【讨论】:

      【解决方案2】:

      它不起作用,因为当getopt_long 返回-1(表示没有更多选项)时,您跳出了外部while 循环。

      您需要将if (optind &lt; argc) 块移出外部while 循环;无论如何它不属于那里。如果您将外部 while 循环编写为:

      while ((c = getopt_long(...)) != -1)
      {
          switch (c)
          {
              /* Deal with flags. */
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-03-28
        • 1970-01-01
        • 2015-10-14
        • 2016-01-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多