Commit d2ccb8c9 authored by John A's avatar John A

add verbose option, error checking, simplify MacOS check

parent 5427b153
......@@ -28,7 +28,7 @@ infinite=0
termSize=1
termColors=0
leafChars='&'
basetype=1
baseType=1
message=""
multiplier=5
......@@ -42,29 +42,25 @@ flag_h=0
# non-CLI options
messageWidth=20
verbose=0
changed=()
seed="$RANDOM"
# ensure MacOS comatability
if [ $(uname) = 'Darwin' ]; then
# check for gnu-getopt (homebrew doesn't symlink this to /usr/local/bin)
GGETOPT=$(find /usr/local/Cellar/gnu-getopt -name getopt -type f -perm +111 | head -n1)
# if string is empty or file is not executable
if [[ -z "$GGETOPT" ]] || [[ ! -x "$GGETOPT" ]]; then
echo 'Error: Running on MacOS requires gnu getopt.'
# ensure MacOS compatibility with GNU getopt
if [ "$(uname)" = 'Darwin' ]; then
GGETOPT="/usr/local/Cellar/gnu-getopt/*/bin/getopt" # should link to gnu-getopt
if [[ ! -x "$GGETOPT" ]]; then # file is not executable
printf '%s\n' 'Error: Running on MacOS requires gnu getopt.'
exit 2
fi
# replace getopt with gnu getopt
getopt() { $GGETOPT "$@"; }
getopt() { $GGETOPT "$@"; } # replace getopt with gnu getopt
fi
# ------ parse options ------
OPTS="hlt:w:ig:c:Tm:b:M:L:s:" # the colon means it requires a value
LONGOPTS="help,live,time:,wait:,infinite,geometry:,leaf:,termcolors,message:,base:,multiplier:,life:,seed:"
OPTS="hlt:w:ig:c:Tm:b:M:L:s:v" # the colon means it requires a value
LONGOPTS="help,live,time:,wait:,infinite,geometry:,leaf:,termcolors,message:,base:,multiplier:,life:,seed:,verbose"
parsed=$(getopt --options=$OPTS --longoptions=$LONGOPTS -- "$@")
eval set -- "${parsed[@]}"
......@@ -114,7 +110,7 @@ while true; do
;;
-b|--base)
basetype="$2"
baseType="$2"
shift 2
;;
......@@ -138,6 +134,11 @@ while true; do
shift 2
;;
-v|--verbose)
verbose=1
shift 1
;;
--) # end of arguments
shift
break
......@@ -150,22 +151,37 @@ while true; do
esac
done
# sanitize input
if [ "$lifeStart" -gt 200 ] || [ "$lifeStart" -lt 1 ]; then
printf '%s\n' "Life value ($lifeStart) out of range: (1-200)"
exit 1
fi
if [ "$multiplier" -lt 0 ] || [ "$multiplier" -gt 20 ]; then
printf '%s\n' "Multiplier value ($multiplier) out of range: (0-20)"
exit 1
fi
if [ "$(printf '%s\n' "$timeStep < 0" | bc -l)" -eq 1 ]; then
printf '%s\n' "Time step ($timeStep) cannot be less than 0."
exit 1
fi
if [ "$(printf '%s\n' "$timeWait < 0" | bc -l)" -eq 1 ]; then
printf '%s\n' "Time step ($timeWait) cannot be less than 0."
exit 1
# ------ check input ------
# ensure integer values
if ! [ "$lifeStart" -eq "$lifeStart" 2> /dev/null ]; then
printf '%s\n' "--life ($lifeStart) invalid: must be an integer"; exit 1
elif ! [ "$multiplier" -eq "$multiplier" 2> /dev/null ]; then
printf '%s\n' "--multiplier ($multiplier) invalid: must be an integer"; exit 1
elif ! [ "$baseType" -eq "$baseType" 2> /dev/null ]; then
printf '%s\n' "--base ($baseType) invalid: must be an integer"; exit 1
# ensure ranges
elif [ "$baseType" -lt 0 ]; then
printf '%s\n' "--base ($baseType) invalid: out of range"; exit 1
elif [ "$lifeStart" -lt 1 ] || [ "$lifeStart" -gt 200 ]; then
printf '%s\n' "--life ($lifeStart) invalid: out of range"; exit 1
elif [ "$multiplier" -lt 0 ] || [ "$multiplier" -gt 20 ]; then
printf '%s\n' "--multiplier ($multiplier) invalid: out of range"; exit 1
elif [ "$seed" -lt 0 ] || [ "$seed" -gt 32767 ]; then
printf '%s\n' "--seed ($seed) invalid: out of range"; exit 1
# ensure floats are less than 0
elif [ "$(printf '%s\n' "$timeStep < 0" | bc -l)" -eq 1 ]; then
printf '%s\n' "--timestep ($timeStep) invalid: out of range"; exit 1
elif [ "$(printf '%s\n' "$timeWait < 0" | bc -l)" -eq 1 ]; then
printf '%s\n' "--wait ($timeWait) invalid: out of range"; exit 1
fi
HELP="\
......@@ -189,6 +205,7 @@ optional args:
branching (0-20) [default: 5]
-L, --life INT life; higher -> more growth (0-200) [default: 28]
-s, --seed INT seed random number generator (0-32767)
-v, --verbose print information each step of generation
-h, --help show help"
if ((flag_h)); then
......@@ -218,7 +235,7 @@ Grey='\e[1;30m'
R='\e[0m'
# create ascii base in lines
case "$basetype" in
case "$baseType" in
1)
width=15
art="\
......@@ -246,7 +263,7 @@ for line in $art; do
done
# create leafchars list
for char in $(echo "$leafChars" | tr , '\n'); do
for char in $(printf '%s' "$leafChars" | tr , '\n'); do
charList+=("$char")
done
charListLen="${#charList[@]}"
......@@ -521,8 +538,7 @@ branch() {
char="${charList[ $((RANDOM % charListLen)) ]}"
fi
# uncomment for help debugging
#echo -e "$life:\t$x, $y: $char"
((verbose)) && printf '%b\n' "$life:\t$x, $y: $char"
# put character in grid
grid[$y,$x]="${color}${char}${R}"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment