Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support allow_hyphen_values in native completions #5594

Closed
2 tasks done
shannmu opened this issue Jul 24, 2024 · 2 comments · Fixed by #5628
Closed
2 tasks done

Support allow_hyphen_values in native completions #5594

shannmu opened this issue Jul 24, 2024 · 2 comments · Fixed by #5628
Labels
A-completion Area: completion generator C-enhancement Category: Raise on the bar on expectations E-easy Call for participation: Experience needed to fix: Easy / not much

Comments

@shannmu
Copy link
Contributor

shannmu commented Jul 24, 2024

Please complete the following tasks

Clap Version

master

Describe your use case

use clap::{CommandFactory, Parser};
use clap_complete::dynamic::shells::CompleteCommand;
#[derive(Parser, Debug)]
#[clap(name = "dynamic", about = "A dynamic command line tool")]
struct Cli {
    /// The subcommand to run complete
    #[command(subcommand)]
    complete: Option<CompleteCommand>,
    /// Output format
    #[clap(short = 'F', long, value_parser = ["--json", "--yaml", "--toml"], allow_hyphen_values = true)]
    format: Option<String>,
    #[clap(long)]
    json: Option<String>,
}
fn main() {
    let cli = Cli::parse();
    if let Some(completions) = cli.complete {
        completions.complete(&mut Cli::command());
    }
    // normal logic continues...
}

For the command line:

dynamic --format --json --js[TAB]

There is no completion generated.
it should be:

dynamic --format --json --json 

This seems to be a very specific use case, but it shows that there is an error in parsing during dynamic completion when the allow_hyphen_values setting is enabled, or that allow_hyphen_values is not being considered.

Describe the solution you'd like

} else if arg.is_escape() {

} else if let Some((flag, value)) = arg.to_long() {

} else if let Some(mut short) = arg.to_short() {

do something more in these branches

Alternatives, if applicable

No response

Additional Context

There are also user cases related to the mistaken parsing.

Case 1

use clap::{CommandFactory, Parser};
use clap_complete::dynamic::shells::CompleteCommand;
#[derive(Parser, Debug)]
#[clap(name = "dynamic", about = "A dynamic command line tool")]
struct Cli {
    /// The subcommand to run complete
    #[command(subcommand)]
    complete: Option<CompleteCommand>,
    /// Output format
    #[clap(short = 'F', long, value_parser = ["json", "yaml", "toml"], allow_hyphen_values = true)]
    format: Option<String>,
    #[clap(value_parser = ["--pos_a"], index = 1)]
    positional_a: Option<String>,
    #[clap(value_parser = ["pos_b"], index = 2)]
    positional_b: Option<String>,
}
fn main() {
    let cli = Cli::parse();
    if let Some(completions) = cli.complete {
        completions.complete(&mut Cli::command());
    }
    // normal logic continues...
}

For the command line

dynamic --format json --pos_a [TAB]

completions are:

--format  --help    --pos_a   -F        -h        help

it should be:

pos_b --format  --help    -F        -h        help

Case 2

use clap::{CommandFactory, Parser};
use clap_complete::dynamic::shells::CompleteCommand;
#[derive(Parser, Debug)]
#[clap(name = "dynamic", about = "A dynamic command line tool")]
struct Cli {
    /// The subcommand to run complete
    #[command(subcommand)]
    complete: Option<CompleteCommand>,
    /// Output format
    #[clap(short = 'F', long, value_parser = ["json", "yaml", "toml"], allow_hyphen_values = true)]
    format: Option<String>,
    #[clap(value_parser = ["-a"], index = 1)]
    positional_a: Option<String>,
    #[clap(value_parser = ["pos_b"], index = 2)]
    positional_b: Option<String>,
}
fn main() {
    let cli = Cli::parse();
    if let Some(completions) = cli.complete {
        completions.complete(&mut Cli::command());
    }
    // normal logic continues...
}

For the command line

dynamic --format json -a [TAB]

completions are:

--format  --help    -F        -a        -h        help

it should be:

pos_b --format  --help    -F        -h        help
@shannmu shannmu added the C-enhancement Category: Raise on the bar on expectations label Jul 24, 2024
@shannmu
Copy link
Contributor Author

shannmu commented Jul 24, 2024

Track issue: #3166

@epage epage added A-completion Area: completion generator E-easy Call for participation: Experience needed to fix: Easy / not much labels Jul 24, 2024
@shannmu
Copy link
Contributor Author

shannmu commented Aug 6, 2024

There is a related case #5602 (comment).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-completion Area: completion generator C-enhancement Category: Raise on the bar on expectations E-easy Call for participation: Experience needed to fix: Easy / not much
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants