我想在C++程序中使用getopt来解析命令行参数。 参数是-d xxx,-s xxx和 - ?. 我无法捕获 - ?参数,我想打印一个标准的使用信息。getopt - capture'?'
while ((c = getopt (argc, argv, "?d:s:")) != -1) {
switch (c) {
case 'd':
...do stuff
break;
case 's':
... do stuff
break;
case '?':
// From example on GNU page, seems to capture -d, -s when no args provided.
// Gets here when -d or -s provided, but no arguments for these options.
default:
// shut down
}
尝试,因为我可能,我似乎无法捕捉“ - ?”选项。有没有特别的技巧来抓住'?'在其自己的?我是否提供了getopt的正确模式(即'?d:s:') 此刻,c正被设置为'?'只要提供了无效的选项,即使'?'不在命令行提供。
谢谢你们。
“getopt”使用'?'作为特殊值的事实是给你一个免费的东西,你不应该用它来显示帮助。这就是'-h'和'--help'的用处。遵守这些惯例而不是做自己的事情是值得的。 –