zsh-workers
 help / color / mirror / code / Atom feed
* help with a completion script?
@ 2015-08-04 17:56 Will Leinweber
  2015-08-04 18:57 ` Oliver Kiddle
  0 siblings, 1 reply; 3+ messages in thread
From: Will Leinweber @ 2015-08-04 17:56 UTC (permalink / raw)
  To: zsh-workers

I have a medium-to-small completion script here: http://git.io/vOaxv .
I've read through several tutorials, other scripts, and zshcompsys(1),
but there is one main thing I haven't been able to figure out.

The first argument to this binary is either one of several
subcommands, and that is all fine, however it can also be a *.cr file.
I tried adding in _files to the subcommand list, but instead of
showing files it shows just a literal * character and ends there.

Does anyone know of example completion scripts that do both
subcommands and files as the first argument, or know how to do this?


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

* Re: help with a completion script?
  2015-08-04 17:56 help with a completion script? Will Leinweber
@ 2015-08-04 18:57 ` Oliver Kiddle
  2015-08-04 20:52   ` Will Leinweber
  0 siblings, 1 reply; 3+ messages in thread
From: Oliver Kiddle @ 2015-08-04 18:57 UTC (permalink / raw)
  To: Will Leinweber; +Cc: zsh-workers

Will Leinweber wrote:
> The first argument to this binary is either one of several
> subcommands, and that is all fine, however it can also be a *.cr file.
> I tried adding in _files to the subcommand list, but instead of
> showing files it shows just a literal * character and ends there.

I'm not quite sure how you added _files to the subcommand list but
that's a literal list while _files is a function. You need to call both
_files and _describe. It sort-of works to just call them one after the
other but it is better to use _alternative which allows things to be
configured with zstyle and tag-order.

You can use _alternative within _arguments directly and _arguments
should probably be used for the --help and --version options. This comes
out as:
  _arguments -C \
    '(- 1 *)'{-h,--help}'[show help]' \
    '(- 1 *)'{-v,--version}'[show version]' \
    '1:sub-command: _alternative "subcommands:sub command:_crystal_commands" "files:file:_files -g \*.cr\(-.\)"' \
    '*::arg:->cmd' && ret=0

The 1: _arguments entry is much the same as the (( CURRENT == 1 )) test.

Other comments on the function:
> _crystal() {
...
> }
> _crystal

It isn't actually necessary to wrap the whole function with that. The
function autoloading mechanism doesn't need it. Or, you might choose to
only put this around the main part of the function after it defines all
the other functions.

> local context state line

You don't actually need to make context local because you're passing -C
to _arguments which uses curcontext instead. Unlike curcontext, context
is an array and is only necessary if more than one state could be valid
in the same situation. That's fairly rare and tends to only occur when
there are optional arguments.

> _call_function ret _crystal-$words[1]

You're ignoring the result of this in the ret variable. It is common for functions to finish with:
  return ret.

It is also wise to allow for the subcommand function not existing and
fallback on the default (file) completion. For example:
  if ! _call_function ret _crystal_$words[1]; then
    _default && ret=0
  fi

> '(-D --define)'{-D,--define}'[Define a compile-time flag]:'

The normal convention is for descriptions to be in all lowercase. You
don't need to follow that of course. Also the explanation for the
argument to -D is missing. If -D can be repeated, you can use \* instead
of the '(-D --define)' exclusion list. You may also find that it can be
--define= and perhaps -D+ (or other variations) depending on how the
option can be separated from the argument.

Crystal looks like an interesting project by the way.

Oliver


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

* Re: help with a completion script?
  2015-08-04 18:57 ` Oliver Kiddle
@ 2015-08-04 20:52   ` Will Leinweber
  0 siblings, 0 replies; 3+ messages in thread
From: Will Leinweber @ 2015-08-04 20:52 UTC (permalink / raw)
  To: Oliver Kiddle; +Cc: zsh-workers

Thanks so much! The completion is great now with most of those
suggestions, only one didn't work out for me:

Oliver Kiddle wrote:
> It isn't actually necessary to wrap the whole function with that. The
> function autoloading mechanism doesn't need it. Or, you might choose to
> only put this around the main part of the function after it defines all
> the other functions.

I had a few problems with only some completion things showing up when
I tried this one.

> Crystal looks like an interesting project by the way.

It is pretty good. I normally don't get excited about new tech, but
this project really has me excited.


Thanks again for the help on this script, and for yours and everyone
on this list's work on zsh.


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

end of thread, other threads:[~2015-08-04 20:53 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-08-04 17:56 help with a completion script? Will Leinweber
2015-08-04 18:57 ` Oliver Kiddle
2015-08-04 20:52   ` Will Leinweber

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