title: Why cant I have a space between option and optional argument using getopt?
body: <p>When using getopt to parse commandline parameters you can put a space in between the option flag and the argument for required arguments but not for optional arguments. Optional arguments will only be parsed if they are right after the option.</p>
  
  <pre><code>TEMP=`getopt -o p:q:: -n 'mkqueue.sh' -- "$@"`
  
  if [ $? != 0 ] ; then echo "Terminating..." &gt;&amp;2 ; exit 1 ; fi
  
  # Note the quotes around `$TEMP': they are essential!
  eval set -- "$TEMP"
  
  # Now go through all the options
  while true ; do
      case "$1" in
          -p) echo "Option p, argument \`$2'" ; shift 2 ;;
          -q) 
              case "$2" in
                  "") echo "Option q, no argument"; shift 2 ;;
                  *)  echo "Option q, argument \`$2'" ; shift 2 ;;
              esac ;;
          --) shift ; break ;;
          *) echo "Internal error!" ; exit 1 ;;
      esac
  done
  </code></pre>
  
  <p>(Based on this: <a href="http://software.frodo.looijaard.name/getopt/docs/getopt-parse.bash" rel="noreferrer">http://software.frodo.looijaard.name/getopt/docs/getopt-parse.bash</a>)</p>
  
  <p>When I run the script without the optional argument it works:</p>
  
  <pre><code>./mkqueue.sh -p adfsa -q
  Option p, argument `adfsa'
  Option q, no argument
  </code></pre>
  
  <p>If I try to add the optional argument to -q with a space in between it doesnt work:</p>
  
  <pre><code>./mkqueue.sh -p adfsa -q sdfasdfa
  Option p, argument `adfsa'
  Option q, no argument
  </code></pre>
  
  <p>It works if you don't have a space in between the option and argument, even though required arguments work with the space:</p>
  
  <pre><code>./mkqueue.sh -p adfsa -qsdfasdfa
  Option p, argument `adfsa'
  Option q, argument `sdfasdfa'
  </code></pre>
  
  <p>Is there a fix for this?</p>