Why is getopt_long ignoring some command line options

300 views Asked by At

ref: getopt_long doesnt handle my arguments right

Win7

cygwin

gcc 4.8.3

Some input command line options are not returned by getopt_long. Before I put the code into production, I am trying to find the behavior of getopt_long. For correctly formed options it seems to work fine. For incorrectly formed options getopt_long sometimes fails to return an incorrect value.

Here is the command line, input, output & code:

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

OUTPUT

'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

There are 1 answers

0
Armali On

There are no badly formatted options here. There are just options and positional parameters. getopt doesn't return positional parameters, you process them separately. – n.m.