title: Understanding `option long_options[]` when using `getopt_long`
body: <p>I am trying to learn to use <code>getopt_long</code>. From <a href="https://en.wikipedia.org/wiki/Getopt#Example_2_.28using_GNU_extension_getopt_long.29" rel="nofollow noreferrer">wikipedia</a>, I see the code</p>
  <pre><code>#include &lt;stdio.h&gt;     /* for printf */
  #include &lt;stdlib.h&gt;    /* for exit */
  #include &lt;getopt.h&gt;    /* for getopt_long; POSIX standard getopt is in unistd.h */
  int main (int argc, char **argv) {
      int c;
      int digit_optind = 0;
      int aopt = 0, bopt = 0;
      char *copt = 0, *dopt = 0;
      static struct option long_options[] = {
          {&quot;add&quot;, 1, 0, 0},
          {&quot;append&quot;, 0, 0, 0},
          {&quot;delete&quot;, 1, 0, 0},
          {&quot;verbose&quot;, 0, 0, 0},
          {&quot;create&quot;, 1, 0, 'c'},
          {&quot;file&quot;, 1, 0, 0},
          {NULL, 0, NULL, 0}
      };
      int option_index = 0;
      while ((c = getopt_long(argc, argv, &quot;abc:d:012&quot;,
                   long_options, &amp;option_index)) != -1) {
          int this_option_optind = optind ? optind : 1;
          switch (c) {
          case 0:
              printf (&quot;option %s&quot;, long_options[option_index].name);
              if (optarg)
                  printf (&quot; with arg %s&quot;, optarg);
              printf (&quot;\n&quot;);
              break;
          case '0':
          case '1':
          case '2':
              if (digit_optind != 0 &amp;&amp; digit_optind != this_option_optind)
                printf (&quot;digits occur in two different argv-elements.\n&quot;);
              digit_optind = this_option_optind;
              printf (&quot;option %c\n&quot;, c);
              break;
          case 'a':
              printf (&quot;option a\n&quot;);
              aopt = 1;
              break;
          case 'b':
              printf (&quot;option b\n&quot;);
              bopt = 1;
              break;
          case 'c':
              printf (&quot;option c with value '%s'\n&quot;, optarg);
              copt = optarg;
              break;
          case 'd':
              printf (&quot;option d with value '%s'\n&quot;, optarg);
              dopt = optarg;
              break;
          case '?':
              break;
          default:
              printf (&quot;?? getopt returned character code 0%o ??\n&quot;, c);
          }
      }
      if (optind &lt; argc) {
          printf (&quot;non-option ARGV-elements: &quot;);
          while (optind &lt; argc)
              printf (&quot;%s &quot;, argv[optind++]);
          printf (&quot;\n&quot;);
      }
      exit (0);
  }
  </code></pre>
  <p>I don't quite understand the <code>option long_options[]</code> object.</p>
  <p><strong>First column</strong></p>
  <p>I think the first &quot;column&quot; of <code>long_options[]</code> should be the long flag (whatever follows <code>--</code>) the user have used in the command line.</p>
  <p><strong>Second column</strong></p>
  <p>I would have thought the second column should contain only <code>no_argument</code>, <code>required_argument</code>, or <code>optional_argument</code> but instead of that I see 0 and 1.</p>
  <p><strong>Third column</strong></p>
  <p>I don't understand the third column.</p>
  <p><strong>Fourth column and maximum number of flags</strong></p>
  <p>The fourth column is the unique identifier one uses in the switch statement. However, if the unique identifier can only be a single character, then are we limited to all lower case letters (26) + all uppercase letters (26) + the numbers (10) + eventually some special characters for a total of a bit more than 62 different arguments max? Is this a limitation of <code>getopt</code>? If I am mistaken, then how can one indicate more than two characters to identify a flag in the third argument to <code>getopt_long</code> (<code>&quot;&quot;abc:d:012&quot;&quot;</code>)</p>
  <p>I suppose the last row of <code>option long_options[]</code> is for when <code>getopt</code> returns <code>-1</code> and therefore does not really matter as long as it exists.</p>