diff --git a/examples/simple_example.sh b/examples/simple_example.sh index d077ffe..d9ee60f 100755 --- a/examples/simple_example.sh +++ b/examples/simple_example.sh @@ -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!" diff --git a/scripts/shArg.sh b/scripts/shArg.sh index 6f6266a..c5a80f3 100755 --- a/scripts/shArg.sh +++ b/scripts/shArg.sh @@ -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 @@ -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 } @@ -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" @@ -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 } \ No newline at end of file diff --git a/spec/unit/equals_assignment_spec.sh b/spec/unit/equals_assignment_spec.sh index 0e342ab..e1644b3 100644 --- a/spec/unit/equals_assignment_spec.sh +++ b/spec/unit/equals_assignment_spec.sh @@ -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' diff --git a/spec/unit/shArgs.val_spec.sh b/spec/unit/shArgs.val_spec.sh index a5e3cb1..58247c2 100644 --- a/spec/unit/shArgs.val_spec.sh +++ b/spec/unit/shArgs.val_spec.sh @@ -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' diff --git a/spec/unit/shArgs_parse_spec.sh b/spec/unit/shArgs_parse_spec.sh index 8c5483a..7d75d99 100644 --- a/spec/unit/shArgs_parse_spec.sh +++ b/spec/unit/shArgs_parse_spec.sh @@ -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)' @@ -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)" @@ -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'