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
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
``` terminal
|
| A bash template (BaT) to ease argument parsing and management
| 1.2.0
|
| Usage:
| bash_template.sh -a alpha -b bravo [-c charlie] -d delta
Expand All @@ -27,7 +28,7 @@
- `args`
- Routines created to:
- Scan command-line arguments for accuracy and completeness
- Parse a JSON file for program configuration details, such as the following JSON configuration file:
- Parse a JSON file for program configuration details, such as the following JSON configuration file (used in the [A-Bash-Template (BaT)](https://github.com/richbl/a-bash-template#a-bash-template) project):

``` json
{
Expand Down Expand Up @@ -80,11 +81,10 @@ Of course we are! Otherwise, what value does a BaT offer if it doesn't get used

All of our bash scripts have been written to use this project. It's cut our development time, and made it easier to provide updates and added functionality without having to spend time reinventing the wheel every time. Check out all of our projects here on [Github](https://github.com/richbl).


## <picture><source media="(prefers-color-scheme: dark)" srcset="https://github.com/user-attachments/assets/2356369f-c752-4e55-8443-49f4174df4b5" width="30"><source media="(prefers-color-scheme: light)" srcset="https://github.com/user-attachments/assets/2356369f-c752-4e55-8443-49f4174df4b5" width="30"><img src="https://github.com/user-attachments/assets/2356369f-c752-4e55-8443-49f4174df4b5" width="30"></picture> This Project Uses Git Submodules <picture><source media="(prefers-color-scheme: dark)" srcset="https://github.com/user-attachments/assets/2356369f-c752-4e55-8443-49f4174df4b5" width="30"><source media="(prefers-color-scheme: light)" srcset="https://github.com/user-attachments/assets/2356369f-c752-4e55-8443-49f4174df4b5" width="30"><img src="https://github.com/user-attachments/assets/2356369f-c752-4e55-8443-49f4174df4b5" width="30"></picture>

This project is managed as a [Git submodule project(s)](https://git-scm.com/book/en/v2/Git-Tools-Submodules) to keep projects that use this library stable while this library is periodically updated.

## License

This project is distributed under the [MIT License](https://github.com/richbl/a-bash-template/blob/main/LICENSE)
This project is distributed under the [MIT License](https://github.com/richbl/bash-lib/blob/main/LICENSE)
47 changes: 29 additions & 18 deletions args
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
#!/usr/bin/env bash

# -----------------------------------------------------------------------------
# Copyright (C) Business Learning Incorporated (businesslearninginc.com)
# Copyright (c) 2025 Richard Bloch
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later
# version.
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# 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
# <http://www.gnu.org/licenses/> for more details.
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# -----------------------------------------------------------------------------
#
# Bash library (bash-lib) for command line argument loading/parsing/validating
# Version: 1.1.0
#

# --- Constants and variables -----------------------------------------------
Expand Down Expand Up @@ -150,17 +151,27 @@ scan_for_args() {
# -----------------------------------------------------------------------------
# check_for_args_completeness() verifies all required arguments are present
#
function check_for_args_completeness() {
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
_load_config
local err_count=0
local arg_count
arg_count=$(get_config_args_length)

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."
((count_errs++))
for ((i = 0; i < arg_count; i++)); do
local is_required text_string short_form long_form
is_required=$(get_config_arg "$i" "required")

if [[ "$is_required" == "true" ]]; then
text_string=$(get_config_arg "$i" "text_string")

if [[ -z "${ARG_VALUE[${text_string}]:-}" ]]; then
short_form=$(get_config_arg "$i" "short_form")
long_form=$(get_config_arg "$i" "long_form")
printf "Error: argument '%s' (%s|%s) is missing.\n" \
"$text_string" "$short_form" "$long_form" >&2
((err_count++))
fi
fi
done

Expand Down
31 changes: 10 additions & 21 deletions general
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
#!/usr/bin/env bash

# -----------------------------------------------------------------------------
# Copyright (C) Business Learning Incorporated (businesslearninginc.com)
# Copyright (c) 2025 Richard Bloch
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later
# version.
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# 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
# <http://www.gnu.org/licenses/> for more details.
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# -----------------------------------------------------------------------------
#
# Bash library (bash-lib) for general-purpose script processing
# Version: 1.1.0
#

# -----------------------------------------------------------------------------
Expand Down Expand Up @@ -104,18 +105,6 @@ exist_directory() {

}

# -----------------------------------------------------------------------------
# function exist_directory() confirms directory existence
#
function exist_directory() {

if [ ! -d "${1}" ]; then
printf "\n%s\n" "Error: directory ${1} not found."
quit 1
fi

}

# -----------------------------------------------------------------------------
# quit() exits program (with exit status passed in)
#
Expand Down