我正在尝试在C++程序中使用getopt来解析命令行参数.参数是-d xxx,-s xxx和 - ?.我无法捕获 - ?参数,我想打印标准用法消息.
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
}
Run Code Online (Sandbox Code Playgroud)
尽我所能,我似乎无法抓住' - ?' 选项本身.是否有一个特殊的技巧来捕捉'?' 在其自己的?我是否为getopt提供了正确的模式(即'?d:s:')目前,c被设置为'?' 无论何时提供无效选项,即使'?' 在命令行中未提供.
多谢你们.