zsh-users
 help / color / mirror / code / Atom feed
* When to (and not to) use _arguments -C with states
@ 2021-09-20 19:09 Eric Cook
  2021-09-21 15:20 ` Oliver Kiddle
  0 siblings, 1 reply; 2+ messages in thread
From: Eric Cook @ 2021-09-20 19:09 UTC (permalink / raw)
  To: zsh-users

I think that i mostly already figured it out but this piece of zshcompsys(1) always confused me.

  Where _arguments encounters action in the `->string' format, it will strip all leading and trailing whitespace from string and
  set the array state to the set of all strings for which an action is to be performed.  The elements of the  array  state_descr
  are assigned the corresponding message field from each optarg containing such an action.

In the majority of completers that i have written that used ->string actions, only one action was
`action`able at a given time. meaning $state is only a single element, with code that assumes as much like:

case $state in ...

Which made me question when is multiple ->string actions possible?

An contrived example being:
_foo() {
   local s expl state state_descr line
   local -A opt_args

   _arguments '1: :->foo' '-a[optional argument]:: :->bar' && return

   for s in $state; do
     case $s in
       foo) _wanted digit-tag expl 123s compadd 1 2 3;;
       bar) _wanted alpha-tag expl abcs compadd a b c;
     esac
   done
}
compdef _foo foo # foo -a <tab>

since -a uses ->string but it's argument is optional, the '1: :->foo' spec is also valid
so foo and bar are correctly in $state at once.

Are there other ways in which $state can contain > 1 elements?
I ask because i would like to know when it is safe to use _argument -C


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

* Re: When to (and not to) use _arguments -C with states
  2021-09-20 19:09 When to (and not to) use _arguments -C with states Eric Cook
@ 2021-09-21 15:20 ` Oliver Kiddle
  0 siblings, 0 replies; 2+ messages in thread
From: Oliver Kiddle @ 2021-09-21 15:20 UTC (permalink / raw)
  To: Eric Cook; +Cc: zsh-users

Eric Cook wrote:
> I think that i mostly already figured it out but this piece of zshcompsys(1) always confused me.
>
>   Where _arguments encounters action in the `->string' format, it will strip all leading and trailing whitespace from string and
>   set the array state to the set of all strings for which an action is to be performed.  The elements of the  array  state_descr
>   are assigned the corresponding message field from each optarg containing such an action.
>
> In the majority of completers that i have written that used ->string actions, only one action was
> `action`able at a given time. meaning $state is only a single element, with code that assumes as much like:

In the vast majority of cases this is true – which allows you to pass -C
and have it modifiy $curcontext instead of needing to pick out the
correct element of $context.

> Which made me question when is multiple ->string actions possible?
>
> An contrived example being:

This is probably the most obvious case – optional arguments in
conjunction with normal arguments. Fortunately even that is rare.
Optional arguments often require an = or no space between option and
argument.

It is also fairly easy to contrive cases using the sets feature of
_arguments, e.g something like:

  _arguments - first '-f' '1:foo' - second '-f:bar'

Sets with overloaded options does happen and can require a little care
(and sometimes the application of _guard for numeric arguments).

You can also get into the question of normal arguments that look like
options. _arguments doesn't always handle that perfectly but given that
argument parsing libraries generally don't cope either it isn't really
something we need to worry about.

The rare cases where you can't use _arguments -C are usually fairly
obvious. It tends to be otherwise apparent that they've got weird
argument parsing to begin with. For some such things, _regex_arguments
can be a better choice.

Oliver


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

end of thread, other threads:[~2021-09-21 15:21 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-09-20 19:09 When to (and not to) use _arguments -C with states Eric Cook
2021-09-21 15:20 ` Oliver Kiddle

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