zsh-users
 help / color / mirror / code / Atom feed
* Optimal use of zparseopts
@ 2005-04-15  1:46 Mariusz Gniazdowski
  2005-04-15  3:11 ` Bart Schaefer
  0 siblings, 1 reply; 3+ messages in thread
From: Mariusz Gniazdowski @ 2005-04-15  1:46 UTC (permalink / raw)
  To: zsh-users

Hi.
I'm trying to use zparseopts but it can be done in many ways.
Which is the most optimal? I mean:

- zparseopts can put options into arrays, like ( -l -h -t )

	How to best test if some value is in array?


- It can put values into arrays too

	How to reach it? Find key, then take [keyidx+1] ?


- It can put values into associative arrays

	How to test if option without argument has ben given? Something
	like:
		isset($hash[-h])

	I tried to use -K option, but it's like not working:

fun() { 
	typeset -A Args
	Args=(-sl 0 -si 0 -sm 0 -ss x)
	echo ${(kvqq)Args}
	zparseopts -K -D -E -A Args sl si sm ss sd
	echo ${(kvqq)Args}
}
$ fun -si a 
'-si' '0' '-sl' '0' '-sm' '0' '-ss' 'x' 
'-si' ''	


- How to handle the same option given many times?

	Like:
		fun -s abab -s ab -s x

-- 
Mariusz Gniazdowski

--------------------------------------------------------------------
Nie dzwon do Londynu...
zanim nie sprawdzisz HALO.interia.pl
Tutaj: http://link.interia.pl/f1870


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

* Re: Optimal use of zparseopts
  2005-04-15  1:46 Optimal use of zparseopts Mariusz Gniazdowski
@ 2005-04-15  3:11 ` Bart Schaefer
  2005-04-15  7:39   ` Mariusz Gniazdowski
  0 siblings, 1 reply; 3+ messages in thread
From: Bart Schaefer @ 2005-04-15  3:11 UTC (permalink / raw)
  To: zsh-users

On Apr 15,  3:46am, Mariusz Gniazdowski wrote:
}
} I'm trying to use zparseopts but it can be done in many ways.
} Which is the most optimal?

As usual, it depends on what you're eventually going to do with the
values you've parsed.

One use of zparseopts is in a wrapper function, where the only purpose
is to consume the options and arguments that actually belong to some
*other* program, and store them until that other program is called.  In
this case you don't care what you parsed, only that you can reguritate
it at need, so tossing them into a single array may be adequate.  The
completion system uses zparseopts this way a lot.

Another use is to parse options that are actually meaninful to the
function that calls zparseopts.  In this case you're more likely to want
either an associative array, or one array per option, or a mixture.

Still another use is to throw away options that you're not interested in,
perhaps because you've already examined them some other way.  That's what
the -D option is for.  And so on.

} - zparseopts can put options into arrays, like ( -l -h -t )
} 
} 	How to best test if some value is in array?

Any of a number of ways; for example:

    if (( ${+array[(r)-l]} )); then ...

} - It can put values into arrays too
} 
} 	How to reach it? Find key, then take [keyidx+1] ?

Normally you would not need to do this.  Instead of

    zparseopts -a array l: h t:

and then trying to examine $array, you'd do

    zparseopts l:=lop h=hop t:=top

which sets any or all of the three arrays $lop, $hop, and $top, so you
can examine them separately in whatever manner you like.

    if [[ $lop[1] == -l ]]; then echo The -l option is $lop[2] ; fi
    if [[ $top[1] != -t ]]; then echo There is no -t option ; fi

} - It can put values into associative arrays
} 
} 	How to test if option without argument has ben given?

    if (( ${+hash[-h]} )); then ...

} 	I tried to use -K option, but it's like not working:

It's working.  The doc says:

    -K
          With this option, the arrays specified with the -a and -A
          options and with the `=array' forms are kept unchanged when
          none of the specs for them is used.  This allows assignment
          of default values to them before calling zparseopts.

Note "when *none* of the specs" are used.  In your example, you used
the spec for the -si option, which is not "none", so the entire default
value of the hash is discarded.

It also appears that your example is missing the trailing colons on all
of the option names, to indicate that they each want an argument.

} - How to handle the same option given many times?
} 
} 	Like:
} 		fun -s abab -s ab -s x

RTFM.

    name+
          If a `+' appears after name, the option is appended to array
          each time it is found in the positional parameters; without
          the `+' only the _last_ occurrence of the option is preserved.

Hence

    zparseopts s+:=sop

would set sop=(-s abab -s ab -s x) given the "fun" call above.  This is
a case where "zparseopts -A" may not be very useful, because

    zparseopts -A hash s+:

would set hash[-s]="abababx" in that same situation.


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

* Re: Optimal use of zparseopts
  2005-04-15  3:11 ` Bart Schaefer
@ 2005-04-15  7:39   ` Mariusz Gniazdowski
  0 siblings, 0 replies; 3+ messages in thread
From: Mariusz Gniazdowski @ 2005-04-15  7:39 UTC (permalink / raw)
  To: zsh-users

Day Fri, Apr 15, 2005 at 03:11:58AM +0000, Bart Schaefer:
> Note "when *none* of the specs" are used.  In your example, you used
> the spec for the -si option, which is not "none", so the entire default
> value of the hash is discarded.
> 
> It also appears that your example is missing the trailing colons on all
> of the option names, to indicate that they each want an argument.
> 
> } - How to handle the same option given many times?
> } 
> } 	Like:
> } 		fun -s abab -s ab -s x
> 
> RTFM.
> 
>     name+
>           If a `+' appears after name, the option is appended to array
>           each time it is found in the positional parameters; without
>           the `+' only the _last_ occurrence of the option is preserved.
> 
> Hence
> 
>     zparseopts s+:=sop
> 
> would set sop=(-s abab -s ab -s x) given the "fun" call above.  This is
> a case where "zparseopts -A" may not be very useful, because
> 
>     zparseopts -A hash s+:
> 
> would set hash[-s]="abababx" in that same situation.

Well i did know that. Just wondering how to do it optimal way. I
now see, that giving such option separate array is probably best joice.

-- 
Mariusz Gniadowski

----------------------------------------------------------------------
Startuj z INTERIA.PL! >>> http://link.interia.pl/f186c 


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

end of thread, other threads:[~2005-04-15  7:40 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-04-15  1:46 Optimal use of zparseopts Mariusz Gniazdowski
2005-04-15  3:11 ` Bart Schaefer
2005-04-15  7:39   ` Mariusz 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).