Clap: Type no longer available for shells

Created on 3 May 2020  路  5Comments  路  Source: clap-rs/clap

Using StructOpt, I was able to parse an option like this:

stuct Cli {
    /// Output Bash, Fish, Zsh, PowerShell, or Elvish shell completion rules
    #[structopt(long)]
    completions: clap::Shell,
}

(Where clap here was the re-exported one StructOpt provided.)

This made it really easy to parse myapp --completions zsh while only accepting valid shells with completion generators. Nothing like myapp --completions foo would be accepted, but all the known shells were. This was really easy to pass to the generator.

Using the new derive system from Git HEAD there doesn't seem to be any way to do this any more. There are type objects that all implement a common interface, but I don't see a way to use that to automatically get the right values filled in here.

generator bug

Most helpful comment

Yeah, I think we should probably provide clap_generate::Shell enum for the ones we have autocompletions available.

All 5 comments

We made it clap_generate separate and rewrote it a bit. There's no Shell anymore because I made the generator agnostic and to be able to generate for other stuff and not only shells. If you need an enum like that you should probably write one yourself.

I understand the refactoring was to make it more extensible to other types of things in the future, but this is also an ergonomics regression coming from StructOpt. Given that the modules does have a fixed list of shells that are supported out of the box it seems like providing an enum and the ability to type cast like this would be an appropriate thing to do as part of the module.

@pksunkara I like the re-write, but I also think it'd nice to add a shell enum that dispatches the proper completion type. I haven't dug into it too deep yet, so it's hard for me to say if it should only include the currently supported shells, or all completion types...

Alternatively, we could so something as simple as provide an example, or clearly in the docs show how to create this enum and dispatch to the corresponding type. I agree it's pretty simple task, but to ease the transition from 2.x/structopt to 3.x, it'd help with this use case.

This is especially useful for the CLIs that provide the completion script generation at runtime via some CLI option (a la rustup with rustup completions <shell>).

Yeah, I think we should probably provide clap_generate::Shell enum for the ones we have autocompletions available.

This enum will need to be #[non_exhaustive]. Who knows which shells we will add in future?

Was this page helpful?
0 / 5 - 0 ratings