This was conceived out of a desire to avoid if [ -z $1 ]; then echo "SomeVar required"; exit 1; fi
for all the positionals
since the number I want to handle might change. Eventually it may handle shift
and named or -
or --
delimited arguments,
but we are starting of simple slow.
Created
August 31, 2018 13:24
-
-
Save espoelstra/ac3f905f5812b53ba4a35551df44cfe0 to your computer and use it in GitHub Desktop.
As a Bash scripter, I want to be able to handle required positional parameters without updating my $# compare to see if enough were supplied constantly
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -Eeu | |
args () { | |
prefix=ARG_ | |
ARG_FIRST=$1 | |
ARG_SECOND=$2 | |
ARG_ETC=$3 | |
for eacharg in "${varargs[@]}"; do arglist+=(${eacharg/ARG_/}=${!eacharg}); done | |
} | |
# This takes all the arguments to the script and passes to the function to handle assigning to variables | |
args "$@" | |
# Source the list we assembled, functionally the same as `eval "${arglist[@]}"` | |
. <(${arglist[@]}) | |
empty_function () { | |
echo $FIRST | |
echo $SECOND | |
} | |
# This prints the values that should be inherited from the parent/script scope which should resolve to the arguments to the test-script.sh | |
empty_function | |
new_function () { | |
echo $1 | |
echo $2 | |
} | |
# This prints the values passed into it which should resolve to the arguments to the magic-args.sh | |
new_function $FIRST $SECOND |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment