Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
function introspect {
if [[ ${FUNCNAME[0]} != "${FUNCNAME[1]}" ]]; then
typeset ret
if ! shopt -qo pipefail; then shopt -so pipefail; ret+='shopt -uo pipefail; '; fi
if shopt -qo monitor; then shopt -uo monitor; ret+='shopt -so monitor; '; fi
if ! shopt -q lastpipe; then shopt -s lastpipe; ret+='shopt -u lastpipe; '; fi
eval "\\${FUNCNAME[0]} \"\$@\"; ${ret}"
fi
unset -v ret
typeset -A optnames=(
[alias]=
[arrayvar]=
[binding]=
[builtin]=
[command]=
[directory]=
[disabled]=
[enabled]=
[export]=
[file]=
[function]=
[group]=
[helptopic]=
[hostname]=
[job]=
[keyword]=
[running]=
[service]=
[setopt]=
[shopt]=
[signal]=
[stopped]=
[user]=
[variable]=
)
typeset opt=1 arg=2
for ((; opt <= $#; arg = ((opt += 2) + 1) )); do
if [[ ! ( ${!opt} =~ ^--([[:alpha:]]+)$ && -v optnames[${BASH_REMATCH[1]}] ) ]]; then
printf 'invalid option: %s\n' "${!opt}" >&2
return 1
fi
compgen -A "${BASH_REMATCH[1]}" | mapfile -t -- "${!arg}" || return
done
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.