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

ret*_*val 0 c getopt command-line-arguments

我试图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;
 }
Run Code Online (Sandbox Code Playgroud)

Jon*_*ler 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;
}
Run Code Online (Sandbox Code Playgroud)

有一种方法可以使用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;
}
Run Code Online (Sandbox Code Playgroud)

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

program -- abc def
Run Code Online (Sandbox Code Playgroud)

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