使用 getopt_long (C++) 如何编写 long & 代码?两个都需要参数的短选项?

发布于 2024-12-26 02:07:05 字数 1608 浏览 0 评论 0原文

#include <iostream>
#include <getopt.h>

#define no_argument 0
#define required_argument 1 
#define optional_argument 2


int main(int argc, char * argv[])
{
  std::cout << "Hello" << std::endl;

  const struct option longopts[] =
  {
    {"version",   no_argument,        0, 'v'},
    {"help",      no_argument,        0, 'h'},
    {"stuff",     required_argument,  0, 's'},
    {0,0,0,0},
  };

  int index;
  int iarg=0;

  //turn off getopt error message
  opterr=1; 

  while(iarg != -1)
  {
    iarg = getopt_long(argc, argv, "svh", longopts, &index);

    switch (iarg)
    {
      case 'h':
        std::cout << "You hit help" << std::endl;
        break;

      case 'v':
        std::cout << "You hit version" << std::endl;
        break;

      case 's':
        std::cout << "You hit stuff" << std::endl;
        break;
    }
  }

  std::cout << "GoodBye!" << std::endl;

  return 0; 
}

输出:

./a.out -s
Hello
You hit stuff
GoodBye!

输出:

./a.out --stuff
Hello
./a.out: option `--stuff' requires an argument
GoodBye!

需要解决的冲突: -s & --s 应该说:./a.out:选项“--stuff”在使用时需要参数,而命令后面没有继续参数。但只有--stuff 可以吗?有谁知道我在这里缺少什么?

期望的结果:

./a.out -s
     Hello
     ./a.out: option `--stuff' requires an argument
      GoodBye!

./a.out --stuff
     Hello
     ./a.out: option `--stuff' requires an argument
     GoodBye!
#include <iostream>
#include <getopt.h>

#define no_argument 0
#define required_argument 1 
#define optional_argument 2


int main(int argc, char * argv[])
{
  std::cout << "Hello" << std::endl;

  const struct option longopts[] =
  {
    {"version",   no_argument,        0, 'v'},
    {"help",      no_argument,        0, 'h'},
    {"stuff",     required_argument,  0, 's'},
    {0,0,0,0},
  };

  int index;
  int iarg=0;

  //turn off getopt error message
  opterr=1; 

  while(iarg != -1)
  {
    iarg = getopt_long(argc, argv, "svh", longopts, &index);

    switch (iarg)
    {
      case 'h':
        std::cout << "You hit help" << std::endl;
        break;

      case 'v':
        std::cout << "You hit version" << std::endl;
        break;

      case 's':
        std::cout << "You hit stuff" << std::endl;
        break;
    }
  }

  std::cout << "GoodBye!" << std::endl;

  return 0; 
}

Output:

./a.out -s
Hello
You hit stuff
GoodBye!

Output:

./a.out --stuff
Hello
./a.out: option `--stuff' requires an argument
GoodBye!

Conflict needing resolved: Both -s & --s should say: ./a.out: option `--stuff' requires an argument when used without proceeding arguments following the command. But only --stuff does? Does anyone know what I'm missing here?

Desired result:

./a.out -s
     Hello
     ./a.out: option `--stuff' requires an argument
      GoodBye!

./a.out --stuff
     Hello
     ./a.out: option `--stuff' requires an argument
     GoodBye!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

欲拥i 2025-01-02 02:07:05

当您的 getopt_long 调用指定“svh”作为短选项时,它应该显示“s:vh”。冒号告诉 getopt 需要“s”参数。

Your getopt_long call specifies "svh" for the short options when it should say "s:vh". The colon tells getopt to require an argument for "s".

云仙小弟 2025-01-02 02:07:05

我很确定您需要将“svh”替换为“s:vh”。根据 getopt_long 的 Linux 手册页,“optstring 是包含合法选项字符的字符串。如果这样的字符后面跟着冒号,则该选项需要一个参数”。

I'm pretty sure you need to replace "svh" with "s:vh". Per the Linux man page for getopt_long "optstring is a string containing the legitimate option characters. If such a character is followed by a colon, the option requires an argument,".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文