Skip to content

Commit

Permalink
Merge pull request #7 from hattan/parse_v2
Browse files Browse the repository at this point in the history
Parse v2
  • Loading branch information
hattan authored Mar 8, 2021
2 parents 26a6195 + 2b0e0fe commit 6d56f52
Show file tree
Hide file tree
Showing 14 changed files with 646 additions and 253 deletions.
158 changes: 117 additions & 41 deletions scripts/shArg.sh
Original file line number Diff line number Diff line change
Expand Up @@ -43,51 +43,127 @@ shArgs.arg(){
fi
}

shArgs.parse(){
_strindex() {
x="${1%%$2*}"
[[ "$x" = "$1" ]] && echo -1 || echo "${#x}"
}

_processFlag() {
local _varName=$1
local autoExport=$2
_SH_ARGUMENTS[$_varName]=true
if [ "$autoExport" == true ]; then
eval "$_varName=true"
fi
}

_processParameter() {
local _varValue=1
local value=$2
local autoExport=$3

if [[ "$value" == *","* ]]; then
local _listInput=$value
local arr
BFS=$IFS
IFS=, read -a arr <<<"${_listInput}"
IFS=$BFS
listData="${arr[@]}"
_varValue=$listData
_SH_ARGUMENTS[$_varName]="$listData"
else
_varValue=$value
fi

_SH_ARGUMENTS[$_varName]=$_varValue

if [ "$autoExport" == true ]; then
eval "$_varName=\"$_varValue\""
fi
}

_processSplitData() {
local name=$1
local value=$2
local _varName
local argType
local autoExport
while [[ "$#" -gt 0 ]]
do
case $1 in
*)
_varName=${_SH_SWITCHES[$1]}

if [ ! -z "$_varName" ]; then
argType=${_SH_TYPES[$_varName]}
autoExport=${_SH_AUTOS[$_varName]}
hookFunction=${_SH_HOOK_FUNCTIONS[$_varName]}
if [ "$argType" == "FLAG" ]; then
_SH_ARGUMENTS[$_varName]=true
if [ "$autoExport" == true ]; then
eval "$_varName=true"
fi
else
local _varValue=""
if [[ "$2" == *","* ]]; then
local _listInput=$2
local arr
IFS=, read -a arr <<<"${_listInput}"
listData="${arr[@]}"
_varValue=$listData
_SH_ARGUMENTS[$_varName]="$listData"
else
_varValue=$2
fi
_SH_ARGUMENTS[$_varName]=$_varValue
if [ "$autoExport" == true ]; then
eval "$_varName=\"$_varValue\""
fi
shift
fi
if [ ! -z "$hookFunction" ]; then
eval "$hookFunction \"${_SH_ARGUMENTS[$_varName]}\""
fi
fi
esac
shift
done

_varName=${_SH_SWITCHES[$name]}
if [ ! -z "$_varName" ]; then
argType=${_SH_TYPES[$_varName]}
autoExport=${_SH_AUTOS[$_varName]}
hookFunction=${_SH_HOOK_FUNCTIONS[$_varName]}
if [ "$argType" == "FLAG" ]; then
_processFlag "$_varName" "$autoExport"
else
_processParameter "$_varName" "$value" "$autoExport"
fi
if [ ! -z "$hookFunction" ]; then
eval "$hookFunction \"${_SH_ARGUMENTS[$_varName]}\""
fi
fi
}

_trim() {
local value=$1
echo $value | xargs
}

