title: Mass arguments (operands) at first place in command line argument passing
body: <p>I use following lines (hope this is best practice if not correct me please) to handle command line options:</p>
  
  <pre><code>#!/usr/bin/bash
  
  read -r -d '' HELP &lt;&lt;EOF
  OPTIONS:
  -c    Enable color output
  -d    Enable debug output
  -v    Enable verbose output
  -n    Only download files (mutually exclusive with -r)
  -r    Only remove files (mutually exclusive with -n)
  -h    Display this help
  EOF
  
  # DECLARE VARIABLES WITH DEFAULT VALUES
  color=0
  debug=0
  verbose=0
  download=0
  remove=0
  
  
  OPTIND=1                # Reset in case getopts has been used previously in the shell
  invalid_options=();     # Array for invalid options
  
  while getopts ":cdvnrh" opt; do
    echo "Actual opt: $opt"
    case $opt in
      c)
        color=1
        ;;
      d)
        debug=1
        ;;
      v)
        verbose=1
        ;;
      n)
        download=1
        ;;
      r)
        remove=1
        ;;
      h)
        echo "$HELP"
        exit 1
        ;;
     \?)
        invalid_options+=($OPTARG)
        ;;
     *)
        invalid_options+=($OPTARG)
        ;;
    esac
  done
  
  # HANDLE INVALID OPTIONS
  if [ ${#invalid_options[@]} -ne 0 ]; then
    echo "Invalid option(s):" &gt;&amp;2
    for i in "${invalid_options[@]}"; do
      echo $i &gt;&amp;2
    done
    echo "" &gt;&amp;2
    echo "$HELP" &gt;&amp;2
    exit 1
  fi
  
  # SET $1 TO FIRST MASS ARGUMENT, $2 TO SECOND MASS ARGUMENT ETC
  shift $((OPTIND - 1))
  
  # HANDLE CORRECT NUMBER OF MASS OPTIONS
  if [ $# -ne 2 ]; then
    echo "Correct number of mass arguments are 2"
    echo "" &gt;&amp;2
    echo "$HELP" &gt;&amp;2
    exit 1
  fi
  
  
  # HANDLE MUTUALLY EXCLUSIVE OPTIONS
  if [ $download -eq 1 ] &amp;&amp; [ $remove -eq 1 ]; then
    echo "Options for download and remove are mutually exclusive" &gt;&amp;2
    echo "$HELP" &gt;&amp;2
    exit 1
  fi
  
  
  
  echo "color:    $color"
  echo "debug:    $debug"
  echo "verbose:  $verbose"
  echo "download: $download"
  echo "remove:   $remove"
  echo "\$1:      $1"
  echo "\$2:      $2"
  </code></pre>
  
  <p>If I call the script way that <a href="http://wiki.bash-hackers.org/howto/getopts_tutorial" rel="nofollow noreferrer">mass arguments</a> (those that are not switches or arguments for switches) are last arguments everything is working correctly:</p>
  
  <pre><code>$ ./getopts.sh -c -d -v -r a b
  Actual opt: c
  Actual opt: d
  Actual opt: v
  Actual opt: r
  color:    1
  debug:    1
  verbose:  1
  download: 0
  remove:   1
  $1:      a
  $2:      b
  </code></pre>
  
  <p>The problem is when I want to call the script so the mass arguments are first (or somewhere in the middle of switches that do not use arguments)</p>
  
  <pre><code>$ ./getopts.sh a b -c -d -v -r
  Correct number of mass arguments are 2
  
  OPTIONS:
  -c    Enable color output
  -d    Enable debug output
  -v    Enable verbose output
  -n    Only download files (mutually exclusive with -r)
  -r    Only remove files (mutually exclusive with -d)
  -h    Display this help
  </code></pre>
  
  <p>or</p>
  
  <pre><code>$ ./getopts.sh -c a b -d -v -r
  Actual opt: c
  Correct number of mass arguments are 2
  
  OPTIONS:
  -c    Enable color output
  -d    Enable debug output
  -v    Enable verbose output
  -n    Only download files (mutually exclusive with -r)
  -r    Only remove files (mutually exclusive with -d)
  -h    Display this help
  </code></pre>
  
  <p>I think this should be OK according (POSIX) standards, because following syntax which is basically the same is working as expected on my system:</p>
  
  <pre><code>$ cp test1/ test2/ -r
  $ cp test1/ -r test2/
  </code></pre>
  
  <p>I have search over the Internet but only thing that was close to my problem was <a href="https://stackoverflow.com/questions/17690289/put-operands-first-in-getopt">this one</a> related to C.</p>