Skip to content

dynamic completions: register as external subcommand #6173

@jakobhellermann

Description

@jakobhellermann

Please complete the following tasks

Clap Version

4.5.51

Describe your use case

I was looking into adding dynamic completions (COMPLETE=shell) support for cargo fuzz. So I'd like to have it complete cargo fuzz <tab>.

By default, clap generates something like this on fish:

complete --keep-order --exclusive --command cargo-fuzz --arguments "(COMPLETE=fish "/path/to/cargo-fuzz" -- (commandline --current-process --tokenize --cut-at-cursor) (commandline --current-token))"

Changing that to --command 'cargo' -n '__fish_seen_subcommand_from fuzz' would make it complete cargo fuzz <tab> as well. I'm not sure if the other supported shells support something like this aswell though.


I also tried to do

#[derive(Parser)]
enum CargoArgs {
    Fuzz(CargoFuzzSubcommand),
}

#[derive(clap::Args)]
struct CargoFuzzSubcommand {
    #[clap(subcommand)]
    subcommand: Command,
}

fn main() -> Result<()> {
    CompleteEnv::with_factory(CargoArgs::command)
        .bin("cargo")
        .complete();
}

which works on fish, but on bash it completely replaces the original completion instead of adding to it.

Describe the solution you'd like

If something like fishs __fish_seen_subcommand_from can be done on other shells, maybe it could be exposed as

    CompleteEnv::with_factory(Command::command)
        .bin("cargo")
        .only_complete_subcommand("fuzz")
        .complete();

Alternatives, if applicable

Now that I think about it, it's not cargo-fuzzs responsibility to register completions for cargo.

Perhaps cargos completion script could instead complete unknown subcommands by letting the shell complete cargo-<subcommand> directly. On fish this is very easy with complete --do-complete "cargo-fuzz", again I have no idea how other shells compare here.

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-completionArea: completion generatorC-enhancementCategory: Raise on the bar on expectationsS-triageStatus: New; needs maintainer attention.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions