Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
>>
|
| A bash template (BaT) to ease argument parsing and management
| 0.2.0
|
| Usage:
| bash_template.sh -a alpha -b bravo [-c charlie] -d delta
Expand Down
63 changes: 29 additions & 34 deletions args
Original file line number Diff line number Diff line change
Expand Up @@ -11,51 +11,51 @@ PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License at
# FOR A PARTICULAR PURPOSE. See the GNU General Public License at
# <http://www.gnu.org/licenses/> for more details.
#
# -----------------------------------------------------------------------------
#
# bash library for command line argument loading/parsing/validating
#

readonly ARGS_FILE="data/config.json"
EXEC_DIR="$(dirname "$(readlink -f "$0")")"
readonly ARGS_FILE="${EXEC_DIR}/data/config.json"

# -----------------------------------------------------------------------------
# query data/config.json, returning the details object
# get_config_details(), using external jq command, queries data/config.json,
# returning the details object
#
function get_config_details {
printf "%s" "$(jq -r '.details.'"$1"'' < "${ARGS_FILE}")"
function get_config_details() {
printf "%s" "$(jq -r '.details.'"$1"'' <"${ARGS_FILE}")"
}

# -----------------------------------------------------------------------------
# query data/config.json, returning the arguments object
# get_config_arg(), using external jq command, queries data/config.json,
# returning the arguments object
#
function get_config_arg {
printf "%s" "$(jq -r '.arguments['"$1"'].'"$2"'' < "${ARGS_FILE}")"
function get_config_arg() {
printf "%s" "$(jq -r '.arguments['"$1"'].'"$2"'' <"${ARGS_FILE}")"
}

# -----------------------------------------------------------------------------
# query data/config.json, returning the count of arguments
# get_config_args_length(), using external jq command, queries
# data/config.json, returning the count of arguments
#
function get_config_args_length {
printf "%s" "$(jq -r '.arguments|length' < "${ARGS_FILE}")"
function get_config_args_length() {
printf "%s" "$(jq -r '.arguments|length' <"${ARGS_FILE}")"
}

# -----------------------------------------------------------------------------
# return argument value from argument key passed into
# get_config_arg_value() returns argument value text from argument keys
#
function get_config_arg_value {
function get_config_arg_value() {

local count_config_args=0
count_config_args=$(get_config_args_length)

for ((j=0;j<count_config_args;j++)) do

local arg_options=""
arg_options=$(get_config_arg "$j" text_string)
for ((j = 0; j < count_config_args; j++)); do

if echo "$1" | grep -Eq "$arg_options"; then
if [ "$1" == "$(get_config_arg "$j" text_string)" ]; then
echo "${ARG_VALUE[$j]}"
return
fi
Expand All @@ -65,44 +65,39 @@ function get_config_arg_value {
}

# -----------------------------------------------------------------------------
# scan command line for arguments
# scan_for_args() scans command line for arguments, setting global ARG_VALUE[]
#
function scan_for_args {
function scan_for_args() {

while [[ $# -gt 0 ]]
do
while [[ $# -gt 0 ]]; do

local count_config_args=0
count_config_args=$(get_config_args_length)

for ((j=0;j<count_config_args;j++)) do

local arg_options=""
arg_options='('$(get_config_arg "$j" short_form)'|'$(get_config_arg "$j" long_form)')'
for ((j = 0; j < count_config_args; j++)); do

if echo "$1" | grep -Eq "$arg_options"; then
if [ "$1" == "$(get_config_arg "$j" short_form)" ] || [ "$1" == "$(get_config_arg "$j" long_form)" ]; then
ARG_VALUE[${j}]="$2"
shift # skip to next argument
break
fi

done

shift # skip to next argument
done

}

# -----------------------------------------------------------------------------
# check for arguments completeness, quitting if required arguments are missing
# check_for_args_completeness() checks for arguments completeness, quitting
# with a message if required arguments are missing
#
function check_for_args_completeness {
function check_for_args_completeness() {

local count_errs=0
local count_config_args=0
count_config_args=$(get_config_args_length)

for ((j=0;j<count_config_args;j++)) do
for ((j = 0; j < count_config_args; j++)); do

if [ -z "${ARG_VALUE[${j}]}" ] && [ "$(get_config_arg "$j" required)" == "true" ]; then
printf "%s\n" "Error: $(get_config_arg "$j" text_string) argument ($(get_config_arg "$j" short_form)|$(get_config_arg "$j" long_form)) missing."
Expand All @@ -115,4 +110,4 @@ function check_for_args_completeness {
quit 1
fi

}
}
28 changes: 15 additions & 13 deletions general
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License at
# FOR A PARTICULAR PURPOSE. See the GNU General Public License at
# <http://www.gnu.org/licenses/> for more details.
#
# -----------------------------------------------------------------------------
Expand All @@ -20,9 +20,10 @@ PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
#

# -----------------------------------------------------------------------------
# check program dependencies
# check_program_dependencies() checks external program dependencies passed in
# through $0
#
function check_program_dependencies {
function check_program_dependencies() {

local -a deps=("$@")

Expand All @@ -38,9 +39,9 @@ function check_program_dependencies {
}

# -----------------------------------------------------------------------------
# check file dependencies
# check_file_dependencies() checks file dependencies passed in through $0
#
function check_file_dependencies {
function check_file_dependencies() {

local -a deps=("$@")

Expand All @@ -56,9 +57,10 @@ function check_file_dependencies {
}

# -----------------------------------------------------------------------------
# display script banner
# display_banner() builds and displays a custom-sized script banner using
# configuration details managed through the args library
#
function display_banner {
function display_banner() {

printf "\n"
printf "%s\n" " |"
Expand All @@ -75,13 +77,13 @@ function display_banner {

# determine maximum column width for setting output columns
#
for ((j=0;j<count_args;j++)) do
for ((j = 0; j < count_args; j++)); do
arg_len=$(get_config_arg "$j" short_form)+$(get_config_arg "$j" long_form)
((${#arg_len} > max_col)) && max_col=${#arg_len}
((j == count_args-1 )) && ((max_col+=6))
((j == count_args - 1)) && ((max_col += 6))
done

for ((j=0;j<count_args;j++)) do
for ((j = 0; j < count_args; j++)); do
printf "%-${max_col}s %s\n" " | $(get_config_arg "$j" short_form), $(get_config_arg "$j" long_form)" "$(get_config_arg "$j" description)"
done

Expand All @@ -90,14 +92,14 @@ function display_banner {
}

# -----------------------------------------------------------------------------
# exit program (with exit status passed in)
# quit() exits program (with exit status passed in)
#
function quit {
function quit() {

local result=""
result=("${1}")

printf "\n"
exit "${result[0]}"

}
}