title: Getopt optional arguments?
body: <p>I have a program where you enter an option
      <code>-d</code>
  and then whether or not you supply a non-optional argument after the option, do something.<br>
  Heres my code:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;getopt.h&gt;
  #include &lt;stdlib.h&gt;
  
  #define OPT_LIST "d::" 
  
  int main (int argc, char *argv[])
  {
      int c;
      char string[] = "blah";
  
      while ((c = getopt (argc, argv, OPT_LIST)) != -1)
      {
          switch (c)
          {
              case 'd':
                      printf("%s\n", optarg);
                      break;
  
              case '?':
                  fprintf(stderr, "invalid option\n");
                  exit(EXIT_FAILURE);
          }   
      }
  }
  </code></pre>
  
  <p>So if you enter a non-optional argument after the option, it prints the argument. But I want it to print out the char "string" if the user doesn't supply a non-optional argument (this is why I put the double colon in the
      OPT_LIST). But I'm not sure how to do this so any help would be greatly appreciated.</p>
  
  <p>Heres what happens when I run the program:</p>
  
  <pre><code>user:desktop shaun$ ./arg -d hello
  hello
  user:desktop shaun$ ./arg -d 
  ./arg: option requires an argument -- d
  invalid option
  </code></pre>
  
  <p>I'm running a Mac with OS X using C language.</p>