title: In C++, how to use only long options with a required argument?
body: <p>In a C++ program, I would like to have a "long-only" option with a required argument. Below is my minimal example using <a href="http://www.gnu.org/software/libc/manual/html_node/Getopt-Long-Option-Example.html#Getopt-Long-Option-Example" rel="nofollow noreferrer">getopt_long()</a>, but it's not working:</p>
  
  <pre><code>#include &lt;getopt.h&gt;
  #include &lt;cstdlib&gt;
  #include &lt;iostream&gt;
  using namespace std;
  
  void help (char ** argv)
  {
    cout &lt;&lt; "`" &lt;&lt; argv[0] &lt;&lt; "` experiments with long options." &lt;&lt; endl;
  }
  
  void parse_args (int argc, char ** argv, int &amp; verbose, int &amp; param)
  {
    int c = 0;
    while (1)
    {
      static struct option long_options[] =
        {
          {"help", no_argument, 0, 'h'},
          {"verbose", required_argument, 0, 'v'},
          {"param", required_argument, 0, 0}
        };
      int option_index = 0;
      c = getopt_long (argc, argv, "hv:",
                       long_options, &amp;option_index);
      cout &lt;&lt; "c=" &lt;&lt; c &lt;&lt; endl;
      if (c == -1)
        break;
      switch (c)
      {
      case 0:
        if (long_options[option_index].flag != 0)
          break;
        printf ("option %s", long_options[option_index].name);
        if (optarg)
          printf (" with arg %s", optarg);
        printf ("\n");
        break;
      case 'h':
        help (argv);
        exit (0);
      case 'v':
        verbose = atoi(optarg);
        break;
      case 'param':
        param = atoi(optarg);
        break;
      case '?':
        abort ();
      default:
        abort ();
      }
    }
  }
  
  int main (int argc, char ** argv)
  {
    int verbose = 0;
    int param = 0;
    parse_args (argc, argv, verbose, param);
    cout &lt;&lt; "verbose=" &lt;&lt; verbose &lt;&lt; " param=" &lt;&lt; param &lt;&lt; endl;
    return EXIT_SUCCESS;
  }
  </code></pre>
  
  <p>I compile it with this command (gcc version 4.1.2 20080704 Red Hat 4.1.2-46):</p>
  
  <pre><code>g++ -Wall test.cpp
  </code></pre>
  
  <p>It tells me this:</p>
  
  <pre><code>test.cpp:44:10: warning: character constant too long for its type
  </code></pre>
  
  <p>And here is the result:</p>
  
  <pre><code>$ ./a.out -v 2 --param 3
  c=118
  c=0
  option param with arg 3
  c=-1
  verbose=2 param=0
  </code></pre>
  
  <p>I tried to make it work on <a href="http://ideone.com/yllaO" rel="nofollow noreferrer">ideone</a> but it doesn't even recognize the option <code>-v</code>.</p>
  
  <p>As indicated by trojanfoe in his <a href="https://stackoverflow.com/a/8169291/597069">comments</a> of another question, it should be possible to use "long-only" options because GNU tar does it. However, GNU tar uses <a href="http://www.gnu.org/software/libc/manual/html_node/Argp.html#Argp" rel="nofollow noreferrer">argp</a> and I have difficulty understanding its <a href="http://git.savannah.gnu.org/cgit/tar.git/tree/src/tar.c#n385" rel="nofollow noreferrer">source code</a>.</p>
  
  <p>Could someone give me a minimal example that works, with GNU <code>getopt_long()</code> or <code>argp()</code>?</p>