小编ret*_*val的帖子

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

我试图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': …
Run Code Online (Sandbox Code Playgroud)

c getopt command-line-arguments

0
推荐指数
1
解决办法
2209
查看次数

标签 统计

c ×1

command-line-arguments ×1

getopt ×1