Skip to content

Commit

Permalink
added disable auto export support
Browse files Browse the repository at this point in the history
  • Loading branch information
hattan committed Nov 14, 2024
1 parent b5352ba commit 54aea5c
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 3 deletions.
1 change: 1 addition & 0 deletions examples/simple_example.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ shArgs.arg "DEBUG" -d --debug
shArgs.parse "$@"

echo "The message is $MESSAGE"
echo "The message is $(shArgs.val "MESSAGE")"

if [ "$DEBUG" == true ]; then
echo "DEBUG is true!"
Expand Down
12 changes: 11 additions & 1 deletion scripts/shArg.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ declare -A _SH_SWITCHES=()
declare -A _SH_TYPES=()
declare -A _SH_AUTOS=()
declare -A _SH_HOOK_FUNCTIONS=()
declare AUTO_EXPORTS=true
shArgs.arg(){
local variableName=$1
local shortName=$2
Expand Down Expand Up @@ -88,6 +89,11 @@ _processFlag() {
_SH_ARGUMENTS[$_varName]=$_varValue

if [ "$autoExport" == true ]; then
existing=$(eval "echo \$$_varName")
if [ -n "$existing" ]; then
_warn "autoexported variable \"$_varName\" contains a value and will be overridden by shArg"
fi

eval "$_varName=\"$_varValue\""
fi
}
Expand All @@ -102,7 +108,7 @@ _processSplitData() {
_varName=${_SH_SWITCHES[$name]}
if [ ! -z "$_varName" ]; then
argType=${_SH_TYPES[$_varName]}
autoExport=${_SH_AUTOS[$_varName]}
autoExport="$AUTO_EXPORTS"
hookFunction=${_SH_HOOK_FUNCTIONS[$_varName]}
if [ -z "$value" ]; then
_processFlag "$_varName" "$autoExport"
Expand Down Expand Up @@ -195,4 +201,8 @@ _warn(){
if [ -z "$SHARG_DISABLE_WARNINGS" ]; then
echo -e "\033[33m@shArg Warning: $1\033[0m"
fi
}

shArgs.disableAutoExport(){
AUTO_EXPORTS=false
}
1 change: 1 addition & 0 deletions spec/unit/equals_assignment_spec.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Describe 'shArg'
shArgs.arg "FILE" -f --file #5
shArgs.arg "URL" -l --url urlHook #6
URL_HOOK_CALLED=false
SHARG_DISABLE_WARNINGS=true
}
BeforeEach 'setup'

Expand Down
2 changes: 1 addition & 1 deletion spec/unit/shArgs.val_spec.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Describe 'shArg'
shArgs.arg "USER" -u
shArgs.arg "OUTPUT" --output
shArgs.arg "FILE" -f --file

SHARG_DISABLE_WARNINGS=true
shArgs.parse --message "efghij" -u picard --output json -f input.dat -d
}
BeforeEach 'setup'
Expand Down
32 changes: 31 additions & 1 deletion spec/unit/shArgs_parse_spec.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@ Context "Argument Parsing"
shArgs.arg "URL" -l --url urlHook #6
shArgs.arg "NAME" -nm --name #7
shArgs.arg "RESOURCE_GROUP" -rgp --resource-group #8
shArgs.arg "LABEL" --label #9
URL_HOOK_CALLED=false
unset "USER"
}

BeforeEach 'setup'

It 'should parse an argument with short name (#1)'
Expand Down Expand Up @@ -100,7 +103,32 @@ Context "Argument Parsing"
It 'should display a warning if an unknown flag is passed in'
When call shArgs.parse --fake-flag
The output should include "@shArg Warning: Unknown argument --fake-flag"
End
End

label_setup(){
export LABEL="hello"
}
label_cleanup(){
unset "LABEL"
}
Before 'label_setup'
It 'should display a warning if an existing variable is overridden'
When call shArgs.parse --label "hello"
The output should include "@shArg Warning: autoexported variable \"LABEL\" contains a value and will be overridden by shArg"
End
After 'label_cleanup'

export_setup(){
#shArgs.disableAutoExport
shArgs.arg "TYPE" --type #9
}
Before 'export_setup'
It 'should not auto export variables if shArgs.disableAutoExport is configured'
When call shArgs.parse --type "macos"
The value "$TYPE" should not equal ""
The value "$TYPE" should equal "macos"
End

End

Context "Argument Parsing (Legacy)"
Expand All @@ -120,6 +148,8 @@ Context "Argument Parsing"
shArgs.arg "NAME" -nm --name PARAMETER true #7
shArgs.arg "RESOURCE_GROUP" -rgp --resource-group PARAMETER true #8
URL_HOOK_CALLED=false
unset "USER"
unset "LABEL"
}
BeforeEach 'setup_legacy'

Expand Down

0 comments on commit 54aea5c

Please sign in to comment.