From 5858553e49ca6d7e87bc0bb6f5ce715437c2d5b0 Mon Sep 17 00:00:00 2001 From: Okinea Dev Date: Tue, 19 Nov 2024 13:09:12 +0200 Subject: [PATCH] =?UTF-8?q?chore:=20=F0=9F=AB=A7=20improve=20error=20handl?= =?UTF-8?q?ing=20and=20default=20variable=20assignments=20in=20`cli-tips.s?= =?UTF-8?q?h`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Simplified default assignments for `TIPS_FOLDER` and `LANGUAGE` using parameter expansion. * Enhanced error checking for language and keyword inputs to ensure proper usage. --- cli-tips.sh | 26 ++++++++++---------------- 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/cli-tips.sh b/cli-tips.sh index e077900..1ccb01d 100755 --- a/cli-tips.sh +++ b/cli-tips.sh @@ -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" @@ -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" @@ -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 ;; @@ -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" @@ -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