Skip to content

Instantly share code, notes, and snippets.

@ormaaj
Created April 14, 2023 14:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ormaaj/18fc078d54e04174a9177d545c68bd35 to your computer and use it in GitHub Desktop.
Save ormaaj/18fc078d54e04174a9177d545c68bd35 to your computer and use it in GitHub Desktop.
function mapfile {
typeset x IFS REPLY OPTARG OPTIND=1
typeset -A ind opts=([c]=5000)
integer ind o
for x; do
getopts :d:n:O:s:u:C:c:t x || break
case $x in
\?|:) return 1 ;;
*) opts[$x]=$OPTARG ind[$x]=OPTIND
esac
done
(( OPTIND > $# )) && set -- "$@" MAPFILE
eval nameref ret=\$$OPTIND
getAttrs -v x ret
if [[ $x != *([ailux]) ]]; then
printf '%s: %s: %s\n' "$FUNCNAME" "${!ret}" 'Type error.' >&2
return 1
fi
[[ $x == *a* ]] || typeset -a ret
(( o = (opts[O] - 1) ))
IFS=
while
(( ++o ))
${bool[${opts[s]+1-}1]+\:} let 'opts[s]--' || unset -v 'opts[s]'
${bool[${opts[n]+1-}1]+\:} let 'opts[n]--' || break
read -r ${opts[d]+'-d'} ${opts[d]+"${opts[d]}"} ${bool[${opts[s]+1-}1]+'ret[o]'}
do [[ -v opts[t] ]] || ret[o]+=${opts[d]-$'\n'}
done <&"${opts[u]:-0}"
unset -v 'ret[o]'
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment