clap-rs / clap

A full featured, fast Command Line Argument Parser for Rust
docs.rs/clap
Apache License 2.0
13.65k stars 1.02k forks source link

Arg dynamic completion via `ValueHint::Execute(String)` #5449

Closed sigoden closed 2 months ago

sigoden commented 2 months ago

Please complete the following tasks

Clap Version

master

Describe your use case

Add dynamic completion for clap::Arg

Describe the solution you'd like

Add ValueHint::Execute(String) to generating possible completion candidates by exectuting the command.

Describe the solution you'd like

Add ValueHint::Execute(String) to generate possible completion candidates by executing the command.

Command::new("aichat")
    .arg(
        Arg::new("model")
            .short('m')
            .long("model")
            .value_hint(ValueHint::Execute("aichat --list-models"))
    )

#[derive(Args, Debug, PartialEq)]
struct AichatCli {
    #[arg(long, value_hint = ValueHint::Execute("aichat --list-models"))]
    model: Option<String>,
}
$ aichat -m <tab>
openai:gpt-3.5-turbo
openai:gpt-3.5-turbo-1106
...

Each line of the command output is a possible completion candidate.

Environment variabes $COMP_LINE and $COMP_POINT should passed when executing the command.

Alternatives, if applicable

No response

Additional Context

Similar issues:

Solved issues:

epage commented 2 months ago

The given use case for this proposal is focused on "let me provide dynamic completions by teaching clap how to call back into myself to provide those completions". This in effect is an alternative to #1232 which the issue acknowledges.

I prefer issues to be organized around problems, rather than solutions. This ensures we have a holistic conversation, rather than spreading the conversation out across multiple issues, making ti hard to follow.

I feel this would be encouraging people to extend their CLIs in ways that don't make sense from a CLI perspective but just as a workaround for not having the proposed solution in #1232.

This proposal also reuses something targeted at integrating in with shell logic but not for shell logic.

This is also a breaking change since ValueHint is Copy. This isn't a reason to say "no" to something on its own but that it raises the bar, making this not viable for a short term solution to a problem.

For these reasons, I'm closing this out. If you have a reason we should reconsider, let us know!