Hi! I am trying to create a macro in zsh using global aliases: ``` alias -g MAGIC=')"' function m_doc() { print -r -- "! { [[ ${(q+)1} == '-h' ]] || [[ ${(q+)1} == '--help' ]] } || { print -r -- \"Help for $2: $@[3,-1]\” ; return 0 }" } alias mdoc='eval "$(m_doc "$*" "$0" ' ## Testing it mdoc_test() { mdoc Usage: Some documentation here. MAGIC echo “You shouldn’t see this if using the help flags" } ## Running this: #$ mdoc_test -h #mdoc_test:1: parse error near `)' #mdoc_test:1: parse error in command substitution mdoc_test2() { eval "$(m_doc "$*" "$0" Usage: Some documentation here.)" ec "$*" } ## Running this: #$ mdoc_test2 -h 1 #Help for mdoc_test2: #Usage: Some documentation here. mdoc_test3() { eval "$(m_doc "$*" "$0" Usage: Some documentation here. MAGIC ec "$*" } ## Running this: #$ mdoc_test3 -h #mdoc_test3:1: parse error near `)' #mdoc_test3:1: parse error in command substitution ``` It seems to me that this is a bug?
On Mon, Aug 12, 2019 at 1:55 AM Aryn Starr <whereislelouch@icloud.com> wrote:
>
> alias -g MAGIC=')"'
> alias mdoc='eval "$(m_doc "$*" "$0" '
>
> mdoc Usage: Some documentation here. MAGIC
You've created an ambiguous "sentence". Aliases are not really
macros; they are both expanded and parsed left-to-right, not fully
expanded before parsing begins. The ambiguity is that because you've
hidden the closing paren from the parser, it follows through the
depth-first interpretation rather than the breadth-first.
First mdoc expands, and then that expansion is parsed, so now the
parser is now working inside the command substitution, and when MAGIC
expands the shell sees it as part of the m_doc command rather than as
part of the mdoc command, if you see what I mean.
I think you can unambiguously get what you're after by using
alias mdoc='m_doc '
alias -g MAGIC='| { eval "$(read -d "" -r -E)" }'