Skip to content

Instantly share code, notes, and snippets.

@dominikj111
Last active January 19, 2024 07:22
Show Gist options
  • Save dominikj111/b7195c06832b5c1e2471d8dc1ca9524e to your computer and use it in GitHub Desktop.
Save dominikj111/b7195c06832b5c1e2471d8dc1ca9524e to your computer and use it in GitHub Desktop.
A bit more generic way to process bash cli params.
#!/bin/bash
set -euo pipefail
script_name=$(basename "$0")
bgreen() {
printf "\e[1m\e[32m"
"$@"
printf "\e[0m"
}
red() {
printf "\e[31m"
"$@"
printf "\e[0m"
}
display_help() {
echo "Usage: ./$script_name [OPTIONS]"
if [ -n "$help_text" ]; then
echo "$help_text"
fi
echo ""
echo "Options:"
echo "-h, --help Display this help message"
# shellcheck disable=SC2154
if [ ${#help_flags[@]} -ne 0 ]; then
for help_flag in "${help_flags[@]}"; do
echo "$help_flag"
done
fi
echo ""
}
while [[ $# -gt 0 ]]; do
key="$1"
case $key in
-h | --help)
display_help
exit 0
;;
*)
# Test if the flag is strictly short or long
if [[ "$key" =~ ^--[[:lower:]][[:lower:]]+$ ]]; then
# nothing todo
:
elif [[ "$key" =~ ^-[[:lower:]]$ ]]; then
# nothing todo
:
else
red echo "Unknown option: $1\n"
display_help
exit 1
fi
flag_name=""
# Loop through the external flag source
for help_flag in "${help_flags[@]}"; do
# Check if the flag is in the external flag source
if [[ "${help_flag}" == *"$1"* ]]; then
long_flag_name=""
short_flag_name=""
# Extract long flag name from the flag description
if [[ "${help_flag}" =~ -[[:lower:]],[[:space:]]--([[:lower:]][[:lower:]]+)[[:space:]].+ ]]; then
long_flag_name="${BASH_REMATCH[1]}"
fi
# Extract short flag name from the flag description
if [[ "${help_flag}" =~ -([[:lower:]]),[[:space:]]--[[:lower:]][[:lower:]]+[[:space:]].+ ]]; then
short_flag_name="${BASH_REMATCH[1]}"
fi
if [[ "$key" == "--$long_flag_name" ]] || [[ "$key" == "-$short_flag_name" ]]; then
flag_name="$long_flag_name"
fi
break
fi
done
if [ -n "$flag_name" ] && [ -n "${2+x}" ] && [ -n "$2" ]; then
flag_value="$2"
eval "${flag_name}='${flag_value}'"
shift
shift
continue
else
red echo "Unknown option: $1\n"
display_help
exit 1
fi
;;
esac
done
#!/bin/bash
# shellcheck disable=SC2034
# shellcheck disable=SC1091
help_text="
Purpose of this script is to test the 'config.sh' script.
list:
a - blah blah
b - blah blah
c - blah blah"
help_flags=(
"-p, --port Specify the port to listen on (default: 8080)"
)
. "$PWD/cli_params.sh"
if [ -z "${port+x}" ] || [ -z "$port" ]; then
port="8080"
fi
echo ""
bgreen echo "Going to serve on port '$port' ..."
echo ""
@dominikj111
Copy link
Author

Some valid examples:
sh cli_params_test.sh -h
sh cli_params_test.sh --help
sh cli_params_test.sh -p 967
sh cli_params_test.sh --port 967

These are invalid:
sh cli_params_test.sh -po 967
sh cli_params_test.sh -port 967
sh cli_params_test.sh --p 967

@dominikj111
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment