title: getopt order of argv when passing multiple arguments to one option
body: <p>I'm trying to pass multiple arguments to one option.
  For example,</p>
  
  <p>(1) <code>./someProgram -f opt1 opt2 opt3 -j opt -d</code>.</p>
  
  <p>(2) <code>./someProgram -f /dir/dir/* -j opt -d</code>.</p>
  
  <p>My settings for getopt_long looks like this.</p>
  
  <pre><code>const char *short_opt = "hp:j:o:f:";
      struct option long_opt[] =
              {
                      {"help",      no_argument,       NULL, 'h'},
                      {"journal",   required_argument, NULL, 'j'},
                      {"partition", required_argument, NULL, 'p'},
                      {"output",    required_argument, NULL, 'o'},
                      {"file",      required_argument, NULL, 'f'},
                      {NULL, 0,                        NULL, 0}
              };
  </code></pre>
  
  <p>I tried to simplify main code just so it doesn't look overwhelming.
  I left parts where I interact with getopt_long variables.</p>
  
  <pre><code>while ((arg = getopt_long(argc, argv, short_opt, long_opt, NULL)) != -1) {
          switch (arg) {
              case -1:
              case 0:        
                  break;
              ...
              case 'j':
                  if (optarg) {
                      setSomeVar; //And nothing else, only set var and break.
                  }
                  break;
              case 'f':
                  if (optarg) {
                      index = optind - 1;
                      while (index &lt; argc) {
                          nextOpt = strdup(argv[index]);
                          index++;
                          if (nextOpt[0] != '-') {
                              callFunc(nextOpt);
                          }
                          else {
                              break;
                          }
                      }
                      optind = index - 1;
                  }
                  else {
                      fprintf(stderr, "...\n");
                      exit(EXIT_FAILURE);
                  }
                  break;
              case ':':
              case '?':
                  fprintf(stderr, "...");
                  return (EXIT_FAILURE);
  
              default:
                  fprintf(stderr, "...", argv[0], arg);
                  return (EXIT_FAILURE);
          };
  </code></pre>
  
  <p>If I call my program like below, everything is as expected.</p>
  
  <p><strong>First input</strong></p>
  
  <pre><code>./prog -f /dir/dir/* - (60 files) or ./prog -f file1 file2 file3
  info about file1
  info about file2
  ... and so on
  
  </code></pre>
  
  <p>If I will add another option to the end.</p>
  
  <p><strong>Second input</strong></p>
  
  <pre><code>./prog -f file1 file2 file3 file4 -j smth
  info about1 file1
  action with -j smth. **End.**
  
  </code></pre>
  
  <p>I tried to list all arguments in the begining, with the <em>second input</em> like this:</p>
  
  <pre><code>for (int i = 0; i &lt; argc; i++) {
          printf("argv[%d] = %s\n", i, argv[i]);
  }
  </code></pre>
  
  <p>And result was as expected(for second input)</p>
  
  <pre><code>-f, file1,file... -j,print
  </code></pre>
  
  <p>Again I included this for loop in my switch, under 'f' case.
  And using second input, I saw that alredy 3 argument was -j, and 4th was option for j, and only after that there were all other files.
  Output was like you already can guess,</p>
  
  <pre><code>file1, -j, print, file2, file3,file4
  </code></pre>
  
  <p>My question is why this is happening? Or how should I fix it, or were do I need to look?
  Also I tried to improve my code looking at several similar questions answered here, but it looks like Im already following all the advice. </p>
  
  <p><a href="https://stackoverflow.com/questions/15466782/parsing-command-line-options-with-multiple-arguments-getopt/15467257">Parsing command line options with multiple arguments [getopt?]</a></p>
  
  <p><a href="https://stackoverflow.com/questions/3939157/c-getopt-multiple-value">C getopt multiple value</a></p>
  
  <p><a href="https://stackoverflow.com/questions/17428093/get-several-values-for-the-same-option?noredirect=1&amp;lq=1">Get several values for the same option [duplicate]</a></p>
  
  <p>Solution by passing all argument in quotes, doesn't work for me, because user may want to redirect input from ls, for example, or pass directrory with * at the end.</p>
  
  <p>Thank you :)</p>