Skip to content

Instantly share code, notes, and snippets.

@mflorida
Created July 19, 2017 02:02
Show Gist options
  • Save mflorida/2f7748e8502068ec0de31c44fa5ed479 to your computer and use it in GitHub Desktop.
Save mflorida/2f7748e8502068ec0de31c44fa5ed479 to your computer and use it in GitHub Desktop.
Parse shell script arguments - accepted in any order but executed in a specific order.
#!/usr/bin/env bash
# This script will iterate through the arguments passed to it and collect
# a list of commands and variables to execute in a specified order.
# Example usage:
# ===================================================================================
# ./parse-args.sh -ktdbyr --name=foo
# ...or...
# ./parse-args.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;
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"
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}'..."
}
reset(){
echo "Resetting the database..."
}
path(){
echo "Path set to '${APPDIR}'..."
}
build(){
echo "Building '${APPNAME}' app..."
}
deploy(){
echo "Deploying '${APPNAME}' app..."
}
restart(){
echo "Restarting '${APPNAME}' app..."
}
# ===================================================================================
# setup command functions for execution in the proper order
# save single-character flags
FLAGS=""
# collect commands to execute with specified parameters
COMMANDS=":"
# iterate through the arguments
while [ $# -gt 0 ]; do
THEARG=$1
# look at single-character arguments that start with a SINGLE hyphen
if [[ ! $1 =~ ^-- ]] && [[ $1 =~ ^- ]]; then
FLAGS+="$1 "
[[ $1 =~ h ]] && helpmenu && exit 0
[[ $1 =~ v ]] && VERBOSE=true
[[ $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::stop::backup::delete::reset::pull::build::deploy::restart:"
# now look at other arguments and variables
else
# remove -- from beginning
THEARG=${THEARG##*--}
# add to list of commands
#COMMANDS+=":${THEARG##*=}:"
# extract argument value after '='
THEVAL=${THEARG##*=}
case "$1" in
--help)
helpmenu
exit 0
;;
--verbose)
VERBOSE=true
;;
# shortcut to catch multiple flags
--backup|--delete|--x*)
COMMANDS+=":${THEARG}:"
;;
# --backup)
# COMMANDS+=":backup:"
# ;;
#
# --delete)
# COMMANDS+=":delete:"
# ;;
--reset)
COMMANDS+=":reset:"
;;
--name=*)
COMMANDS+=":name:"
APPNAME=${THEVAL}
;;
--path=*|--dir=*)
COMMANDS+=":path:"
APPDIR=${THEVAL}
;;
--build)
COMMANDS+=":build:"
;;
--restart)
COMMANDS+=":restart:"
;;
--deploy)
COMMANDS+=":deploy:"
;;
--rebuild)
COMMANDS+=":REBUILD::stop::backup::delete::reset::pull::build::deploy::restart:"
;;
*)
echo "Error: Invalid option: ${1}"
;;
esac
fi
# next arg please
shift
done
echo ""
COMMANDS+=":"
# print command string if $VERBOSE == true
# it doesn't matter if commands are repeated in
# the string - they'll still only be executed once
[[ ${VERBOSE} == true ]] && echo "FLAGS = ${FLAGS}" && echo "COMMANDS = ${COMMANDS}"
# setup variables that were passed in arguments
[[ ${COMMANDS} =~ :name: ]] && echo "App name is '${APPNAME}'"
[[ ${COMMANDS} =~ :path: ]] && path
# get confirmation for a destructive operation
if [[ ${COMMANDS} =~ :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; }
fi
# test commands and execute in proper order
[[ ${COMMANDS} =~ :backup: ]] && backup
[[ ${COMMANDS} =~ :delete: ]] && delete ${APPNAME}
[[ ${COMMANDS} =~ :reset: ]] && reset
[[ ${COMMANDS} =~ :build: ]] && build
[[ ${COMMANDS} =~ :deploy: ]] && deploy
# restart last
[[ ${COMMANDS} =~ :restart: ]] && restart
echo "Done."
echo ""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment