Changeset - 372e0596bf33
[Not reviewed]
0 1 0
Branko Majic (branko) - 2 years ago 2022-08-15 23:02:47
branko@majic.rs
[TEMPLATE.sh.tpl] Switch to using upper-case names for global variables:

- Makes it easier to differntiate local variables in functions.
- Allows for avoiding "shadowing" of lower-cased/upper-cased variable
names by mistake in functions.
1 file changed with 37 insertions and 37 deletions:
0 comments (0 inline, 0 general) First comment
templates/TEMPLATE.sh.tpl
Show inline comments
 
@@ -24,39 +24,39 @@
 
# 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}"
 
PROGRAM="`(file-name-nondirectory (buffer-file-name))`"
 
VERSION="${1:version}"
 

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

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

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

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

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

	
 
\$program ${4:long description}
 
\$PROGRAM ${4:long description}
 

	
 
\$program accepts the following options:
 
\$PROGRAM accepts the following options:
 

	
 
    -q
 
        Quiet mode.
 
    -d
 
        Enable debug mode.
 
    -v
 
@@ -67,13 +67,13 @@ function long_help() {
 
Please report bugs and send feature requests to <branko@majic.rs>.
 
EOF
 
}
 

	
 
function version() {
 
        cat <<EOF
 
\$program, version \$version
 
\$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  |
 
@@ -91,87 +91,87 @@ function version() {
 

	
 
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)
 
_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=""
 
    _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}" "\$@"
 
    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}" "\$@"
 
    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}" "\$@"
 
    if [[ $QUIET == 0 ]]; then
 
        echo "\${_TEXT_BOLD}\${_TEXT_GREEN}[OK]   \${_TEXT_RESET}" "\$@"
 
    fi
 
}
 

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

	
 
function error() {
 
    echo "\${_text_bold}\${_text_red}[ERROR]\${_text_reset}" "\$@" >&2
 
    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
 
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;;
 
	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
 
if [[ \$QUIET != 0 && \$DEBUG != 0 ]]; then
 
    error "Quiet and debug options are mutually exclusive."
 
    exit "\$ERROR_ARGUMENTS"
 
fi
0 comments (0 inline, 0 general) First comment
You need to be logged in to comment. Login now