#!/bin/bash # # Bash Template Script Generator # Generates a Bash Script Template that handles Arguments and Flags Cleanly # # @author: Mad Robot # @license: GNU v3 # @date: March 12, 2016 ## SCRIPT_NAME="Bash Script Generator" SCRIPT_FILE="${0}" SCRIPT_VER="1.0.0" SCRIPT_OPTS=("(-n|--name):NAME" "(-v|--version):VERSION" "(-c|--catch-all):CATCHALL") SCRIPT_CATCHALL="yes" # Must be either "yes" or "no", enables a '_catchall' method executed when no command given # Print Usage for CLI function _help () { echo -e "${SCRIPT_NAME}\n" echo -e "-v|--version To display script's version" echo -e "-h|--help To display script's help\n" echo -e "Available commands:\n" echo -e "methods To display script's methods" _available-methods exit 0 } # Print CLI Version function _version () { echo -e "${SCRIPT_NAME}" 1>&2 echo -en "Version " 1>&2 echo -en "${SCRIPT_VER}" echo -e "" 1>&2 exit 0 } # List all the available public methods in this CLI function _available-methods () { METHODS=$(declare -F | grep -Eoh '[^ ]*$' | grep -Eoh '^[^_]*' | sed '/^$/d') if [ -z "${METHODS}" ]; then echo -e "No methods found, this is script has a single entry point." 1>&2 else echo -e "${METHODS}" fi exit 0 } # Dispatches CLI Methods function _handle () { METHOD=$(_available-methods 2>/dev/null | grep -Eoh "^${1}\$") if [ "x${METHOD}" != "x" ]; then ${METHOD} ${@:2}; exit 0 else # Call a Catch-All method if [ "${SCRIPT_CATCHALL}" == "yes" ]; then _catchall ${@}; exit 0 # Display usage options else echo -e "Method '${1}' is not found.\n"; _help; fi fi } # Generate Autocomplete Script function _generate-autocomplete () { SCRIPT="$(printf "%s" ${SCRIPT_NAME} | sed -E 's/[ ]+/-/')" ACS="function __ac-${SCRIPT}-prompt() {" ACS+="local cur" ACS+="COMPREPLY=()" ACS+="cur=\${COMP_WORDS[COMP_CWORD]}" ACS+="if [ \${COMP_CWORD} -eq 1 ]; then" ACS+=" _script_commands=\$(${SCRIPT_FILE} methods)" ACS+=" COMPREPLY=( \$(compgen -W \"\${_script_commands}\" -- \${cur}) )" ACS+="fi; return 0" ACS+="}; complete -F __ac-${SCRIPT}-prompt ${SCRIPT_FILE}" printf "%s" "${ACS}" } # # User Implementation Begins # # Catches all executions not performed by other matched methods function _catchall () { # Fetch the Bash Script Template SCRIPT_TEMPLATE=$(curl -Ls http://bit.ly/bash-script-template) # Verify the Catch All option if [ "${OPTS_CATCHALL}" != "yes" -a "${OPTS_CATCHALL}" != "no" ]; then OPTS_CATCHALL="no"; fi # Replace tokens here to enable template customization SCRIPT_TEMPLATE=$(printf "%s" "${SCRIPT_TEMPLATE}" | sed 's//'"${OPTS_NAME:-ScriptName}"'/g') SCRIPT_TEMPLATE=$(printf "%s" "${SCRIPT_TEMPLATE}" | sed 's//'"${OPTS_VERSION:-1.0.0}"'/g') SCRIPT_TEMPLATE=$(printf "%s" "${SCRIPT_TEMPLATE}" | sed 's//'"${OPTS_CATCHALL:-no}"'/g') # Output the Template printf "%s" "${SCRIPT_TEMPLATE}" exit 0 } # # User Implementation Ends # Do not modify the code below this point. # # Main Method Switcher # Parses provided Script Options/Flags. It ensures to parse # all the options before routing to a metched method. # # `