Skip to content

zsh: option with value breaks completions #369

@kpcyrd

Description

@kpcyrd

Having an flag that takes a value seems to break tab completion in zsh because the value is considered a subcommand.

I think this might be a clap bug but I'm having issues reproducing the exact example in clap, help to report this appropriately would be greatly appreciated!

The example works correctly with bash.

Code

use std::io::stdout;
use structopt::StructOpt;
use structopt::clap::Shell;

#[derive(Debug, StructOpt)]
pub struct Args {
    #[structopt(short="f", long="foo")]
    pub foo: Option<String>,

    #[structopt(subcommand)]
    pub subcommand: Option<SubCommand>,
}

#[derive(Debug, StructOpt)]
pub enum SubCommand {
    Zsh,
    Bash,
}

fn main() {
    let args = Args::from_args();
    eprintln!("{:?}", args);

    if let Some(c) = args.subcommand {
        let shell = match c {
            SubCommand::Zsh => Shell::Zsh,
            SubCommand::Bash => Shell::Bash,
        };

        Args::clap().gen_completions_to("completion-bug", shell, &mut stdout());
    }
}

Steps to reproduce

cargo run -- zsh > ~/.zsh_completions/_completion-bug # you might need to tweak this for your setup
zsh

# this works
completion-bug z<tab>
# this doesn't
completion-bug -f x z<tab>
# this does something unexpected
completion-bug -f z<tab>

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions