在C,getopt_long不解析可选参数的命令行参数的参数。

当我运行程序时,可选的参数不被识别像下面运行该示例。

$ ./respond --praise John
Kudos to John
$ ./respond --blame John
You suck !
$ ./respond --blame
You suck !

下面是测试代码。

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

int main(int argc, char ** argv )
{
    int getopt_ret, option_index;
    static struct option long_options[] = {
               {"praise",  required_argument, 0, 'p'},
               {"blame",  optional_argument, 0, 'b'},
               {0, 0, 0, 0}       };
    while (1) {
        getopt_ret = getopt_long( argc, argv, "p:b::",
                                  long_options,  &option_index);
        if (getopt_ret == -1) break;

        switch(getopt_ret)
        {
            case 0: break;
            case 'p':
                printf("Kudos to %s\n", optarg); break;
            case 'b':
                printf("You suck ");
                if (optarg)
                    printf (", %s!\n", optarg);
                else
                    printf ("!\n", optarg);
                break;
            case '?':
                printf("Unknown option\n"); break;
        }
    } 
    return 0;
}
有帮助吗?

解决方案

虽然glibc的文档或getopt的手册页中没有提及,可选参数长式命令行参数需要“等号”(=)。空间从参数分离可选参数不工作。

与测试代码运行的示例:

$ ./respond --praise John
Kudos to John
$ ./respond --praise=John
Kudos to John
$ ./respond --blame John
You suck !
$ ./respond --blame=John
You suck , John!

其他提示

在男子页肯定不会记录非常好,但源代码可以帮助一点点。

简而言之:你应该这样做下面的(虽然这可能有点过分迂腐):

if(   !optarg
   && optind < argc // make sure optind is valid
   && NULL != argv[optind] // make sure it's not a null string
   && '\0' != argv[optind][0] // ... or an empty string
   && '-' != argv[optind][0] // ... or another option
  ) {
  // update optind so the next getopt_long invocation skips argv[optind]
  my_optarg = argv[optind++];
}
/* ... */
  

从前面的评论中_getopt_internal:

     

...

     

如果getopt找到另一个选项字符,它返回该字符,   的更新optind nextchar以便下一次调用getopt可以   恢复使用下列选项字符或ARGV元件的扫描。

     

如果不存在更多的选择的字符,getopt返回-1。   然后optind处于第一ARGV元件的ARGV索引   这是不是一种选择。 (该ARGV-元件已经置换   使得那些不选择现在最后到来。)<-- a note from me: if the 3rd argument to getopt_long starts with a dash, argv will not be permuted

     

...

     

如果在一个OPTSTRING炭后面跟着冒号,这意味着它想要的精氨酸,   所以在同样ARGV元件下面的文本,或以下的文本   ARGV元素,在optarg返回。两个冒号意味着一个选项,   希望有一个可选的ARG;如果在当前ARGV元素的文字,   它是在optarg返回,,否则optarg设为零

     

...

...但你必须做的字里行间读点书。下面你想要做什么:

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

int main(int argc, char* argv[] ) {
  int getopt_ret;
  int option_index;
  static struct option long_options[] = {
      {"praise",  required_argument, 0, 'p'}
    , {"blame",  optional_argument, 0, 'b'}
    , {0, 0, 0, 0}
  };

  while( -1 != ( getopt_ret = getopt_long(  argc
                                          , argv
                                          , "p:b::"
                                          , long_options
                                          , &option_index) ) ) {
    const char *tmp_optarg = optarg;
    switch( getopt_ret ) {
      case 0: break;
      case 1:
        // handle non-option arguments here if you put a `-`
        // at the beginning of getopt_long's 3rd argument
        break;
      case 'p':
        printf("Kudos to %s\n", optarg); break;
      case 'b':
        if(   !optarg
           && NULL != argv[optindex]
           && '-' != argv[optindex][0] ) {
          // This is what makes it work; if `optarg` isn't set
          // and argv[optindex] doesn't look like another option,
          // then assume it's our parameter and overtly modify optindex
          // to compensate.
          //
          // I'm not terribly fond of how this is done in the getopt
          // API, but if you look at the man page it documents the
          // existence of `optarg`, `optindex`, etc, and they're
          // not marked const -- implying they expect and intend you
          // to modify them if needed.
          tmp_optarg = argv[optindex++];
        }
        printf( "You suck" );
        if (tmp_optarg) {
          printf (", %s!\n", tmp_optarg);
        } else {
          printf ("!\n");
        }
        break;
      case '?':
        printf("Unknown option\n");
        break;
      default:
        printf( "Unknown: getopt_ret == %d\n", getopt_ret );
        break;
    }
  }
  return 0;
}

我也遇到了同样的问题,来到了这里。然后,我意识到这一点。 你没有太多的“optional_argument”的使用情况。如果需要一个选项,从程序逻辑检查,如果一个选项是可选的,那么你不需要做任何事情,因为getopt的级别的所有选项都是可选的,他们是不是强制性的,所以有“optional_argument”没有用的情况。希望这有助于。

PS:对于上面的例子,我认为正确的选项是 --praise --praise名 “姓名” --blame --blame名 “姓名”

如果你写旁边的参数的变量没有空格字符既不等于也适用。例如:

$ ./respond --blameJohn
You suck John!
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top