zsh-users
 help / color / mirror / code / Atom feed
* A minor syntax question
@ 2016-06-07  6:58 Jesper Nygårds
  2016-06-07 11:54 ` Oliver Kiddle
  0 siblings, 1 reply; 3+ messages in thread
From: Jesper Nygårds @ 2016-06-07  6:58 UTC (permalink / raw)
  To: Zsh Users

[-- Attachment #1: Type: text/plain, Size: 1174 bytes --]

I am writing a function that's basically a wrapper to an invocation of
'find'. I have the need to transform some arguments to my function into an
expression I pass on to 'find'. Here's a simplified example of what I want
to do:

xff() {
    zparseopts -D -E t+:=types || return 1
    typearg="("${"$(print -- '-o -type '${^types:#-t})"#-o }")"
    print $typearg
}

What I want to do is to construct a nested expression for the variable
number of file types that can be given. They should be contained within
parenthesis, and joined by "-o". So, for example:

xff -t f   ->  "(-type f)"
xff -t f -t d   -> "(-type f -o -type d)"

As you can see, my function strips away the '-t' elements, then expands the
parameter list with '-o -type ' before each element, and finally strips
away the leading '-o'. It works as intended, but I feel it is more
complicated than required. In particular, I couldn't find a way to make the
'${^...}' parameter expansion trigger without the embedded print statement.
At the same time, I'm rather happy with having found a one-liner expressing
what I want.

So my question is: is there a more elegant way of solving this which is
still compact?

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: A minor syntax question
  2016-06-07  6:58 A minor syntax question Jesper Nygårds
@ 2016-06-07 11:54 ` Oliver Kiddle
  2016-06-07 18:47   ` Jesper Nygårds
  0 siblings, 1 reply; 3+ messages in thread
From: Oliver Kiddle @ 2016-06-07 11:54 UTC (permalink / raw)
  To: Jesper Nygårds; +Cc: Zsh Users

Jesper Nygårds wrote:
>     typearg="("${"$(print -- '-o -type '${^types:#-t})"#-o }")"

> away the leading '-o'. It works as intended, but I feel it is more
> complicated than required. In particular, I couldn't find a way to make the
> '${^...}' parameter expansion trigger without the embedded print statement.

Joining the array to form a string should avoid the need for a print,
allowing the #-o to apply to the string as a whole. E.g:

typearg=${${(j. .):-'-o -type '${^types:#-t}}#-o }

Shorter versions should be possible such as the following:

typearg="${${=types//-t/-o -type}[2,-1]}"

I'd be inclined to keep typearg as an array, however.

Oliver


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: A minor syntax question
  2016-06-07 11:54 ` Oliver Kiddle
@ 2016-06-07 18:47   ` Jesper Nygårds
  0 siblings, 0 replies; 3+ messages in thread
From: Jesper Nygårds @ 2016-06-07 18:47 UTC (permalink / raw)
  To: Zsh Users

[-- Attachment #1: Type: text/plain, Size: 1070 bytes --]

Both your examples work great, Oliver. Thank you.

Inspired by your second example I also found a solution which more closely
follows the way I have been thinking about the problem, and that I find
quite readable:

typearg="("${(j: -o :)${types:#-t}/#/-type }")"

On Tue, Jun 7, 2016 at 1:54 PM, Oliver Kiddle <okiddle@yahoo.co.uk> wrote:

> Jesper Nygårds wrote:
> >     typearg="("${"$(print -- '-o -type '${^types:#-t})"#-o }")"
>
> > away the leading '-o'. It works as intended, but I feel it is more
> > complicated than required. In particular, I couldn't find a way to make
> the
> > '${^...}' parameter expansion trigger without the embedded print
> statement.
>
> Joining the array to form a string should avoid the need for a print,
> allowing the #-o to apply to the string as a whole. E.g:
>
> typearg=${${(j. .):-'-o -type '${^types:#-t}}#-o }
>
> Shorter versions should be possible such as the following:
>
> typearg="${${=types//-t/-o -type}[2,-1]}"
>
> I'd be inclined to keep typearg as an array, however.
>
> Oliver
>

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2016-06-07 18:47 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-06-07  6:58 A minor syntax question Jesper Nygårds
2016-06-07 11:54 ` Oliver Kiddle
2016-06-07 18:47   ` Jesper Nygårds

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).