Win7
赛格温
GCC 4.8.3
getopt_long 不返回某些输入命令行选项。在将代码投入生产之前,我试图找出 getopt_long 的行为。对于正确形成的选项,它似乎工作正常。对于格式不正确的选项,getopt_long 有时无法返回不正确的值。
这是命令行、输入、输出和代码:
COMMAND LINE
-s -t -u -v -h --solution --statistics --unique --verbose --statistics="key, node, part, bins" -t "keys,node,part,bin" -s (max min) --solution=(max,min) -s max /cygdrive/c/home/skidmarks/Projects/MPHASH/old/mphash.files/SYM/ADAWORDS.SYM
输入
argv[ 1]-s
argv[ 2]-t
argv[ 3]-u
argv[ 4]-v
argv[ 5]-h
argv[ 6]--solution
argv[ 7]--statistics
argv[ 8]--unique
argv[ 9]--verbose
argv[10]--statistics=key, node, part, bins
argv[11]-t
argv[12]keys,node,part,bin
argv[13]-s
argv[14](max
argv[15]min)
argv[16]--solution=(max,min)
argv[17]-s
argv[18]max
argv[19]/cygdrive/c/home/skidmarks/Projects/MPHASH/old/mphash.files/SYM/ADAWORDS.SYM
输出
's' 0x73
't' 0x74
'u' 0x75
'v' 0x76
'h' 0x68
's' 0x73
't' 0x74
'' 0x00
'' 0x00
't' 0x74 key, node, part, bins
't' 0x74
's' 0x73
's' 0x73 (max,min)
's' 0x73
missing argv[12], argv[14-15], argv[18]
代码
bool FrontEnd::getopt(int argc, char** argv) {
int option_index;
int printUniqueFlag = 0;
int optionFlags = 0;
char short_Option[] = "bhs::uvt::\0";
static struct option long_options[] =
{ {"solution", optional_argument, 0, 's'} //!< "max", "min"
, {"statistics", optional_argument, 0, 't'} //!< ("part", "node", "key", "bins")
, {"unique", no_argument, &printUniqueFlag, 0x01 }
, {"verbose", no_argument, &optionFlags, 0x7F }
, {0, 0, 0, 0 }
};
int c;
inputFlag = true;
for(int ndx = 0; ndx < argc; ndx++) {
cout << "argv[" << setw(2)<< ndx << "]" << argv[ndx] << endl;
}
while ((c = ::getopt_long(argc, argv, short_Option, long_options, &option_index)) != -1) {
cout << '\'' << (char)c << "\' 0x" << setw(2) << setfill('0') << hex << c << " " ;
if (optarg) cout << optarg;
cout << endl;
}
return inputFlag;
}; // int FrontEnd::getopt(int argc, char** argv)