Skip to content

Commit

Permalink
Refactored flag script: made color variables local, improved default …
Browse files Browse the repository at this point in the history
…value handling, retained concatenation logic
  • Loading branch information
AmosNimos committed Apr 22, 2024
1 parent 7db356b commit ac8aaac
Showing 1 changed file with 52 additions and 54 deletions.
106 changes: 52 additions & 54 deletions flag
Original file line number Diff line number Diff line change
Expand Up @@ -6,78 +6,76 @@
# Description: The flag script parses command-line flags and sets variables accordingly.
################################################################################

# ▛▀▘▌ ▞▀▖▞▀▖
# ▙▄ ▌ ▙▄▌▌▄▖
# ▌ ▌ ▌ ▌▌ ▌
# ▘ ▀▀▘▘ ▘▝▀
# Function to print help message
print_help() {
# Define colors
local color_reset='\e[0m'
local yellow_color='\e[33m'
local green_color='\e[32m'

# If already define by the parrent script don't define the print_help
if [[ ! "$(declare -f)" =~ "print_help" ]]; then
print_help() {
# Define colors
color_reset='\e[0m'
yellow_color='\e[33m'
green_color='\e[32m'
echo "Usage: $0 [flags] [--help]"
echo ""

echo "Usage: $0 [flags] [--help]"
echo ""
echo -e "${green_color}Description:${color_reset}"
echo "The flag script parses command-line flags and sets variables accordingly."
echo "All flags, regardless of capitalization, are treated as taking a value if one is not explicitly provided."
echo "If a flag is provided without a value, it is set to 1 (true as boolean)."
echo ""

echo -e "${green_color}Description:${color_reset}"
echo "The flag script parses command-line flags and sets variables accordingly."
echo "All flags, regardless of capitalization, are treated as taking a value if one is not explicitly provided."
echo "If a flag is provided without a value, it is set to 1 (true as boolean)."
echo ""
echo -e "${green_color}Options:${color_reset}"
echo " --help Display this help message"
echo ""

echo -e "${green_color}Options:${color_reset}"
echo " --help Display this help message"
echo ""
echo -e "${green_color}Example:${color_reset}"
echo "To use the 'flag' script, include it in your project and call it with the source command, like this:"
echo ""
echo -e "${yellow_color}source flag \"\$@\"${color_reset}"
echo ""

echo -e "${green_color}Example:${color_reset}"
echo "To use the 'flag' script, include it in your project and call it with the source command, like this:"
echo ""
echo -e "${yellow_color}source flag \"\$@\"${color_reset}"
echo ""
echo -e "${green_color}Warning:${color_reset}"
echo -e "Make sure to source this script after the print_help function of your other script, to avoid overwriting them."

echo -e "${green_color}Warning:${color_reset}"
echo -e "Make sure to source this script after the print_help function of your other script, to avoid overwriting them."

exit 0
}
fi
exit 0
}

# Print help message if "--help" or "-h" flag is provided
if [[ ${1,,} == *"help" || ${1,,} == "-h" ]]; then
print_help
fi

# Function to process flags
process_flags() {
# Iterate through the arguments
while [[ $1 ]]; do
# Check if the argument starts with "--"
if [[ $1 =~ ^-- ]]; then
flag=${1#--} # Extract the flag name
elif [[ $1 =~ ^-[A-Za-z]$ ]]; then
# Check if the argument starts with a dash followed by an uppercase or lowercase letter
flag=${1#-} # Extract the flag letter
else
shift # Move to the next argument
continue
fi
local flag=""
local value=""

shift # Move to the next argument
while [[ $# -gt 0 ]]; do
arg="$1"
shift

if [[ $1 && $1 != -* ]]; then
# If the next argument exists and doesn't start with a dash (i.e., it's a value)
# Assign the value to the flag variable
eval "flag_$flag=\"$1\""
shift # Move to the next argument
# Check if the argument is a flag
if [[ $arg =~ ^-- ]]; then
flag="${arg#--}" # Extract the flag name
elif [[ $arg =~ ^-[A-Za-z]$ ]]; then
flag="${arg#-}" # Extract the flag letter
else
# If there's no value or the next argument starts with a dash
# Set the flag variable to 1
eval "flag_$flag=1"
# Argument is a value for the previous flag
if [[ -n $flag ]]; then
# Concatenate the argument to the existing value
value+=" $arg"
fi
fi

# If the flag is followed directly by another flag or is the last argument, set its value
if [[ $flag && ( ! $1 || $1 =~ ^- ) ]]; then
if [[ -z $value ]]; then
value=1
fi
eval "flag_$flag=\"$value\""
flag=""
value=""
fi
done
}

# Process flags from passed arguments
# Process flags
process_flags "$@"

0 comments on commit ac8aaac

Please sign in to comment.