[shtpl] Added template hashbang params resolution and help generator
This commit is contained in:
parent
e513897de7
commit
d0bf795dc7
113
shtpl/shtpl
113
shtpl/shtpl
|
@ -1,5 +1,116 @@
|
|||
#!/bin/bash
|
||||
|
||||
# force verbose
|
||||
#_verbose=true
|
||||
verbose(){ [[ $_verbose == true ]] && echo "$@"; }
|
||||
debug(){ [[ $_verbose == true ]] && verbose -e "$(declare -p $@ | sed "s/^declare -\(.\) \([^=]*\)=/\\$c_purple\1 \\$c_cyan\2\\$c_yellow=\\$c_/")"; }
|
||||
|
||||
args=("$@")
|
||||
opts=()
|
||||
mandatories=()
|
||||
envs=()
|
||||
declare -A params=()
|
||||
file=""
|
||||
need_help=""
|
||||
|
||||
_icons=(🔘 ⚪ ⚫)
|
||||
|
||||
debug args
|
||||
|
||||
if [[ -r "${args[0]}" ]]
|
||||
then
|
||||
file="${args[0]}"
|
||||
unset args[0]
|
||||
elif [[ -r "${args[1]}" ]]
|
||||
then
|
||||
file="${args[1]}"
|
||||
opts=(${args[0]})
|
||||
unset args[0] args[1]
|
||||
fi
|
||||
|
||||
args=("${args[@]}") # Re-index array after unset of some indexes
|
||||
|
||||
#declare -p args file opts
|
||||
|
||||
given()
|
||||
{
|
||||
[[ -z "${!1}" ]] && echo false || echo true
|
||||
}
|
||||
|
||||
show_help()
|
||||
{
|
||||
local mands=$(for i in ${!mandatories[@]}
|
||||
do
|
||||
local mand="${mandatories[$i]}"
|
||||
# debug mand
|
||||
[[ $(given ${mandatories[$i]}) == true ]] &&
|
||||
printf "$c_cyan⚫${mandatories[$i]}$c_ = $c_green\"${!mand}\"$c_\n " ||
|
||||
printf "$c_light$c_red🔘${mandatories[$i]}$c_ is mandatory\n "
|
||||
done)
|
||||
local opts=$(for i in ${!envs[@]}
|
||||
do
|
||||
declare -a envar=(`echo "${envs[$i]}" | sed 's/=/ /'`)
|
||||
printf "$c_light$c_blue⚪${envar[0]}$c_, defaults to $c_green\"${envar[1]}\"$c_\n "
|
||||
done)
|
||||
local exps=""
|
||||
|
||||
cat << EOF
|
||||
|
||||
SHTPL template $(echo -e "$c_purple$file$c_")
|
||||
This template $([[ $isPipable == true ]] && echo is pipable and" ")needs:
|
||||
$mands
|
||||
and accepts optional:
|
||||
$opts
|
||||
|
||||
Examples:
|
||||
$(for i in ${!mandatories[@]}; do printf "${mandatories[$i]}=foo "; done)$file
|
||||
$(for i in ${!mandatories[@]}; do printf "${mandatories[$i]}=foo "; done)$(for i in ${!envs[@]}; do printf "$(echo "${envs[$i]}" | sed 's/=.*//')=foo "; done)$file
|
||||
$([[ $isPipable == true ]] && echo "something | ")$(for i in ${!mandatories[@]}; do printf "${mandatories[$i]}=foo "; done)$file
|
||||
|
||||
EOF
|
||||
exit 0
|
||||
}
|
||||
|
||||
param()
|
||||
{
|
||||
# echo param "$1"
|
||||
local name="$(echo "$1" | sed 's/^-*\([^=]*\).*/\1/')" # | awk -F= '{print $1}')"
|
||||
local value="$(echo "$1" | sed 's/^-+//' | awk -F= '{print $2}')"
|
||||
# debug name value
|
||||
params[$name]="${value:-true}"
|
||||
case "$name" in
|
||||
h|help) need_help=true; params[h]="${value:-true}"; params[help]="${value:-true}" ;;
|
||||
v|verbose) _verbose=true; params[v]="${value:-true}"; params[verbose]="${value:-true}" ;;
|
||||
p|pipable) isPipable=true; params[p]="${value:-true}"; params[pipable]="${value:-true}" ;;
|
||||
esac
|
||||
}
|
||||
|
||||
for i in ${!opts[@]}
|
||||
do
|
||||
case "${opts[$i]}" in
|
||||
-*|--*) param "${opts[$i]}" ;;
|
||||
*=*) envs+=("${opts[$i]}") ;;
|
||||
*) mandatories+=("${opts[$i]}"); [[ $(given ${opts[$i]}) == false ]] && need_help=true ;;
|
||||
esac
|
||||
done
|
||||
|
||||
for i in ${!args[@]}
|
||||
do
|
||||
case "${args[$i]}" in
|
||||
-*|--*) param "${args[$i]}" ;;
|
||||
# *) ;;
|
||||
esac
|
||||
|
||||
# [[ "${args[$i]}" == '-h' || "${args[$i]}" == '--help' ]] && need_help=true
|
||||
done
|
||||
|
||||
|
||||
debug args opts mandatories envs params file need_help _verbose isPipable
|
||||
|
||||
[[ $need_help == true ]] && show_help;
|
||||
|
||||
|
||||
|
||||
|
||||
# Utils
|
||||
|
||||
|
@ -39,7 +150,7 @@ slug()
|
|||
|
||||
eval "echo \"$(cat $1 | sed '
|
||||
# Suppress hashbang
|
||||
1{ /#\!.*shtpl/ d }
|
||||
1{ /#\!.*shtpl.*/ d }
|
||||
# Excape quotes
|
||||
s/"/\\"/g
|
||||
')\""
|
||||
|
|
Loading…
Reference in New Issue