_processLine(){
local input_string=$1
local firstChar="${input_string:0:1}"
if [ "$firstChar" != "-" ]; then
return 0 # not a switch exit
fi

local size=${#input_string}
local spaceIndex=$(_strindex "$input_string" " ")
local name=${input_string:0:spaceIndex}
local value=${input_string:spaceIndex+1:size}
local valueSize=${#input_string}
value=$(_trim "$value")
if [ "$spaceIndex" == "-1" ]; then
name=$value
value=""
fi

if [[ "$name" == *"="* ]]; then
name=${input_string:0:size}
BFS=$IFS
IFS='='
read -ra arr <<<"$name"
IFS=$BFS
name=${arr[0]}
value=${arr[1]}
value=$(_trim "$value")
fi
_processSplitData "$name" "$value"
}

shArgs.parse(){
local input_string=$@
local char=""
local tmp=""

for (( i=0; i<${#input_string}; i++ )); do
char=${input_string:$i:1}
if [ "$char" == "-" ]; then
if [ ! -z "$tmp" ]; then
_processLine "$tmp"
tmp=""
fi
if [ "${input_string:$i:2}" == "--" ]; then
tmp+="$char"
i=$((i+1))
fi
fi
tmp+="$char"
done
if [ ! -z "$tmp" ]; then
_processLine "$tmp"
tmp=""
fi
}

shArgs.val(){
Expand Down
20 changes: 20 additions & 0 deletions spec/integration/harness/hook.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/usr/bin/env bash

source scripts/shArg.sh

declare MESSAGE

messageHook() {
local message=$1

echo "message hooked invoked with $message"
echo "Global variable is still available $MESSAGE"
}

# register arguments
shArgs.arg "MESSAGE" -m --message PARAMETER true messageHook

# parse inputs
shArgs.parse $@


12 changes: 12 additions & 0 deletions spec/integration/harness/list.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/usr/bin/env bash

source scripts/shArg.sh

declare IPS
shArgs.arg "IPS" -i --ips PARAMETER true
shArgs.parse $@

for ip in $IPS
do
echo "ip: $ip"
done
8 changes: 8 additions & 0 deletions spec/integration/harness/runner.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env bash

run_file() {
local file=$1
local input=$2
output=$(bash $file $input)
echo $output
}
22 changes: 22 additions & 0 deletions spec/integration/harness/simple.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/env bash

# load shArg
source scripts/shArg.sh

declare MESSAGE
declare DEBUG

# register arguments
shArgs.arg "MESSAGE" -m --message PARAMETER true
shArgs.arg "DEBUG" -d --debug FLAG true

# parse inputs
shArgs.parse $@

echo "The message is $MESSAGE"

if [ "$DEBUG" == true ]; then
echo "DEBUG is true!"
else
echo "DEBUG is false"
fi
11 changes: 11 additions & 0 deletions spec/integration/hook_spec.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Describe 'shArg'
Include spec/integration/harness/runner.sh

Context "Simple Script Tests"
It 'should parse a csv parameter'
When call run_file "spec/integration/harness/hook.sh" "-m great123"
The output should equal "message hooked invoked with great123 Global variable is still available great123"
End

End
End
16 changes: 16 additions & 0 deletions spec/integration/list_spec.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
Describe 'shArg'
Include spec/integration/harness/runner.sh

Context "Simple Script Tests"
It 'should parse a csv parameter'
When call run_file "spec/integration/harness/list.sh" "-i 123,456"
The output should equal "ip: 123 ip: 456"
End

It 'should parse a space delimited parameter'
When call run_file "spec/integration/harness/list.sh" "-i 888 777"
The output should equal "ip: 888 ip: 777"
End

End
End
95 changes: 95 additions & 0 deletions spec/integration/simple_spec.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
Describe 'shArg'
Include spec/integration/harness/runner.sh

Context "Simple Script Tests"
It 'should parse a parameter with short name and no quotes'
When call run_file "spec/integration/harness/simple.sh" "-m hello"
The output should equal "The message is hello DEBUG is false"
End

It 'should parse a parameter with short name, no quotes and equals assignment'
When call run_file "spec/integration/harness/simple.sh" "-m=hello"
The output should equal "The message is hello DEBUG is false"
End

It 'should parse a parameter with short name and double quotes'
When call run_file "spec/integration/harness/simple.sh" "-m \"world\""
The output should equal "The message is world DEBUG is false"
End

It 'should parse a parameter with short name, double quotes and equals assignment'
When call run_file "spec/integration/harness/simple.sh" '-m="hello"'
The output should equal "The message is hello DEBUG is false"
End

It 'should parse a parameter with short name and single quotes'
When call run_file "spec/integration/harness/simple.sh" "-m 'world'"
The output should equal "The message is world DEBUG is false"
End

It 'should parse a parameter with short name, single quotes and equals assignment'
When call run_file "spec/integration/harness/simple.sh" "-m='hello'"
The output should equal "The message is hello DEBUG is false"
End

It 'should parse a parameter with long name and no quotes'
When call run_file "spec/integration/harness/simple.sh" "--message hello"
The output should equal "The message is hello DEBUG is false"
End

It 'should parse a parameter with long name, no quotes and equals assignment'
When call run_file "spec/integration/harness/simple.sh" "--message=hello"
The output should equal "The message is hello DEBUG is false"
End

It 'should parse a parameter with long name and double quotes'
When call run_file "spec/integration/harness/simple.sh" "--message \"world\""
The output should equal "The message is world DEBUG is false"
End

It 'should parse a parameter with long name, double quotes and equals assignment'
When call run_file "spec/integration/harness/simple.sh" '--message="hello"'
The output should equal "The message is hello DEBUG is false"
End

It 'should parse a parameter with long name and single quotes'
When call run_file "spec/integration/harness/simple.sh" "--message 'world'"
The output should equal "The message is world DEBUG is false"
End

It 'should parse a parameter with long name, single quotes and equals assignment'
When call run_file "spec/integration/harness/simple.sh" "--message='hello'"
The output should equal "The message is hello DEBUG is false"
End

It 'should parse a parameter with long name and spaces. Single param only'
When call run_file "spec/integration/harness/simple.sh" "--message=hi there"
The output should equal "The message is hi there DEBUG is false"
End

It 'should parse a parameter with long name and spaces. Multiple params'
When call run_file "spec/integration/harness/simple.sh" "--message=hi there -d"
The output should equal "The message is hi there DEBUG is true!"
End

It 'should parse a parameter with short name and spaces. Equals assignment. Single param only'
When call run_file "spec/integration/harness/simple.sh" "-m=hi there"
The output should equal "The message is hi there DEBUG is false"
End

It 'should parse a parameter with long name and spaces. Equals assignment. Multiple params'
When call run_file "spec/integration/harness/simple.sh" "-m=hi there -d"
The output should equal "The message is hi there DEBUG is true!"
End

It 'should parse a parameter with short name and spaces. Positional assignment. Single param only'
When call run_file "spec/integration/harness/simple.sh" "-m hi there"
The output should equal "The message is hi there DEBUG is false"
End

It 'should parse a parameter with long name and spaces. Positional assignment. Multiple params'
When call run_file "spec/integration/harness/simple.sh" "-m hi there -d"
The output should equal "The message is hi there DEBUG is true!"
End
End
End
Loading

0 comments on commit 6d56f52

Please sign in to comment.