added cli options functions

This commit is contained in:
Nathaniel Landau
2015-01-04 00:50:21 -05:00
parent 8f558f6ccd
commit 5ff40a206c
6 changed files with 175 additions and 100 deletions

57
lib/parseOpts.sh Executable file
View File

@@ -0,0 +1,57 @@
#!/usr/bin/env bash
# Parse commandline options
#
# Works in along with the 'Command Line Options' and 'Set Switches' functions
# of many of my scripts
#
# All of this taken whole-cloth from: https://github.com/kvz/bash3boilerplate
#####################################################################
# Translate usage string -> getopts arguments, and set $arg_<flag> defaults
while read line; do
opt="$(echo "${line}" |awk '{print $1}' |sed -e 's#^-##')"
if ! echo "${line}" |egrep '\[.*\]' >/dev/null 2>&1; then
init="0" # it's a flag. init with 0
else
opt="${opt}:" # add : if opt has arg
init="" # it has an arg. init with ""
fi
opts="${opts}${opt}"
varname="arg_${opt:0:1}"
if ! echo "${line}" |egrep '\. Default=' >/dev/null 2>&1; then
eval "${varname}=\"${init}\""
else
match="$(echo "${line}" |sed 's#^.*Default=\(\)#\1#g')"
eval "${varname}=\"${match}\""
fi
done <<< "${usage}"
# Reset in case getopts has been used previously in the shell.
OPTIND=1
# Overwrite $arg_<flag> defaults with the actual CLI options
while getopts "${opts}" opt; do
line="$(echo "${usage}" |grep "\-${opt}")"
[ "${opt}" = "?" ] && help "Invalid use of script: ${@} "
varname="arg_${opt:0:1}"
default="${!varname}"
value="${OPTARG}"
if [ -z "${OPTARG}" ] && [ "${default}" = "0" ]; then
value="1"
fi
eval "${varname}=\"${value}\""
#debug "cli arg ${varname} = ($default) -> ${!varname}"
done
shift $((OPTIND-1))
[ "$1" = "--" ] && shift

View File

@@ -27,10 +27,10 @@ function scriptPath() {
# Outputs each line in a variable named $result
# ------------------------------------------------------
function readFile() {
unset $result
unset ${result}
while read result
do
echo $result
echo ${result}
done < "$1"
}
@@ -93,8 +93,8 @@ function pushover() {
# Send to Pushover
PUSHOVERURL="https://api.pushover.net/1/messages.json"
API_KEY="$PUSHOVER_API_KEY"
USER_KEY="$PUSHOVER_USER_KEY"
API_KEY="${PUSHOVER_API_KEY}"
USER_KEY="${PUSHOVER_USER_KEY}"
DEVICE=""
TITLE="${1}"
MESSAGE="${2}"
@@ -298,4 +298,22 @@ function unmountDrive() {
if [ -d "$1" ]; then
diskutil unmount "$1"
fi
}
# help
# ------------------------------------------------------
# Prints help for a script when invoked from the command
# line. Typically via '-h'. If additional flags or help
# text is available in the script they will be printed
# in the '$usage' variable.
# ------------------------------------------------------
function help () {
echo "" 1>&2
e_bold " ${@}" 1>&2
if [ -n "${usage}" ]; then # print usage information if available
echo " ${usage}" 1>&2
fi
echo "" 1>&2
exit 1
}

View File

@@ -9,12 +9,12 @@
# First we locate this script and populate the $SCRIPTPATH variable
# Doing so allows us to source additional files from this utils file.
SOURCE="${BASH_SOURCE[0]}"
while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
SOURCE="$(readlink "$SOURCE")"
[[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located
while [ -h "${SOURCE}" ]; do # resolve ${SOURCE} until the file is no longer a symlink
DIR="$( cd -P "$( dirname "${SOURCE}" )" && pwd )"
SOURCE="$(readlink "${SOURCE}")"
[[ ${SOURCE} != /* ]] && SOURCE="${DIR}/${SOURCE}" # if ${SOURCE} was a relative symlink, we need to resolve it relative to the path where the symlink file was located
done
SOURCEPATH="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
SOURCEPATH="$( cd -P "$( dirname "${SOURCE}" )" && pwd )"
# Write the list of utility files to be sourced
FILES="
@@ -26,10 +26,10 @@ FILES="
# Source the Utility Files
for file in $FILES
do
if [ -f "$SOURCEPATH/$file" ]; then
source "$SOURCEPATH/$file"
if [ -f "${SOURCE}PATH/${file}" ]; then
source "${SOURCE}PATH/${file}"
else
e_error "$file does not exist. Exiting"
e_error "${file} does not exist. Exiting"
Exit 1
fi
done