Skip to content

Instantly share code, notes, and snippets.

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}"
unset -v ret
typeset -A optnames=(
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
compgen -A "${BASH_REMATCH[1]}" | mapfile -t -- "${!arg}" || return
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.