Skip to content

Commit

Permalink
chore: 🫧 improve error handling and default variable assignments in `…
Browse files Browse the repository at this point in the history
…cli-tips.sh`

* Simplified default assignments for `TIPS_FOLDER` and `LANGUAGE` using parameter expansion.
* Enhanced error checking for language and keyword inputs to ensure proper usage.
  • Loading branch information
okineadev committed Nov 19, 2024
1 parent 57e15fa commit 5858553
Showing 1 changed file with 10 additions and 16 deletions.
26 changes: 10 additions & 16 deletions cli-tips.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
# Exit on any error
set -e

# Determine prefix
if [[ ! -n "$PREFIX" ]]; then
if echo "$OSTYPE" | grep -qE '^darwin.*'; then
prefix="/usr/local"
Expand All @@ -16,18 +17,12 @@ else
fi

# Default tips folder
if [ ! -n "$TIPS_FOLDER" ]; then
TIPS_FOLDER="$prefix/share/cli-tips"
fi
TIPS_FOLDER="${TIPS_FOLDER:-$prefix/share/cli-tips}"

SYSTEM_LANGUAGE="$(echo "$LANG" | cut -d'_' -f1)"

# Default language is based on the user's environment
if [ ! -n "$TIPS_LANGUAGE" ]; then
LANGUAGE="$SYSTEM_LANGUAGE"
else
LANGUAGE="$TIPS_LANGUAGE"
fi
LANGUAGE="${TIPS_LANGUAGE:-$SYSTEM_LANGUAGE}"

show_help() {
echo -e "\e[1mUsage:\e[0m $(basename "$0") \e[1;34m[flags]\e[0m \e[1;32m[options]\e[0m"
Expand Down Expand Up @@ -57,17 +52,21 @@ show_help() {
while [[ "$#" -gt 0 ]]; do
case $1 in
-l | --language | --lang)
LANGUAGE="$2"
if [[ ! -n "$LANGUAGE" ]]; then
if [[ -z "$2" ]]; then
echo "Error: No language specified"
exit 1
fi
LANGUAGE="$2"
shift
;;
--language=* | --lang=*)
LANGUAGE="${1#*=}"
;;
--about)
if [[ -z "$2" ]]; then
echo "Error: No keyword specified"
exit 1
fi
KEYWORD="$2"
shift
;;
Expand All @@ -83,11 +82,6 @@ while [[ "$#" -gt 0 ]]; do
shift
done

if [[ ! -n "$LANGUAGE" ]]; then
echo "Error: No language specified"
exit 1
fi

# Find the localized tips file
if [ -f "$TIPS_FOLDER/${LANGUAGE}.txt" ]; then
localized_file="$TIPS_FOLDER/${LANGUAGE}.txt"
Expand All @@ -102,7 +96,7 @@ mapfile -t tips <"$localized_file"
if [ -n "$KEYWORD" ]; then
filtered_tips=()
for tip in "${tips[@]}"; do
if [[ "$tip" == *"$KEYWORD"* ]]; then
if [[ "$tip" =~ $KEYWORD ]]; then
filtered_tips+=("$tip")
fi
done
Expand Down

0 comments on commit 5858553

Please sign in to comment.