adaszko / complgen

Declarative bash/fish/zsh completions without writing shell scripts
Apache License 2.0
221 stars 7 forks source link

Representing multiple exclusive words separated by , #53

Closed kpbaks closed 1 month ago

kpbaks commented 1 month ago

First of all. Thank you for creating this tool, it really makes it simple to write completion scripts!

I am trying to write a completion for duf. Several of its flags support giving multiple values separated with comma e.g:

duf --hide-fs tmpfs,vfat

So far I got it working with a single value with the <FS> nonterminal. But when i specify the alternative -hide-fs <FS>[,<FS>]... and press tab i get all of them at once with no comma separation:

image

Is it possible to express this pattern, and if so what is the correct syntax for it?

System

I use fish version 3.7.1


Here is my duf.usage file:

// https://github.com/muesli/duf
duf [<OPTS>]...;

<OPTS> ::=
    -all "include pseudo, duplicate, inaccessible file systems"
    | -hide-fs <FS>[,<FS>]... "hide specific filesystems, separated with commas"
    ;

<FS@fish> ::= {{{ string split ' ' --fields 3 </proc/mounts | sort --unique }}};
adaszko commented 1 month ago

Hi! Glad you're making use of it!

I am able to complete the first <FS> (complgen version gives v0.2.0-15-g1d555d2ec7):

image

But it won't work for the subsequent <FS>s because you're hitting one of the caveats:

  • Within subwords, {{{ ... }}} is only allowed at tail position, where it doesn't lead to matching against an arbitrary output of an external command:

    • ERROR: {{{ git tag }}}..{{{ git tag }}}

      Impossible to guess at compilation time where the output of the first {{{ git tag }} ends and our .. begins.

    • OK: --option={{{ echo foo }}}

    • ERROR: {{{ echo foo }}}{{{ echo bar }}}

      Impossible to guess at compilation time where the output of the first {{{ echo foo }} ends the second {{{ echo bar }}} begins.

Basically, complgen cannot tell where the output of {{{ string split ' ' --fields 3 </proc/mounts | sort --unique }}} ends and the , begins — the output might as well have contained a comma and then it would be ambiguous.

Now, you should be getting a compilation error from complgen for this specific case but apparently it didn't work — I will fix that.

It's not possible to cover this pattern if the completions come from an external source currently. For it to do what you want, the splitting on commas would have to happen inside of {{{ string split ' ' --fields 3 </proc/mounts | sort --unique }}}.

kpbaks commented 1 month ago

Thank you for the elaborate reply. I forgot to mention the version of complgen I use. I think it is v0.1.8 based on the version packages in nixpkgs unstable right now.

adaszko commented 1 month ago

Wow, I didn't realize someone wrote a nixpkg for it. I will check it out.

adaszko commented 1 month ago

Tightened error reporting and sketched out a new feature that should cover this case. Thanks for reporting!