1

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)
Community
  • 1
  • 1
lostbits
  • 928
  • 1
  • 9
  • 23
  • If you have an optional argument, you must specify it in the same word with the option, i.e. `-tkeys,node,part,bin` (no space) or `--statistics="keys,node,part,bin"`. Otherwise what you want to be optional arguments are in fact positional parameters. – n. m. could be an AI Dec 10 '14 at 23:00
  • I understand this. The question is why aren't badly formatted options returned? Well formed options (seem to be) always returned. Badly formed options with arguments are sometimes return, sometimes not. I expected that all command line inputs would have been returned in some format or another (or getopt_long would have thrown its hands up and said, "I quit"). The issue is that if a badly formatted argument (or other) is not returned, then the command line process will not abort. A bad thing. – lostbits Dec 10 '14 at 23:53
  • 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. could be an AI Dec 11 '14 at 03:13
  • thunk (hand hitting head). Thanks! – lostbits Dec 11 '14 at 18:24

1 Answers1

0

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.

Armali
  • 18,255
  • 14
  • 57
  • 171