使用 getopt_long (C++) 如何编写 long & 代码?两个都需要参数的短选项?
#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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您的 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".
我很确定您需要将“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,".