Q:

How to show help text if no arguments are passed

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.:
$ ./script
$ help: xyz - argument must be used.

Here's what I have at the moment.
#!/bin/bash

function helptext {
    # ...
}

function mitlicense {
    # ...
}

while getopts "hl" opt; do
  case $opt in
    h) helptext >&2
      exit 1
    ;;
    l) mitlicense >&2
      exit 0
    ;;
    \?) echo "Invalid option: -$OPTARG" >&2
      exit 1
    ;;
    :) echo "Option -$OPTARG requires an argument." >&2
      exit 1
    ;;
    *) helptext >&2
      exit 1
    ;;
  esac
done

A:

Validate user input using an if test such as below.
The -z option of test returns true if the length of the string which follows -z is zero.
 if [ -z "$1" ]
 then
  helptext
  exit 1
 fi

A:

Try using this in your script :
#!/bin/bash

[[ $@ ]] || { helptext; exit 1; }

# --- the rest of the script ---

The line of code is the boolean shortened version of 
if [[ $@ ]]; then
    true
else
    helptext
    exit 1
fi

$@ is all the arguments of the script
[[ $var ]]

is a shorthand for
[[ -n $var ]]

See http://mywiki.wooledge.org/BashGuide/TestsAndConditionals

A:

Gilles Quenot's answer works great and is very concise; if you're looking for solutions that express the intent more explicitly, you can try these, which are based on the count of parameters, $#:
[[ $# -gt 0 ]] || { helptext; exit 1; }

Alternative, using arithmetic expressions:
(( $# > 0 )) ||  { helptext; exit 1; }

Finally, shorthand that relies on 0 evaluating to false, and any non-zero number to true:
(( $# )) || { helptext; exit 1; }

William Pursell offers another variant, which is both descriptive and POSIX-compliant:
test $# -gt 0 || { helptext; exit 1; }

test / [ ... ] is a POSIX utility/built-in, whereas the similar [[ ... ]] conditional is bash-specific (as is (( ... ))).
Generally, however, bash's [[ ... ]] offers more features and has fewer surprises than test / [...].

