为什么getopt_long忽略了一些命令行选项

时间:2014-12-10 22:45:48

标签: c c++11 g++4.8

参考:getopt_long doesnt handle my arguments right

Win7的

cygwin的

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

INPUT

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]

CODE

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)

1 个答案:

答案 0 :(得分:0)

此处没有格式错误的选项。只有选项和位置参数。 getopt不返回位置参数,您可以单独处理它们。 - n.m。

相关问题