1

Is it posseble to have a --help argument with getopts?

I'm currently using this to code the help feature:

#!/bin/bash

PROGNAME=${0##*/}
PROGVERSION=1.0

usage()
{
  cat << EO
Prog description goes here.

Usage: $PROGNAME

Options:
EO
  cat <<EO | column -s\& -t

-h|--help & show this output
-v|--version & show version information
EO
}

SHORTOPTS="hv"
LONGOPTS="help,version"

ARGS=$(getopt -s bash --options $SHORTOPTS  \
  --longoptions $LONGOPTS --name $PROGNAME -- "$@" )

eval set -- "$ARGS"

while true; do
   case $1 in
      -h|--help)
         usage
         exit 0
         ;;
      -v|--version)
         echo "$PROGVERSION"
         exit 0
         ;;
      --)
         shift
         break
         ;;
      *)
         shift
         break
         ;;
   esac
   shift
done

1 Answer 1

1

The bash getopts builtin does not support long option names with the double-dash prefix. It only supports single-character options.

Sign up to request clarification or add additional context in comments.

3 Comments

Yeah - that's what it looks like. Actually I'd love to have both -h and --help option. Can You recommend me something?
have a look at gnu getopt or shflags
@Adobe, @Fredrik: getopt has served me well the last few years (example). The only issue I know of is that it's not part of Bash - It's a separate package.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.