bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Bash getopts option


From: Bob Proulx
Subject: Re: Bash getopts option
Date: Fri, 2 Mar 2007 00:42:34 -0700
User-agent: Mutt/1.5.9i

Matthew Woehlke wrote:
> Shanks wrote:
> >The above code does not parse -help option. Any suggestions
> 
> I don't think getopts knows how to parse words as options (and not as 
> non-GNU-style, certainly). You are probably better off writing your own 
> parser, something like this:

Personally I prefer 'getopt'.  On my Debian system there are examples
shipped with the system in:
  /usr/share/doc/util-linux/examples/getopt-parse.bash.gz 
  /usr/share/doc/util-linux/examples/getopt-test.bash.gz

And it is useful to review the GNU coding standards.

  
http://www.gnu.org/prep/standards/html_node/Command_002dLine-Interfaces.html#Command_002dLine-Interfaces

Here is a simple but complete example.  This produces output suitable
to generate the man page with help2man.

Enjoy,
Bob

#!/bin/sh
#
# A sample program illustrating shell option parsing.
#
# Copyright (C) 2007 by Bob Proulx <address@hidden>.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

version=0.0

progname=$(basename $0)

print_help()
{
    cat <<'EOF'
Usage: exampleprog [options] SOURCE...
  or:  exampleprog [options] DIRECTORY

Do something with something.  Write your man page description here.

Options:
      --help          print this help message
      --version       print program version
  -a, --alpha         do alpa
  -b, --bravo=STRING  do bravo
  -d, --debug         debug program
  -q, --quiet         quiet output
  -v, --verbose       verbose output

Examples:

The most common use is to run it like this.

  $ exampleprog

But sometimes like this.

  $ exampleprog -q -a --bravo=foo

Report bugs to <mailing-address>.
EOF
}

print_version()
{
    cat <<EOF
$progname (@GNU_PACKAGE@) $version
Copyright (C) @RELEASE_YEAR@ Free Software Foundation, Inc.
This is free software.  You may redistribute copies of it under the terms of
the GNU General Public License <http://www.gnu.org/licenses/gpl.html>.
There is NO WARRANTY, to the extent permitted by law.

Written by Bob Proulx.
EOF
}

SHORTOPTS="ab:dqv"
LONGOPTS="help,version,alpha,bravo:,debug,quiet,verbose"

if $(getopt -T >/dev/null 2>&1) ; [ $? = 4 ] ; then # New longopts getopt.
    OPTS=$(getopt -o $SHORTOPTS --long $LONGOPTS -n "$progname" -- "$@")
else # Old classic getopt.
    # Special handling for --help and --version on old getopt.
    case $1 in --help) print_help ; exit 0 ;; esac
    case $1 in --version) print_version ; exit 0 ;; esac
    OPTS=$(getopt $SHORTOPTS "$@")
fi

if [ $? -ne 0 ]; then
    echo "'$progname --help' for more information" 1>&2
    exit 1
fi

eval set -- "$OPTS"

alpha=false
bravo=""
debug=false
quiet=false
verbose=false
while [ $# -gt 0 ]; do
    : debug: $1
    case $1 in
        --help)
            print_help
            exit 0
            ;;
        --version)
            print_version
            exit 0
            ;;
        -a|--alpha)
            alpha=true
            shift
            ;;
        -b|--bravo)
            bravo=$2
            shift 2
            ;;
        -d|--debug)
            debug=true
            shift
            ;;
        -q|--quiet)
            quiet=true
            shift
            ;;
        -v|--verbose)
            verbose=true
            shift
            ;;
        --)
            shift
            break
            ;;
        *)
            echo "Internal Error: option processing error: $1" 1>&2
            exit 1
            ;;
    esac
done

if $verbose; then
   echo "Verbose output here."
fi

if ! $quiet; then
    echo Say stuff here.
fi

$debug && echo No bugs found here.

exit 0




reply via email to

[Prev in Thread] Current Thread [Next in Thread]