[英]getopt always returns 1
I want to use getopt
to get the argument list of my console tool.我想使用
getopt
来获取我的控制台工具的参数列表。 When I call my tool like below getopt
returns always 1
and doesn't mactch any switch/case
.当我像下面这样调用我的工具时,
getopt
总是返回1
并且不匹配任何switch/case
。
Am I doing something wrong?难道我做错了什么?
mytool -f farg -d darg
int
main(int argc, char** argv) {
int c;
while((c = getopt(argc, argv, "f:d:h") != -1)) {
switch(c) {
case'f':
break;
default:
break;
}
}
while((c = getopt(argc, argv, "f:d:h") != -1))
It works like它的工作原理类似于
c = (getopt(argc, argv, "f:d:h") != -1)
Well, that is 1 always because the result of the comparison is stored to c
.嗯,这总是 1 因为比较的结果存储到
c
。 In your case the getopt
does not return -1
.在您的情况下,
getopt
不会返回-1
。 If it returns -1
then c
would be 0
.如果它返回
-1
那么c
将是0
。 The fix is修复是
while((c = getopt(argc, argv, "f:d:h")) != -1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.