title: Best way to parse arguments in bash script
body: <p>So I've been reading around about getopts, getopt, etc. but I haven't found an exact solution to my problem.</p>
  
  <p>The basic idea of the usage of my script is:</p>
  
  <pre><code>./program [-u] [-s] [-d] &lt;TEXT&gt;
  </code></pre>
  
  <p>Except TEXT is not required if -d is passed. Note that TEXT is usually a paragraph of text.</p>
  
  <p>My main problem is that once getopts finishing parsing the flags, I have no way of knowing the position of the TEXT parameter. I could just assume that TEXT is the last argument, however, if a user messes up and does something like:</p>
  
  <pre><code>./program -u "sentence 1" "sentence 2"
  </code></pre>
  
  <p>then the program will not realize that the usage is incorrect.</p>
  
  <p>The closest I've come is using getopt and IFS by doing </p>
  
  <pre><code>ARGS=$(getopt usd: $*)
  IFS=' ' read -a array &lt;&lt;&lt; "$ARGS"
  </code></pre>
  
  <p>The only problem is that TEXT might be a long paragraph of text and this method splits every word of text because of the spaces.</p>
  
  <p>I'm thinking my best bet is to use a regular expression to ensure the usage is correctly formed and then extract the arguments with getopts, but it would be nice if there was a simpler solution</p>