#! /bin/zsh # Copyright 2023-2024 Steinar Knutsen # # Licensed under the EUPL, Version 1.2 or - as soon they will be approved by the # European Commission - subsequent versions of the EUPL (the "Licence"); You may # not use this work except in compliance with the Licence. You may obtain a copy # of the Licence at: # # https://joinup.ec.europa.eu/collection/eupl/eupl-text-eupl-12 # # Unless required by applicable law or agreed to in writing, software # distributed under the Licence is distributed on an "AS IS" basis, WITHOUT # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the # Licence for the specific language governing permissions and limitations under # the Licence. setopt EXTENDED_GLOB setopt ERR_EXIT setopt PIPE_FAIL helptext() { cat <<'EOF' Usage: zshtemplate [-b] [-h] [-s string] [-x number] first second Template file with a bunch of arbitrary settings to use for templating scripts. -b Set a boolean value to true. -h Print this message and exit. -s name Set a string value to "name". -x number Set a value for arithmetic operations to "number". EOF exit $1 } check_number() { if [[ $1 != [0-9]## ]]; then echo >&2 'X must be an integer equal to or greater than zero.' exit 1 fi echo $1 } BOOLEAN='' NAME='' X=0 while getopts bhs:x: option; do case $option in (b) BOOLEAN=TRUE;; (h) helptext 0;; (s) NAME=$OPTARG;; (x) X=$(check_number $OPTARG);; (*) helptext >&2 1;; esac done shift $(( OPTIND - 1 )) if [[ $ARGC -ne 2 ]]; then helptext >&2 1 fi FIRST=$1 SECOND=$2 TRAPZERR() { if [[ -v WORKFILE && -f $WORKFILE ]]; then rm -f $WORKFILE fi } WORKFILE=$(mktemp) echo First is $FIRST echo Second is $SECOND if [[ -n $BOOLEAN ]] && (( X > 0 )); then echo Boolean is set and X is larger than 0. fi if [[ -z $BOOLEAN ]]; then echo Boolean is false. fi echo X is $X if [[ -n $NAME ]]; then echo Hello, $NAME. fi rm $WORKFILE