Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using getopt_long (C++) how do I code up a long & short option to both require arguments?

#include <iostream>
#include <getopt.h>

#define no_argument 0
#define required_argument 1 
#define optional_argument 2


int main(int argc, char * argv[])
{
  std::cout << "Hello" << std::endl;

  const struct option longopts[] =
  {
    {"version",   no_argument,        0, 'v'},
    {"help",      no_argument,        0, 'h'},
    {"stuff",     required_argument,  0, 's'},
    {0,0,0,0},
  };

  int index;
  int iarg=0;

  //turn off getopt error message
  opterr=1; 

  while(iarg != -1)
  {
    iarg = getopt_long(argc, argv, "svh", longopts, &index);

    switch (iarg)
    {
      case 'h':
        std::cout << "You hit help" << std::endl;
        break;

      case 'v':
        std::cout << "You hit version" << std::endl;
        break;

      case 's':
        std::cout << "You hit stuff" << std::endl;
        break;
    }
  }

  std::cout << "GoodBye!" << std::endl;

  return 0; 
}

Output:

./a.out -s
Hello
You hit stuff
GoodBye!

Output:

./a.out --stuff
Hello
./a.out: option `--stuff' requires an argument
GoodBye!

Conflict needing resolved: Both -s & --s should say: ./a.out: option `--stuff' requires an argument when used without proceeding arguments following the command. But only --stuff does? Does anyone know what I'm missing here?

Desired result:

./a.out -s
     Hello
     ./a.out: option `--stuff' requires an argument
      GoodBye!

./a.out --stuff
     Hello
     ./a.out: option `--stuff' requires an argument
     GoodBye!
like image 528
stackoverflow Avatar asked Dec 17 '22 05:12

stackoverflow


1 Answers

Your getopt_long call specifies "svh" for the short options when it should say "s:vh". The colon tells getopt to require an argument for "s".

like image 200
Kyle Jones Avatar answered Dec 18 '22 20:12

Kyle Jones