PDA

View Full Version : Detect --help in a shell scripts



lorderico
July 21st, 2010, 03:13 PM
Hi,

I'm having trouble finding the way to detect --help using getopts in shell script. Can I get a pointer?

Thanks,
Eric

dwhitney67
July 21st, 2010, 03:58 PM
Try the following to see if it meets your requirements:


#!/bin/bash

set -e

function help()
{
echo "All good questions deserve a good answer."
}

function usage()
{
echo "Usage: `basename $0` [-h | --help]"
exit 1
}


# script body
#
OPTS=`getopt -n $0 -o -h --long help -- $@`

eval set -- "$OPTS"

while true
do
case "$1" in
-h|--help) help; shift ;;
--) shift ; break ;;
*) usage; break;;
esac
done

# continue with script...