title: How to show help text if no arguments are passed
body: <p>I've been using getopts recently and I've set everything up. I've got a problem though. I want it to work so that if someone doesn't enter an argument on the command line, they get the help text, e.g.:</p>
  
  <pre><code>$ ./script
  $ help: xyz - argument must be used.
  </code></pre>
  
  <p>Here's what I have at the moment.</p>
  
  <pre><code>#!/bin/bash
  
  function helptext {
      # ...
  }
  
  function mitlicense {
      # ...
  }
  
  
  while getopts "hl" opt; do
    case $opt in
      h) helptext &gt;&amp;2
        exit 1
      ;;
      l) mitlicense &gt;&amp;2
        exit 0
      ;;
      \?) echo "Invalid option: -$OPTARG" &gt;&amp;2
        exit 1
      ;;
      :) echo "Option -$OPTARG requires an argument." &gt;&amp;2
        exit 1
      ;;
      *) helptext &gt;&amp;2
        exit 1
      ;;
    esac
  done
  </code></pre>