mirror of
https://github.com/flatcar/scripts.git
synced 2025-09-27 16:41:11 +02:00
pkg-auto: Add function for declaring structs
Declaring structs differs a bit from declaring typical variables in that it takes one initializer and applies it to all the declared variables. Will be used a lot by upcoming libraries.
This commit is contained in:
parent
3931cbff5f
commit
b52676a64b
@ -324,4 +324,28 @@ function gen_varname() {
|
||||
__UTIL_SH_COUNTER=$((__UTIL_SH_COUNTER + 1))
|
||||
}
|
||||
|
||||
# Declares variables with a given initializer.
|
||||
#
|
||||
# Params:
|
||||
#
|
||||
# @: flags passed to declare, followed by variable names, followed by
|
||||
# an initializer
|
||||
function struct_declare() {
|
||||
local -a args=()
|
||||
while [[ $# -gt 0 ]]; do
|
||||
if [[ ${1} != -* ]]; then
|
||||
break
|
||||
fi
|
||||
args+=( "${1}" )
|
||||
shift
|
||||
done
|
||||
if [[ ${#} -lt 2 ]]; then
|
||||
fail "bad use of struct_declare"
|
||||
fi
|
||||
local definition=${*: -1}
|
||||
set -- "${@:1:$((${#} - 1))}"
|
||||
set -- "${@/%/=${definition}}"
|
||||
declare "${args[@]}" "${@}"
|
||||
}
|
||||
|
||||
fi
|
||||
|
Loading…
x
Reference in New Issue
Block a user