-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Closed
Labels
A-completionArea: completion generatorArea: completion generatorC-bugCategory: bugCategory: bugE-hardCall for participation: Experience needed to fix: Hard / a lotCall for participation: Experience needed to fix: Hard / a lot
Description
Please complete the following tasks
- I have searched the discussions
- I have searched the open and rejected issues
Rust Version
rustc 1.86.0 (05f9846f8 2025-03-31)
Clap Version
v4.5.48
Minimal reproducible code
use clap::{CommandFactory, Parser, Subcommand};
use clap_complete::{CompleteEnv, Shell};
#[derive(Parser)]
#[command(version, about, long_about = None)]
struct Cli {
#[command(subcommand)]
action: Action,
}
#[derive(Subcommand)]
enum Action {
Complete(CompleteArgs),
}
#[derive(Parser)]
struct CompleteArgs {
/// The shell to generate completion for
#[arg(long, value_enum)]
shell: Shell,
}
fn main() {
CompleteEnv::with_factory(Cli::command).complete();
let cli = Cli::parse();
match cli.action {
Action::Complete(args) => {
let mut cmd = Cli::command();
let name = cmd.get_name().to_string();
clap_complete::generate(args.shell, &mut cmd, name, &mut std::io::stdout());
}
}
}Steps to reproduce the bug with the above code
- Use Bash
source <(cargo run -- complete --shell bash)- Type
<app-name> -- completein the shell (don't run it) - Position caret immediately after
--and press Tab twice
Actual Behaviour
Suggestions:
--shell --help
--shell is only valid after complete, so it shouldn't be an option.
Expected Behaviour
Only global flags should be available in this position:
--help --version
Additional Context
This also applies to nested subcommands. For example, for exhaustive, exhaustive ⇥ pacman would suggest one, two and help, which are subcommands of pacman.
The fix is to only consider $COMP_WORDS that come before $COMP_CWORD. This does not affect dynamic completion.
Debug Output
No response
Metadata
Metadata
Assignees
Labels
A-completionArea: completion generatorArea: completion generatorC-bugCategory: bugCategory: bugE-hardCall for participation: Experience needed to fix: Hard / a lotCall for participation: Experience needed to fix: Hard / a lot