#!/usr/bin/env bash
help_text="
NAME
bash-func - One line description.
USAGE
bash-func [options] <parameters>
OPTIONS
-x
Description...
-h|--help
Show help text.
DESCRIPTION
Description description description description.
AUTHOR
mjnurse.github.io - 2026
"
help_line="Example Bash Script"
web_desc_line="Example Bash Script"
tmpfile=$(mktemp)
trap 'rm -f "$tmpfile"' EXIT
# set -x # for debugging
# Terminal Colours
cdef="\e[39m" # default colour
cbla="\e[30m"; cgra="\e[90m"; clgra="\e[37m"; cwhi="\e[97m"
cred="\e[31m"; cgre="\e[32m"; cyel="\e[33m"; cblu="\e[34m"; cmag="\e[35m"; ccya="\e[36m";
clred="\e[91m"; clgre="\e[92m"; clyel="\e[93m"; clblu="\e[94m"; clmag="\e[95m"; clcya="\e[96m"
function cecho {
if [[ "$1" == "-u" ]]; then ul=true; shift; else ul=false; fi
color=c$1; shift; echo -e "${!color}$*${cdef}"
if $ul; then echo -e "${!color}${*//?/-}${cdef}"; fi
}
try="Try ${0##*/} -h for more information"
tmp="${help_text##*USAGE}"
usage=$(echo "Usage: ${tmp%%OPTIONS*}" | tr -d "\n" | sed "s/ */ /g")
if [[ "$1" == "" ]]; then
cecho -u lcya "The following files contain options but no '# arg:' comments:"
for f in *; do
if grep -q "case .1 in" "$f" && grep -q "^OPTIONS" "$f" && ! grep -q "# *arg: " "$f"; then
echo $f
fi
done
exit
fi
file="$1"
if [[ ! -f "$file" ]]; then
echo "Error: File '$file' not found."
exit 1
fi
if ! $(grep "# *arg:" "$file" > /dev/null); then
echo "No '# arg:' arguments found in $file. No changes made."
exit 0
fi
options="$(grep '# arg' "$file" | grep -v 'grep' | sed 's/^ */ /; s/) *# *arg: *\(<[^>]*>\)/ \1\n /; s/) *# *arg: */\n /; s/$/\.\n/; s/\.\.\n/\.\n/')"
safe_options=$(printf '%s' "$options" | sed 's/[&]/\\&/g; :a;N;$!ba;s/\n/#NL#/g')
echo "Extracted options:"
echo
echo OPTIONS
echo -e "$safe_options" | sed 's/#NL#/\n/g'
sed -i "/^OPTIONS/,/^DESCRIPTION/{/^\(OPTIONS\|DESCRIPTION\)/!d}; s@^OPTIONS@OPTIONS\n${safe_options}\n@; s/#NL#/\n/g" $file
echo
echo File: $file updated