Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Problem with a substitution
- X-seq: zsh-users 23877
- From: Sebastian Gniazdowski <sgniazdowski@xxxxxxxxx>
- To: Zsh Users <zsh-users@xxxxxxx>
- Subject: Problem with a substitution
- Date: Sun, 17 Mar 2019 03:55:27 +0100
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:from:date:message-id:subject:to :content-transfer-encoding; bh=VO15GqwtLfSSmyF68HYYvPvPHzCYLXFFHN3TamUFTlE=; b=qFU0nF8zFCjhl6fsaMNAYrogi2oqDTke2Zhv0ChMpbDY2Mz4wNhOWNNVuK33LOW+lF FrjMAEw6qI9CDXMT4SURhfaaKNjYLICiFGCXID6SEVutmqq3+QXz7SWKOTx+jE8A4fMS WEc1gDH+ydiRyXOw6iI5egNgGX9A9Ed8uqGvNXqpcyZ/B4W2Yh0ymA/fhwec5YQX6pkJ VvkFfFtFnlqkj1f0V/jJRvIQrQXdi0WQroRHii2orW1sYvmCDNH8Y/Qb61EREoXOYrKU 9BZa1Gr9g3Bq/NGuQan04sFCU+HQCFMjMWIxiImpQJGq9yUEuKm6Z0eKjqA40LFEL6GC jBnw==
- List-help: <mailto:zsh-users-help@zsh.org>
- List-id: Zsh Users List <zsh-users.zsh.org>
- List-post: <mailto:zsh-users@zsh.org>
- List-unsubscribe: <mailto:zsh-users-unsubscribe@zsh.org>
- Mailing-list: contact zsh-users-help@xxxxxxx; run by ezmlm
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