I'm writing a function that will make a REST API calls which could be either GET, PUT, DELETE, POST, etc.
I would like to feed this method to the function as a parameter and add it to the options array for that single function call. Is this possible?
Currently I am solving this by creating a separate local array but would prefer to only use the single options array.
#!/bin/bash
options=(
--user me:some-token
-H "Accept: application/json"
)
some_func () {
local urn=$1
shift
local func_opts=("${options[@]}" "$@")
printf '%s\n' "${func_opts[@]}"
}
# This should return all options including -X GET
some_func /test -X GET
# This should return only the original options
printf '%s\n' "${options[@]}"
I could also use a temporary array to store the contents of options, add the new options, and then reset it before the function ends, but I don't think that is a particularly clean method either.