SUBCMDS="$PWD/subcmds"# XXX: TODO: don't hardcode as relative
CMD_ABS="$SUBCMDS"# (initial value) CMD_ABS stores the current cmd's absolute path
CMD_MAJ="$THIS"# (initial value) CMD_MAJ stores the current cmd's parent's name
CMD_MIN=""# (initial value) CMD_MIN stores the current cmd's name
# ======== INTERNAL STATE ========
# ANSI Coloring
BLACK='\033[30m'
RED='\033[31m'
GREEN='\033[32m'
YELLOW='\033[33m'
BLUE='\033[34m'
MAGENTA='\033[35m'
CYAN='\033[36m'
WHITE='\033[37m'
DEFCOL='\033[39m'# default colour
# ANSI Styling
RESET='\033[0m'
BOLD='\033[1m'
DIM='\033[2m'
ITALIC='\033[3m'
UNDERLINE='\033[4m'
BLINKSLOW='\033[5m'
BLINKFAST='\033[6m'
REVERSE='\033[7m'
INVISIBLE='\033[8m'
# Error Messages
function perr {echo -e "${BOLD}${RED}error:${RESET}$@\nTry ${BOLD}${GREEN}'--help'${RESET} for more information." >&2;}
function perr-usage {echo -e "$USAGE" >&2;}
function perr-badflag { perr "unrecognised flag ${BOLD}${MAGENTA}'$1'${RESET}";}
function perr-noflagval { perr "flag ${BOLD}${MAGENTA}'$1'${RESET} requires ${BOLD}${MAGENTA}${2}${RESET} argument(s), but only ${BOLD}${MAGENTA}${3}${RESET} were given";}
function perr-badarg { perr "unrecognised arg ${BOLD}${MAGENTA}'$1'${RESET}";}
function perr-noarg { perr "required argument ${BOLD}${MAGENTA}'$1'${RESET} is missing";}
# Failures
function throw {echo -e "${@:2}" >&2;if[["$1" -ge 0]];thenexit"$1";fi;}
function throw-usage { throw "$1""$(perr-usage 2>&1)";}
function throw-badflag { throw "$1""$(perr-badflag "${@:2}" 2>&1)";}
function throw-noflagval { throw "$1""$(perr-noflagval "${@:2}" 2>&1)";}
function throw-badarg { throw "$1""$(perr-badarg "${@:2}" 2>&1)";}
function throw-noarg { throw "$1""$(perr-noarg "${@:2}" 2>&1)";}
# Parsing/Validation
function required {[[ -n "$1"]]|| throw-noarg 1"${@:2}";}
# Other
function confirm-action {
local CHAR
while :;do
echo -e "$1"
read -n1 CHAR
case$CHAR in
[yY])
return0;;
[nN])
return1;;
esac
done
}
function confirm { confirm-action ":: Proceed? [Y/n] ";}