Skip to content

Instantly share code, notes, and snippets.

@reggi
Created August 12, 2021 15:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save reggi/af1cff981affa9e1d07d8e8da168faab to your computer and use it in GitHub Desktop.
Save reggi/af1cff981affa9e1d07d8e8da168faab to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
BASEDIR=$(dirname "$0")
function fileName () {
echo $(basename ${1%.*})
}
# define variables
FILES="$(find "$BASEDIR" ! -name 'index.sh' -type f -name '*.sh' -print | sort -t '\0' -n)"
TUSAGE="\\n"
HELP=false
FNPREFIX=''
USAGE=""
DESC=""
FOUND=false
# loop over script files
for FILE in $FILES; do
if [ -f "$FILE" ]; then
# shellcheck source=/dev/null
. "$FILE" --source-only
FNAME=`fileName $FILE`
BUSAGE="$FNAME $USAGE"
TUSAGE="$TUSAGE\\t$BUSAGE\\n $DESC\n\n"
USAGE=""
if [[ "$1" = "$FNAME" || "$FOUND" = true ]]; then
FOUND=true
fi
fi
done
# add help to end of usage
TUSAGE="$TUSAGE\\t--help (shows this help output)\\n"
function namespaceRmDash () {
echo "$1" | tr -d -
}
function namespaceUpperCase () {
echo "$(echo "${1:0:1}" | tr '[:lower:]' '[:upper:]')${1:1}"
}
for ARG in "$@"; do
if [[ "$ARG" = '--help' ]] || [[ "$ARG" = '-h' ]] || [[ "$ARG" = '--h' ]] || [[ "$ARG" = '-help' ]]; then
HELP=true
fi
done
function showUsage {
printf "Usage: function <...args>\n"
printf "$TUSAGE"
exit 0
}
if [[ "$HELP" = true ]]; then
showUsage
elif [[ ! -z "$@" && "$FOUND" = true ]]; then
eval "$@"
else
showUsage
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment