Add list-envvars.zsh

This commit is contained in:
2025-11-28 01:00:38 -08:00
parent 6da1ad41b5
commit 69654a19b9

43
list-envvars.zsh Executable file
View File

@ -0,0 +1,43 @@
#!/usr/bin/env zsh
print_help_and_exit() {
echo "Usage: list-envvars.zsh [-h] [-z] <SCRIPT>"
echo " List the environment variables exported by SCRIPT, which is run in the"
echo " process. SCRIPT must be a zsh script as it will be sourced by this script. It"
echo " also mustn't use the DEBUG trap, as this script uses it."
echo "Options:"
echo " -h Print this message, then exit"
echo " -z Use a NULL byte instead of a newline to delimit output"
exit 0
}
(( ${#} == 0 )) && print_help_and_exit
local line_delim='\n'
while getopts 'hz' OPTOPT; do
case "${OPTOPT}" in
h)
print_help_and_exit
;;
z)
line_delim='\0'
;;
esac
done
local target_script="${@[${OPTIND}]}"
local found_vars=()
handle_debug_trap() {
local cmdline=(${(Q)${(Z:C:)1}})
[[ "${cmdline[1]}" = 'export' ]] || return
found_vars+=(${${cmdline:1}/=*})
}
() {
emulate -L zsh
trap 'handle_debug_trap "${ZSH_DEBUG_CMD}"' DEBUG
source "${target_script}" >/dev/null
}
printf "%s${line_delim}" ${found_vars}