tealdeer-rs / tealdeer

A very fast implementation of tldr in Rust.
https://tealdeer-rs.github.io/tealdeer/
Apache License 2.0
4.17k stars 123 forks source link

Auto-generate completion files through clap #220

Closed dbrgn closed 2 years ago

dbrgn commented 2 years ago

Replace our manually-written completion files for bash, zsh and fish with auto-generated completion files.

Clap supports this through https://github.com/clap-rs/clap/tree/master/clap_generate.

These completion files don't need to be checked in, instead the release build script should generate them. Additionally, the README could contain a section on how to generate these completions (might be interesting for packagers).

dmaahs2017 commented 2 years ago

How would we feel about a tldr completions <bash|zsh|fish> subcommand or tldr --completions <bash|zsh|fish> flag that generates the completions to stdout?

I've noticed some other CLI apps I use do this, so just wanted to point it out as an additional option.

dbrgn commented 2 years ago

While I see your point, I don't think it's worth the additional CLI option (which increases complexity) for a feature that is mostly relevant for packagers (not really for end users) and will only be used max once per installation.

dmaahs2017 commented 2 years ago

clap_generate was depractated and moved to a new crate called clap_complete in clap v3 https://crates.io/crates/clap_complete

dmaahs2017 commented 2 years ago

The way I see to do this involves creating a build.rs script described here. But I'm unsure of how to use this with our Args struct that derives Parser, because it appears that clap_complete::generate_to requires a clap::App struct as an argument. https://docs.rs/clap_complete/latest/clap_complete/generator/fn.generate_to.html

niklasmohrin commented 2 years ago

If I understand correctly, what you are proposing has already been tried in #256, which was closed because dynamic completion of pages did not work with the generated completions - or are you proposing something else?

dmaahs2017 commented 2 years ago

Oh I see, so we want it to complete the page lookups, and not just the flags, correct?

How is this done currently?

niklasmohrin commented 2 years ago

All three shells we support do support such dynamic completion. Currently, we write the completion files by hand and make use of the features (see usages of tldr --list in the completion files in completion/).

This is of course not optimal, because each completion needs to be kept up to date and in sync with each other - I just found a bug with the fish one (#268)

dbrgn commented 2 years ago

Yeah, until clap supports such dynamic lookups, it's probably best to close this issue.