Created
September 17, 2016 14:43
-
-
Save ormaaj/6dc167dce4f931f4866f4b87118200f0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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