#!/bin/sh

NAME=$(basename "$0")
help() {
    # extract contiguous lines of comments in a function as help text
    awk -v cmd="${1:?'Command name required.'}" -v NAME="$NAME" '
    $0 ~ "^" cmd "\\s*\\(\\)\\s*{" { is_found=1; next }
    is_found && !NF { exit }
    is_found { gsub(/^\s*#\s?/, ""); gsub(/NAME/, NAME); print; }
    ' "$0"
}

main() {
    # Usage: NAME [<flags>]
    #
    # Flag | Description
    # ---- | -----------
    # -a   | Alpha (lower) characters.
    # -A   | Alpha (upper) characters.
    # -n   | Numeric characters.
    # -s   | Symbol characters.
    # -x   | Enable xtrace debug logging.

    while getopts h opt; do
      case "${opt}" in
        h) help main; exit ;;
        *) help main; exit ;;
      esac
    done
    shift "$((OPTIND - 1))"
}

main "$@"
