我正尝试在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
}尽管我可能会尝试,我似乎不能捕捉到'-?‘选项本身。有什么特别的窍门吗?单打独斗?我是否提供了正确的模式来获取the (即'?d:s:')目前,c设置为'?‘只要提供了无效的选项,即使'?‘在命令行中未提供。
谢谢你们。
发布于 2012-11-03 19:07:14
getopt使用'?'作为特殊值,表示缺少选项值或未知选项。所以我不认为有任何方法可以使用getopt来处理'-?‘。
我建议使用'-h‘作为帮助消息。这是一种常见的约定。
https://stackoverflow.com/questions/13208553
复制相似问题