Files @ c8b6db5862e6
Branch filter:

Location: majic-scripts/templates/TEMPLATE.sh.tpl

c8b6db5862e6 4.7 KiB application/vnd.groove-tool-template Show Annotation Show as Raw Download as Raw
branko
Noticket: [TEMPLATE.sh.tpl] Improve help output and tweak quiet/debug option processing:

- Introduce short and long help output. Short help includes only the
basic usage information, while long help provides full description
with all options etc.
- Short help includes only the basic usage, and should fit much more
easily on a single screen of output.
- Long help is only shown when script is invoked with the -h option.
- Output warning messages to stderr instead of stdout.
- Hide info and success messages if quiet output is requested.
- Introduce check for exclusiveness between the debug and quiet
options.
- Fix some smaller issues reported by shellcheck.
# -*- mode: snippet -*-
# name: Bash script template with standard CLI processing and helper functions.
# key: bash-script
# contributor: Branko Majic <branko@majic.rs>
# type: snippet
# --
#!/bin/bash
#
# `(file-name-nondirectory (buffer-file-name))`
#
# Copyright (C) `(format-time-string "%Y")`, Branko Majic <branko@majic.rs>
#
# 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 3 of the License, 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, see <http://www.gnu.org/licenses/>.
#

# Treat unset variables as errors.
set -u

program="`(file-name-nondirectory (buffer-file-name))`"
version="${1:version}"

function usage() {
    cat <<EOF
\$program \$version, ${2:short description }

Usage:
  \$program [OPTIONS] ${3:arguments}
EOF
}

function short_help() {
    cat <<EOF
\$(usage)

For more details see \$program -h.
EOF
}

function long_help() {
    cat <<EOF
\$(usage)

\$program ${4:long description}

\$program accepts the following options:

    -q
        Quiet mode.
    -d
        Enable debug mode.
    -v
        Show script version and licensing information.
    -h
        Show full help.

Please report bugs and send feature requests to <branko@majic.rs>.
EOF
}

function version() {
        cat <<EOF
\$program, version \$version

+-----------------------------------------------------------------------+
| Copyright (C) `(format-time-string "%Y")`, Branko Majic <branko@majic.rs>                    |
|                                                                       |
| 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 3 of the License, 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, see <http://www.gnu.org/licenses/>. |
+-----------------------------------------------------------------------+

EOF
}

# Set-up colours for message printing if we're not piping and terminal is
# capable of outputting the colors.
_color_terminal=\$(tput colors 2>&1)
if [[ -t 1 ]] && (( _color_terminal > 0 )); then
    _text_bold=\$(tput bold)
    _text_white=\$(tput setaf 7)
    _text_blue=\$(tput setaf 6)
    _text_green=\$(tput setaf 2)
    _text_yellow=\$(tput setaf 3)
    _text_red=\$(tput setaf 1)
    _text_reset=\$(tput sgr0)
else
    _text_bold=""
    _text_white=""
    _text_blue=""
    _text_green=""
    _text_yellow=""
    _text_red=""
    _text_reset=""
fi

# Set-up functions for printing coloured messages.
function debug() {
    if [[ $debug != 0 ]]; then
        echo "\${_text_bold}\${_text_blue}[DEBUG]\${_text_reset}" "\$@"
    fi
}

function info() {
    if [[ $quiet == 0 ]]; then
        echo "\${_text_bold}\${_text_white}[INFO] \${_text_reset}" "\$@"
    fi
}

function success() {
    if [[ $quiet == 0 ]]; then
        echo "\${_text_bold}\${_text_green}[OK]   \${_text_reset}" "\$@"
    fi
}

function warning() {
    echo "\${_text_bold}\${_text_yellow}[WARN] \${_text_reset}" "\$@" >&2
}

function error() {
    echo "\${_text_bold}\${_text_red}[ERROR]\${_text_reset}" "\$@" >&2
}

# Define error codes.
SUCCESS=0
ERROR_ARGUMENTS=1

# Disable debug and quiet modes by default.
debug=0
quiet=0

# If no arguments were given, just show usage help.
if [[ -z \${1-} ]]; then
    short_help
    exit "$SUCCESS"
fi

# Parse the arguments
while getopts "qdvh" opt; do
    case "\$opt" in
	q) quiet=1;;
	d) debug=1;;
        v) version
           exit "$SUCCESS";;
        h) long_help
           exit "$SUCCESS";;
        *) short_help
           exit "$ERROR_ARGUMENTS";;
    esac
done
i=\$OPTIND
shift \$(( i-1 ))

# Quiet and debug are mutually exclusive.
if [[ \$quiet != 0 && \$debug != 0 ]]; then
    error "Quiet and debug options are mutually exclusive."
    exit "\$ERROR_ARGUMENTS"
fi