Skip to content

Commit

Permalink
## Version 1.0.17
Browse files Browse the repository at this point in the history
- Args refactor
  • Loading branch information
aho-dips committed Oct 31, 2024
1 parent 6797973 commit f9c881e
Show file tree
Hide file tree
Showing 6 changed files with 196 additions and 162 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## Version 1.0.17
- Args refactor

## Version 1.0.16
- Help Cleanup

Expand Down
162 changes: 1 addition & 161 deletions smud-cli/include.sh
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ contains() {
# if [ ${#contains_list__[@]} -gt 1 ]; then
# contains_list__="${contains_list__[@]}"
# fi
if [[ ! " ${contains_list__[@]} " =~ " (|'"'|"'")$2(|'"'|"'") " ]]; then
if [] ! " ${contains_list__[@]} " =~ " (|'"'|"'")$2(|'"'|"'") " ]; then
echo 'true'
fi
# [[ $contains_list__ =~ (^|[[:space:]])(|'"'|"'")$2(|'"'|"'")($|[[:space:]]) ]] && echo 'true' || echo ''
Expand Down Expand Up @@ -81,156 +81,6 @@ string_to_array()
IFS=$old_SEP
}

parse_arguments()
{
local old_SEP=$IFS
local -n parse_arguments_args="$1"
# echo "0.str: $@"
if [ $# -gt 0 ];then
has_args="true"
local str="$(echo " $@"|sed -e 's/--/-_sep_/g' |sed -e 's/ -/§/g')"
# echo "str: $str"
IFS="§"; read -ra array <<< " $str";
local c=$(echo "${array[0]}" | grep '-' -c)
if [ $c -eq 0 ]; then
shift
if [ $# -eq 0 ];then
IFS=$old_SEP
return
fi
local str="$(echo " $@"|sed -e 's/--/-_sep_/g' |sed -e 's/ -/§/g')"
# echo "str: $str"
IFS="§"; read -ra array <<< " $str";
fi
# echo "str: $str"
# echo "array: ${array[@]}"
for s in "${array[@]}"
do
# echo "0:s: $s"
if [ ! "$s" ] || [ "$s" = " " ]; then
continue
fi
local s="$(echo "$s"|xargs -d ' '| tr -d '\n')"
# echo "s: $s"
local s="$(echo "-$s" |sed -e 's/_sep_/-/g')"
# echo "s: $s"
arg=()
if [ $(grep '=' -c <<< $s) -gt 0 ]; then
IFS='='; read -ra arg <<< "$s"
# echo "eq.arg: { key: ${arg[0]}, value: ${arg[1]} }"
else
IFS=' '; read -ra arg_ <<< "$s"
arg[0]="${arg_[0]}"
arg[1]="${arg_[@]:1}"
# echo "space.arg: { key: ${arg[0]}, value: ${arg[1]} }"
fi
# echo "%%% ${arg[@]}"

local key="${arg[0]}"
# echo "key: $key"
if [ ! "$key" = "-n" ]; then
local key="$(echo "${arg[0]}"|xargs -d ' '| tr -d '\n')"
fi
local value="${arg[1]}"
# echo "key: $key, value: $value"

if [ ! "$value" ]; then
local c=$(echo "$s" | grep ' ' -c)
if [ $c -gt 0 ]; then
IFS=' ';read -ra arg <<< "$s"
local key="$(echo "${arg[0]}")"
local value="$(echo "$s"|sed -e "s/$key //g"|xargs -d ' '| tr -d '\n')"
fi
fi
local value="${value:-true}"
if [ "$key" ]; then
if [ "$key" != "-n" ]; then
key="$(echo "$key"|sed -e "s/---/--/g"|xargs -d ' '| tr -d '\n')"
fi
if [ "$key" != "-" ]; then
local value=$(echo "$value"|xargs -d ' '| tr -d '\n')
old_value=${parse_arguments_args["$key"]}
if [ "$old_value" != "$value" ]; then
if [ "$old_value" ] && [ "$value" != "true" ]; then
if [ ! "$argument_single_mode" ] && [ "$value" ]; then
value="$old_value,$value"
else
value="$old_value"
fi
fi
# if [ "$old_value" ]; then
# print_gray "old_value: [$old_value], value: [$value]"
# fi

parse_arguments_args["$key"]="$value"
fi
fi
fi
done
fi

get_arg verbose '--verbose'
if [ "$verbose" ]; then
debug="true"
for key in "${!parse_arguments_args[@]}";
do
key="${key}"
value="${parse_arguments_args[${key}]}"
done
fi
IFS=$old_SEP
}

get_arg()
{
local -n value="$1"
local old_SEP=$IFS
local global="true"
if [ "$4" ];then
local -n get_arg_args="$4"
fi

if [ "$5" ];then
local global="$5"
fi

if [ "$global" != "true" ]; then
value=""
fi

# echo "keys: $2"
IFS=","; read -ra keys <<< "$2";
for key in "${keys[@]}"
do
if [ ! "$key" = "-n" ]; then
key="$(echo "$key"|xargs -d ' '| tr -d '\n')"
fi
key_value=""
if [ ! "$4" ];then
key_value="${ARGS[$key]}"
else
key_value="${get_arg_args[$key]}"
fi
if [ "$key_value" ]; then
if [ ! "$argument_single_mode" ] && [ "$value" ] && [ "$value" != "true" ];then
value="$value,$key_value"
else
value="$key_value"
fi
fi
done

if [ ! "$value" ] && [ "$3" ]; then
value="$3"
fi
IFS=$old_SEP
if [ "$value" ] && [ "$1" != "debug" ] && [ "$1" != "verbose" ]; then
if [ "$global" == "true" ]; then
print_verbose "Loaded argument $1:'$value'"
fi
fi
}

fix_print_message()
{
msg="$(echo "$1"| sed -e 's/%/%%/g')"
Expand Down Expand Up @@ -738,18 +588,8 @@ git__setup()
}

if [ ! "$include_loaded" ]; then
declare -A ARGS

first_param="$3"
shift
parse_arguments ARGS $@

curr_dir="$(pwd)"
namespace_filter="-A"

get_arg silent '--silent'
get_arg verbose '--verbose'
get_arg debug '--debug' "$verbose"

include_main
fi
185 changes: 185 additions & 0 deletions smud-cli/include_args.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
#!/usr/bin/env bash

if [ "$_" = "/usr/bin/sh" ] || [ "$_" = "/bin/sh" ]; then
echo "Native '$_' not supported here :-("
echo "Please run this inside bash!"
exit
fi

parse_arguments()
{
local old_SEP=$IFS
local -n parse_arguments_args="$1"
# echo "0.str: $@"
if [ $# -gt 0 ];then
has_args="true"
local str="$(echo " $@"|sed -e 's/--/-_sep_/g' |sed -e 's/ -/§/g')"
# echo "str: $str"
IFS="§"; read -ra array <<< " $str";
local c=$(echo "${array[0]}" | grep '-' -c)
if [ $c -eq 0 ]; then
shift
if [ $# -eq 0 ];then
IFS=$old_SEP
return
fi
local str="$(echo " $@"|sed -e 's/--/-_sep_/g' |sed -e 's/ -/§/g')"
# echo "str: $str"
IFS="§"; read -ra array <<< " $str";
fi
# echo "str: $str"
# echo "array: ${array[@]}"
for s in "${array[@]}"
do
# echo "0:s: $s"
if [ ! "$s" ] || [ "$s" = " " ]; then
continue
fi
local s="$(echo "$s"|xargs -d ' '| tr -d '\n')"
# echo "s: $s"
local s="$(echo "-$s" |sed -e 's/_sep_/-/g')"
# echo "s: $s"
arg=()
if [ $(grep '=' -c <<< $s) -gt 0 ]; then
IFS='='; read -ra arg <<< "$s"
# echo "eq.arg: { key: ${arg[0]}, value: ${arg[1]} }"
else
IFS=' '; read -ra arg_ <<< "$s"
arg[0]="${arg_[0]}"
arg[1]="${arg_[@]:1}"
# echo "space.arg: { key: ${arg[0]}, value: ${arg[1]} }"
fi
# echo "%%% ${arg[@]}"

local key="${arg[0]}"
# echo "key: $key"
if [ ! "$key" = "-n" ]; then
local key="$(echo "${arg[0]}"|xargs -d ' '| tr -d '\n')"
fi
local value="${arg[1]}"
# echo "key: $key, value: $value"

if [ ! "$value" ]; then
local c=$(echo "$s" | grep ' ' -c)
if [ $c -gt 0 ]; then
IFS=' ';read -ra arg <<< "$s"
local key="$(echo "${arg[0]}")"
local value="$(echo "$s"|sed -e "s/$key //g"|xargs -d ' '| tr -d '\n')"
fi
fi
local value="${value:-true}"
if [ "$key" ]; then
if [ "$key" != "-n" ]; then
key="$(echo "$key"|sed -e "s/---/--/g"|xargs -d ' '| tr -d '\n')"
fi
if [ "$key" != "-" ]; then
local value=$(echo "$value"|xargs -d ' '| tr -d '\n')
old_value=${parse_arguments_args["$key"]}
if [ "$old_value" != "$value" ]; then
if [ "$old_value" ] && [ "$value" != "true" ]; then
if [ ! "$argument_single_mode" ] && [ "$value" ]; then
value="$old_value,$value"
else
value="$old_value"
fi
fi
# if [ "$old_value" ]; then
# print_gray "old_value: [$old_value], value: [$value]"
# fi

parse_arguments_args["$key"]="$value"
fi
fi
fi
done
fi

get_arg verbose '--verbose'
if [ "$verbose" ]; then
debug="true"
for key in "${!parse_arguments_args[@]}";
do
key="${key}"
value="${parse_arguments_args[${key}]}"
done
fi
IFS=$old_SEP
}

get_arg()
{
local -n value="$1"
local old_SEP=$IFS
local global="true"
if [ "$4" ];then
local -n get_arg_args="$4"
fi

if [ "$5" ];then
local global="$5"
fi

if [ "$global" != "true" ]; then
value=""
fi

# echo "keys: $2"
IFS=","; read -ra keys <<< "$2";
for key in "${keys[@]}"
do
if [ ! "$key" = "-n" ]; then
key="$(echo "$key"|xargs -d ' '| tr -d '\n')"
fi
key_value=""
if [ ! "$4" ];then
key_value="${ARGS[$key]}"
else
key_value="${get_arg_args[$key]}"
fi
if [ "$key_value" ]; then
if [ ! "$argument_single_mode" ] && [ "$value" ] && [ "$value" != "true" ];then
value="$value,$key_value"
else
value="$key_value"
fi
fi
done

if [ ! "$value" ] && [ "$3" ]; then
value="$3"
fi
IFS=$old_SEP
if [ "$value" ] && [ "$1" != "debug" ] && [ "$1" != "verbose" ]; then
if [ "$global" == "true" ]; then
print_verbose_args "Loaded argument $1:'$value'"
fi
fi
}
print_verbose_args()
{
if [ "$verbose" ]; then
if [ "$1" ]; then
printf "\x1b[1;90m$1$(tput sgr0)\n"
else
echo ""
fi
fi
}

if [ ! "$include_args_loaded" ]; then
declare -A ARGS

first_param="$3"
if [ "$startup_shift" ]; then
shift
fi

parse_arguments ARGS $@

get_arg silent '--silent'
get_arg verbose '--verbose'
get_arg debug '--debug' "$verbose"

IFS=$'\n'
include_args_loaded="true"
fi
4 changes: 3 additions & 1 deletion smud-cli/install-cli.sh
Original file line number Diff line number Diff line change
Expand Up @@ -73,5 +73,7 @@ if [ $HOMEDRIVE ] && [ -f $HOME_ABS_DIR/.bash_profile ]; then
elif [ -f $HOME_ABS_DIR/.bashrc ]; then
. ~/.bashrc
fi

if [ -f "$DEST_DIR/smud.sh" ]; then
chmod +x $DEST_DIR/smud.sh
fi
print_verbose "**** END: install-cli.sh"
4 changes: 4 additions & 0 deletions smud-cli/smud-main.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
#!/usr/bin/env bash

startup_shift="true"
. $(dirname "$0")/include_args.sh "$@"

if [ ! "$smud_main_loaded" ]; then

smud_main_loaded="true"
Expand Down
Empty file modified smud-cli/smud.sh
100644 → 100755
Empty file.

0 comments on commit f9c881e

Please sign in to comment.