title: Getopt- Passing string parameter for argument
body: <p>I have a program which takes in multiple command line arguments so I am using getopt. One of my arguments takes in a string as a parameter. Is there anyway to obtain that string through the getopt function or would I have to obtain it through the argv[] array? Also can getopt read args like <code>-file</code> ? All the arguments I have seen till now have only one character such as <code>-a</code></p>
  <p><strong>EDIT</strong></p>
  <p>From the below answers I have written a program to use getopt_long(), but the switch statement only recognizes the argument when I use the character argument and not the long argument. I'm not sure why this happening. On passing the arguments <code>-mf -file sample</code> I do not see the print statements.</p>
  <p><strong>EDIT</strong></p>
  <p>I tried entering the command arguments as <code>--file</code> and then it worked. Is it not possible to do this with just <code>-file</code> ?</p>
  <pre><code>static struct option long_options[] =
  {
      {&quot;mf&quot;, required_argument, NULL, 'a'},
      {&quot;md&quot;, required_argument, NULL, 'b'},
      {&quot;mn&quot;, required_argument, NULL, 'c'},
      {&quot;mw&quot;, required_argument, NULL, 'd'},
      {&quot;lf&quot;, required_argument, NULL, 'e'},
      {&quot;ld&quot;, required_argument, NULL, 'f'},
      {&quot;ln&quot;, required_argument, NULL, 'g'},
      {&quot;lw&quot;, required_argument, NULL, 'h'},
      {&quot;rf&quot;, required_argument, NULL, 'i'},
      {&quot;rd&quot;, required_argument, NULL, 'j'},
      {&quot;rn&quot;, required_argument, NULL, 'k'},
      {&quot;rw&quot;, required_argument, NULL, 'l'},
      {&quot;df&quot;, required_argument, NULL, 'm'},
      {&quot;dd&quot;, required_argument, NULL, 'n'},
      {&quot;dn&quot;, required_argument, NULL, 'o'},
      {&quot;dw&quot;, required_argument, NULL, 'p'},
      {&quot;file&quot;, required_argument, NULL, 'q'},
      {NULL, 0, NULL, 0}
  };
  int ch=0;
  while ((ch = getopt_long(argc, argv, &quot;abcdefghijklmnopq:&quot;, long_options, NULL)) != -1)
  {
      // check to see if a single character or long option came through
          switch (ch){
          case 'a':
              cout&lt;&lt;&quot;title&quot;;
              break;
          case 'b':
              
              break;
          case 'c':
              
              break;
          case 'd':
              
              break;
          case 'e':
              
              break;
          case 'f':
              
              break;
          case 'g':
              
              break;
          case 'h':
              
              break;
          case 'i':
              
              break;
          case 'j':
              
              break;
          case 'k':
              
              break;
          case 'l':
              
              break;
          case 'm':
              
              break;
          case 'n':
              
              break;
          case 'o':
              
              break;
          case 'p':
              
              break;
          case 'q':
              cout&lt;&lt;&quot;file&quot;;
              break;
          case '?':
              cout&lt;&lt;&quot;wrong message&quot;
              break;  
      }
  }
  </code></pre>