Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: help with a completion script?



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



Messages sorted by: Reverse Date, Date, Thread, Author