* Problem with a substitution
@ 2019-03-17 2:55 Sebastian Gniazdowski
2019-03-17 3:33 ` Sebastian Gniazdowski
0 siblings, 1 reply; 2+ messages in thread
From: Sebastian Gniazdowski @ 2019-03-17 2:55 UTC (permalink / raw)
To: Zsh Users
Hello,
I think that the parsing of command line can be performed by 1-2
substitutions. Below is the one that I'm currently working on:
${(j::)${@[@]//(#b)(--quiet|-q|--force|-f)[[:blank:]=]#(([a-zA-Z-]#)~(--quiet|-q|--force|-f))#/${TARGET[${match[1]}]::=${match[1]}//${${match[2]:+${match[2]}}:-1}
}}}
Explanation:
${(j::)${@[@]//
# Work on the command line arguments – substitute sub-strings (i.e.
the "//" substitution)
(#b)(--quiet|-q|--force|-f)
# Enable $match array (the #b); match 4 different option cases
[[:blank:]=]#
# Optional before-option's-value space and =
(([a-zA-Z-]#)~(--quiet|-q|--force|-f))#
# Optional value – a string from letters and dashes, but not any of
the 4 option-names
/
# Substitute to:
${TARGET[${match[1]}]::=${match[1]}
# option name, also assigning it to the TARGET hash (the ::=)
//${${match[2]:+${match[2]}}:-1} }}}
# After-//, also assign optional option's value (using "1" if there's no value)
So, for the following input:
set -- --quiet -fa -q
local -A TARGET
the output of:
print -r -- ${(j::)${@[@]//(#b)(--quiet|-q|--force|-f)[[:blank:]=]#(([a-zA-Z-]#)~(-q|--force|-f))#/${TARGET[${match[1]}]::=${match[1]}//${${match[2]:+${match[2]}}:-1}
}}}
is:
--quiet//1 -f//1 -q//1
and for:
print -rl -- ${(kv)TARGET}
it is (keys and values alternating):
-q
-q//1
--quiet
--quiet//1
-f
-f//1
I.e.: there's no "-f//a" string in it. Why?
--
Sebastian Gniazdowski
News: https://twitter.com/ZdharmaI
IRC: https://kiwiirc.com/client/chat.freenode.net:+6697/#zplugin
Blog: http://zdharma.org
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: Problem with a substitution
2019-03-17 2:55 Problem with a substitution Sebastian Gniazdowski
@ 2019-03-17 3:33 ` Sebastian Gniazdowski
0 siblings, 0 replies; 2+ messages in thread
From: Sebastian Gniazdowski @ 2019-03-17 3:33 UTC (permalink / raw)
To: Zsh Users
It turned out that it was `#'-repeat-0-or-more the cause, changing it
to `##'-repeat-1-or-more solved the problem. It's the [a-zA-Z-]## in
the part below:
...[[:blank:]=]#(([a-zA-Z-]##)~(-q|--force|-f))#...
I wonder why such effect?
On Sun, 17 Mar 2019 at 03:55, Sebastian Gniazdowski
<sgniazdowski@gmail.com> wrote:
>
> Hello,
> I think that the parsing of command line can be performed by 1-2
> substitutions. Below is the one that I'm currently working on:
>
> ${(j::)${@[@]//(#b)(--quiet|-q|--force|-f)[[:blank:]=]#(([a-zA-Z-]#)~(--quiet|-q|--force|-f))#/${TARGET[${match[1]}]::=${match[1]}//${${match[2]:+${match[2]}}:-1}
> }}}
>
> Explanation:
>
>
> ${(j::)${@[@]//
> # Work on the command line arguments – substitute sub-strings (i.e.
> the "//" substitution)
>
> (#b)(--quiet|-q|--force|-f)
> # Enable $match array (the #b); match 4 different option cases
>
> [[:blank:]=]#
> # Optional before-option's-value space and =
>
> (([a-zA-Z-]#)~(--quiet|-q|--force|-f))#
> # Optional value – a string from letters and dashes, but not any of
> the 4 option-names
>
> /
> # Substitute to:
>
> ${TARGET[${match[1]}]::=${match[1]}
> # option name, also assigning it to the TARGET hash (the ::=)
>
> //${${match[2]:+${match[2]}}:-1} }}}
> # After-//, also assign optional option's value (using "1" if there's no value)
>
> So, for the following input:
> set -- --quiet -fa -q
> local -A TARGET
>
> the output of:
> print -r -- ${(j::)${@[@]//(#b)(--quiet|-q|--force|-f)[[:blank:]=]#(([a-zA-Z-]#)~(-q|--force|-f))#/${TARGET[${match[1]}]::=${match[1]}//${${match[2]:+${match[2]}}:-1}
> }}}
>
> is:
> --quiet//1 -f//1 -q//1
>
> and for:
> print -rl -- ${(kv)TARGET}
>
> it is (keys and values alternating):
> -q
> -q//1
> --quiet
> --quiet//1
> -f
> -f//1
>
> I.e.: there's no "-f//a" string in it. Why?
>
> --
> Sebastian Gniazdowski
> News: https://twitter.com/ZdharmaI
> IRC: https://kiwiirc.com/client/chat.freenode.net:+6697/#zplugin
> Blog: http://zdharma.org
--
Sebastian Gniazdowski
News: https://twitter.com/ZdharmaI
IRC: https://kiwiirc.com/client/chat.freenode.net:+6697/#zplugin
Blog: http://zdharma.org
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2019-03-17 3:34 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-03-17 2:55 Problem with a substitution Sebastian Gniazdowski
2019-03-17 3:33 ` Sebastian Gniazdowski
Code repositories for project(s) associated with this public inbox
https://git.vuxu.org/mirror/zsh/
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).