Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Problem with a substitution



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



Messages sorted by: Reverse Date, Date, Thread, Author