Add functions for handling defined functions and variables

This commit is contained in:
Eric Levy 2022-01-24 01:09:19 -05:00
parent e42e4fce94
commit 2f382c5efe

54
mo
View File

@ -1128,6 +1128,10 @@ moTrimWhitespace
moUsage
moListFuncs
moListVars
moUnload
moExport
moUnexport
moDeclare
EOF
}
@ -1139,13 +1143,63 @@ moListVars() {
cat <<EOF
MO_ORIGINAL_COMMAND
MO_VERSION
MO_EXPORT
EOF
}
# Clear all shell functions and variables for names used by mo,
# restoring the shell to a state as though mo had never been sourced
# (assuming no naming collisions).
moUnload() {
unset -v $(moListVars)
unset -f $(moListFuncs)
}
# Mark for export all shell functions and variables used by mo, such
# that any child (or descendant) Bash process may call mo as though
# itself having sourced the file.
moExport() {
export $(moListVars)
export -f $(moListFuncs)
MO_EXPORT=1
}
# Unmark for export all shell functions and variables used by, such
# that any new child (or descendant) Bash process would not inherit
# them, regardless of any past operations.
moUnexport() {
export -n $(moListVars)
export -fn $(moListFuncs)
MO_EXPORT=
}
# Print declaration of all shell functions and variables used by mo,
# suitable for processing by another Bash shell. Any Bash instance
# processing the output of this function may afterward call mo as
# though itself having sourced the file.
moDeclare() {
local functions="$(moListFuncs)"
if ! [ -z "${functions}" ]; then
declare -fp ${functions}
fi
local vars="$(moListVars)"
if ! [ -z "${vars}" ]; then
declare -p ${vars}
fi
if ! [ -z "${MO_EXPORT}" ]; then
echo moExport
fi
}
# Save the original command's path for usage later
MO_ORIGINAL_COMMAND="$(cd "${BASH_SOURCE[0]%/*}" || exit 1; pwd)/${BASH_SOURCE[0]##*/}"
MO_VERSION="2.2.0"
MO_EXPORT=
# If sourced, load all functions.
# If executed, perform the actions as expected.