Skip to content

Instantly share code, notes, and snippets.

@mflorida
Last active August 30, 2017 03:15
Show Gist options
  • Save mflorida/3d2189f5d76c6c0ccf4720c3b60ec4a9 to your computer and use it in GitHub Desktop.
Save mflorida/3d2189f5d76c6c0ccf4720c3b60ec4a9 to your computer and use it in GitHub Desktop.
Specify script arguments in any order and execute in a specific order.
#!/usr/bin/env bash
# This script will parse the arguments passed to it
# and execute commands in a specified order.
# Example usage:
# ===================================================================================
# ./parse-args-ez.sh -ktdbyr --name=foo
# ...or...
# ./parse-args-ez.sh --name=foo --backup --reset --delete --build --deploy --restart
# -----------------------------------------------------------------------------------
# backs up the app, deletes the contents of the data folders, resets the database,
# rebuilds and deploys the app
OWD=`dirname $0`
SCRIPT=${BASH_SOURCE[0]}
# at least one argument is required
[[ $# == 0 ]] && echo "Command not specified. Example usage: '${SCRIPT} --rebuild'" && exit 1;
# save all passed arguments
ALLARGS=$*
# define list of acceptable arguments
OPTIONS=''
OPTIONS+='--help.'
OPTIONS+='--test.'
OPTIONS+='--verbose.'
OPTIONS+='--backup.'
OPTIONS+='--delete.'
OPTIONS+='--reset.'
OPTIONS+='--name.'
OPTIONS+='--path.'
OPTIONS+='--dir.'
OPTIONS+='--build.'
OPTIONS+='--deploy.'
OPTIONS+='--restart.'
OPTIONS+='--rebuild.'
bold=$(tput bold)
normal=$(tput sgr0)
# disable verbosity by default
VERBOSE=false
# save app name for use later
APPNAME="default"
# save path to app dir for use later
APPDIR="/app/default"
# save single-character flags
FLAGS=''
# collect commands to execute with specified parameters
COMMANDS=''
# define variable to hold argument values
THEVAL=''
# get value from --flag=value and save to $THEVAL var
# usage: getval 'flag' ${SRCSTR}
getval(){
local str=$2
[[ -z ${str} ]] && str=${COMMANDS}
THEVAL=${str##--*$1=}
THEVAL=${THEVAL%%.*}
}
helpmenu() {
echo ""
echo "================================================================================"
echo ""
echo "$ ${bold}parse-args${normal} [options]"
echo ""
echo " -h | --help - displays available flags with brief descriptions "
echo " (no other flags will be processed) "
echo " -v | --verbose - enable verbose output "
echo " -k | --backup - backup 'app' to '/backups' folder "
echo " -d | --delete - delete file data "
echo " -t | --reset - reset database "
echo " --name=x - name of the 'app' we're working with "
echo " --path=x - path for the 'app' we're working with "
echo " --dir=x - [alias to --path] "
echo " -b | --build - build the 'app' "
echo " -y | --deploy - deploy the 'app' "
echo " -r | --restart - restart the 'app' process(es) "
echo " -R | --rebuild - backup, delete, reset, build, deploy, restart "
echo ""
echo "================================================================================"
echo ""
exit 0
}
backup(){
echo "Backing up..."
}
delete(){
[[ -z $1 ]] && echo "Deleting stuff..." || echo "Deleting '$1'..."
}
resetdb(){
[[ ! -z $1 ]] \
&& echo "Resetting the '$1' database..." \
|| echo "Resetting the database..."
}
setpath(){
echo "Path set to '$1'..."
}
build(){
echo "Building '$1' app..."
}
deploy(){
echo "Deploying '$1' app..."
}
restart(){
echo "Restarting '$1' app..."
}
# ===================================================================================
# setup command functions for execution in the proper order
# keep track of invalid commands
INVALID=""
# iterate through the arguments
while [ $# -gt 0 ]; do
THEARG=$1
# look only at single-character arguments that start with a SINGLE hyphen
if [[ ! $1 =~ ^-- ]] && [[ $1 =~ ^- ]]; then
[[ $1 =~ h ]] && helpmenu && exit 0
[[ $1 =~ v ]] && COMMANDS+='--verbose.'
[[ $1 =~ k ]] && COMMANDS+='--backup.'
[[ $1 =~ d ]] && COMMANDS+='--delete.'
[[ $1 =~ t ]] && COMMANDS+='--reset.'
[[ $1 =~ b ]] && COMMANDS+='--build.'
[[ $1 =~ y ]] && COMMANDS+='--deploy.'
[[ $1 =~ r ]] && COMMANDS+='--restart.'
[[ $1 =~ R ]] && COMMANDS+='--REBUILD.'
# collect valid and invalid single-character flags
while read -n 1 FLAG; do
[[ ${FLAG} =~ [hvkdtbyrR] ]] && FLAGS+="-${FLAG} " || INVALID+="-${FLAG} "
done < <(printf %s ${1##*-})
# now look at other arguments and variables
else
# strip prepended '--'
THEARG=${THEARG##*--}
# add to command list
COMMANDS+="--${THEARG}."
# extract flag name before '='
OPTION=${THEARG%=*}
[[ ! ${OPTIONS} =~ "--${OPTION}." ]] && INVALID+="$1 "
fi
# next arg please
shift
done
echo ""
[[ ! -z ${INVALID} ]] && echo "Error: Invalid option(s): ${INVALID}" && echo ""
# print command string if $VERBOSE == true
# it doesn't matter if commands are repeated in
# the string - they'll still only be executed once
[[ ${COMMANDS} =~ (--verbose.|--test.) ]] && echo "FLAGS = ${FLAGS}" && echo "COMMANDS = ${COMMANDS}"
# --test flag exits before executing commands
[[ ${COMMANDS} =~ '--test.' ]] && echo "" && exit 0
# explicitly set the app name
[[ ${COMMANDS} =~ '--name=' ]] && getval 'name' && APPNAME=${THEVAL} && THEVAL=""
[[ ${COMMANDS} =~ '--app=' ]] && getval 'app' && APPNAME=${THEVAL} && THEVAL=""
# explicitly set the app path
[[ ${COMMANDS} =~ '--path=' ]] && getval 'path' && setpath ${THEVAL} && THEVAL=""
# get confirmation for a destructive operation
if [[ ${COMMANDS} =~ (--REBUILD.|--rebuild.) ]]; then
echo ""
read -p "Are you sure you'd like to rebuild and deploy your app? [y/N] " CHOICE
echo ""
[[ ${CHOICE} =~ [Yy] ]] \
&& { echo "Proceeding with rebuild..."; echo ""; } \
|| { echo "Exiting without rebuilding."; echo ""; exit 1; }
COMMANDS+='--stop.--backup.--delete.--reset.--build.--deploy.--restart.'
fi
# test commands and execute in proper order
[[ ${COMMANDS} =~ '--backup.' ]] && backup
[[ ${COMMANDS} =~ '--delete.' ]] && delete ${APPNAME}
[[ ${COMMANDS} =~ '--reset.' ]] && resetdb
[[ ${COMMANDS} =~ '--reset=' ]] && getval 'reset' && resetdb ${THEVAL} && THEVAL=""
[[ ${COMMANDS} =~ '--build.' ]] && build ${APPNAME}
[[ ${COMMANDS} =~ '--deploy.' ]] && deploy ${APPNAME}
# restart last
[[ ${COMMANDS} =~ '--restart.' ]] && restart ${APPNAME}
echo "Done."
echo ""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment