Changeset - 436450a5ad64
[Not reviewed]
0 1 0
Branko Majic (branko) - 4 years ago 2021-03-02 18:36:35
branko@majic.rs
Noticket: [TEMPLATE.sh.tpl] Update snippet syntax for latest(ish) YASnippet:

- Use correct syntax for dynamic values.
- Perform the necessary escaping for the YASnippet syntax.
- Introduces quiet and debug mode options.
1 file changed with 59 insertions and 36 deletions:
0 comments (0 inline, 0 general) First comment
templates/TEMPLATE.sh.tpl
Show inline comments
 
# -*- 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<<<)
 
# `(file-name-nondirectory (buffer-file-name))`
 
#
 
# Copyright (C) (>>>YEAR<<<), Branko Majic <branko@majic.rs>
 
# 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.
 
#
 
@@ -18,39 +24,45 @@
 
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
#
 

	
 
# Treat unset variables as errors.
 
set -u
 

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

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

	
 
Usage: $program [OPTIONS] (>>>ARGUMENTS<<<)
 
Usage: \$program [OPTIONS] ${3:arguments}
 

	
 
$program (>>>LONG_DESC<<<)
 
\$program ${4:long description}
 

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

	
 
    -v        show script version and licensing information
 
    -h        show usage help
 
    -q
 
        Quiet mode.
 
    -d
 
        Enable debug mode.
 
    -v
 
        Show script version and licensing information.
 
    -h
 
        Show usage 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) (>>>YEAR<<<), Branko Majic <branko@majic.rs>                    |
 
| 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.                                   |
 
|                                                                       |
 
@@ -65,66 +77,77 @@ $program, version $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=""
 
fi
 

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

	
 
function info() {
 
    echo "${_text_bold}${_text_white}[INFO] ${_text_reset}" "$@"
 
    echo "\${_text_bold}\${_text_white}[INFO] \${_text_reset}" "\$@"
 
}
 

	
 
function success() {
 
    echo "${_text_bold}${_text_green}[OK]   ${_text_reset}" "$@"
 
    echo "\${_text_bold}\${_text_green}[OK]   \${_text_reset}" "\$@"
 
}
 

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

	
 
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
 

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

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

	
 
i=\$OPTIND
 
shift \$((\$i-1))
0 comments (0 inline, 0 general) First comment
You need to be logged in to comment. Login now