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

Re: _arguments '(-o --option)'[...] functionality without calling _arguments



On Wed, Sep 13, 2023 at 1:41 PM Oliver Kiddle <opk@xxxxxxx> wrote:
> _describe does the same thing for words that are not options. It's
> implementation is in C code that is shared with that used by
> comparguments. It doesn't have to be done in C, however - compadd's
> interface would allow you to achieve the same thing from shell code if
> you wanted but that is more involved so check whether _describe meets
> your needs first.

Great, here's what I found out.  What works with _arguments:

cmd.zsh:
    _cmd () {
        _arguments '(-p --package)'{-p+,--package=}'[specify package
to build]:package:_cargo_package_names'
        return 0
    }
    compdef _cmd cmd

% source cmd.zsh
% cmd <TAB> # does what I want

With _describe, the same effect is achieved with:

cmd.zsh:
    _cmd () {
        local -a args=("-p" "--package")
        local -a descrs=("-p:specify package to build"
"-package:specify package to build")
        _describe '' descrs args
        return 0
    }
    compdef _cmd cmd


% source cmd.zsh
% cmd <TAB> # also does what I want

So it works (provided list-grouped zstyle is set to true but that's totally
fine). I'd be curious however to know how to get the same effect with plain
compadd.

> If you're looking at other completion functions for inspiration, I would
> recommend looking at those that come with zsh rather than those other
> projects include. Cargo's is fairly good but external projects often
> include completions that are auto-generated, converted from bash or
> otherwise less well written.

Thanks, that's good to know!

> Oliver
Thank you for taking a look at this, Oliver!
— Adam




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