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

标签 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;
 }

最佳答案

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

#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() 循环而不处理文件名参数。

关于c - getopt_long() 和不是标志的参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3679994/

相关文章:

c - C中循环内的 sleep 函数有问题

c - 保存和读取二进制列表?

c - 在 C 命令行程序中处理 `argv`

c - 使用 getopt 获取 optarg

c++ - 为什么 const 值在 C++ 中不能更改,而在 C 中却不能更改?

c - 如何计算C中两个矩阵之间的距离

c - C程序中的参数类型区分

Colortext 程序无法正确着色

linux - getopt 不能像在 Linux 中那样在 AIX 上运行

c - getopt() 中的 optarg 始终为 null