zsh-users
 help / color / mirror / code / Atom feed
* Minor issue about zparseopts
@ 2014-08-19 18:47 Jesper Nygårds
  2014-08-19 19:39 ` Bart Schaefer
  0 siblings, 1 reply; 3+ messages in thread
From: Jesper Nygårds @ 2014-08-19 18:47 UTC (permalink / raw)
  To: zsh-users

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

I have a function to which I want to pass one or more "filter words". I use
zparseopts for this to specify that -v can be used several times with
different arguments. Below is a simplified version of my function:

filter_stuff() {
    local filter
    local -a filters

    zparseopts -D -E v+:=v || return

    local n=1
    for filter in $v; do
       (( n++ % 2 == 0 )) && filters+=$filter
    done

    # Use $filters...
}

This works fine, I get my filter words in the array $filters. However, it
seems a bit inelegant to me.

1. Why is zparseopts implemented this way? the $v array has "-v" as odd
elements, and the arguments themselves as even elements. It seems a bit
redundant to include the "-v" elements, as I know what I specified the flag
to be.

2. Is there a more elegant way of extracting the "real arguments", i.e. the
even elements of the array, than the one I use above?

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

* Re: Minor issue about zparseopts
  2014-08-19 18:47 Minor issue about zparseopts Jesper Nygårds
@ 2014-08-19 19:39 ` Bart Schaefer
  2014-08-20  7:51   ` Jesper Nygårds
  0 siblings, 1 reply; 3+ messages in thread
From: Bart Schaefer @ 2014-08-19 19:39 UTC (permalink / raw)
  To: Jesper Nygards, zsh-users

On Aug 19,  8:47pm, Jesper Nygards wrote:
}
}     zparseopts -D -E v+:=v || return
} 
} seems a bit inelegant to me.
} 
} 1. Why is zparseopts implemented this way? the $v array has "-v" as odd
} elements, and the arguments themselves as even elements.

Several reasons:

* You can use the same array for multiple different flags, e.g.

    zparseopts -D v+:=args x+=args

and in that case you need to be able to tell which values were paired with
which flag.

* A common use of zparseopts is to extract the arguments of some common
external command, augment them in some way, and then call through to the
actual external command with the augmented list.  It's convenient to be
able to call through with just

    something $v

rather than e.g.

   something ${(s:,:):-"-v,"${^v}}

} 2. Is there a more elegant way of extracting the "real arguments", i.e. the
} even elements of the array, than the one I use above?

There are probably several of them.  Most obvious is that zsh allows
more than one loop variable in a for loop.

    local dashv filter
    for dashv filter in $v; do filters+=($filter); done

Or if you know that none of the filters is "-v" you can just do

    filters=( ${v#-v} )


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

* Re: Minor issue about zparseopts
  2014-08-19 19:39 ` Bart Schaefer
@ 2014-08-20  7:51   ` Jesper Nygårds
  0 siblings, 0 replies; 3+ messages in thread
From: Jesper Nygårds @ 2014-08-20  7:51 UTC (permalink / raw)
  To: zsh-users

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

Thank you Bart for the very clear explanation, and thank you as well for
the elegant extraction examples.

>
>

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

end of thread, other threads:[~2014-08-20  7:52 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-08-19 18:47 Minor issue about zparseopts Jesper Nygårds
2014-08-19 19:39 ` Bart Schaefer
2014-08-20  7:51   ` 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).