clap-rs / clap

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

zsh completion ignores or fails options consumed by the zsh _arguments helper function #5521

Closed ben-- closed 3 weeks ago

ben-- commented 3 weeks ago

Please complete the following tasks

Rust Version

rustc 1.76.0 (07dca489a 2024-02-04) (Homebrew)

Clap Version

4.5.2

Minimal reproducible code

use std::io;

use clap::CommandFactory;
use clap::Parser;
use clap_complete::generate;
use clap_complete::Shell;

#[derive(Parser)]
struct Cli {
    #[clap(short = 'M')]
    m_test: Option<String>,

    #[clap(long = "zsh")]
    zsh: bool,
}

fn main() {
    let cli = Cli::parse();
    if cli.zsh {
        let mut cmd = Cli::command();
        generate(Shell::Zsh, &mut cmd, "test-app", &mut io::stdout());
    }
}

Steps to reproduce the bug with the above code

First, build the app, then set up your environment:

# Setup
zsh
autoload -Uz compinit
compinit
. <(test-app --zsh)

The validate:

test-app <TAB><TAB>

Actual Behaviour

% test-app
_describe:compadd:114: unknown match specification character `+'
_describe:compadd:114: unknown match specification character `+'
_describe:compadd:114: unknown match specification character `+'
_describe:compadd:134: unknown match specification character `+'
_describe:compadd:134: unknown match specification character `+'
_describe:compadd:134: unknown match specification character `+'
_describe:compadd:134: unknown match specification character `+'
_describe:compadd:134: unknown match specification character `+'
_arguments:compadd:551: unknown match specification character `+'

Expected Behaviour

% test-app -
--help  -h  -- Print help
--zsh       -- Generate zsh completions
-M          -- This operation is broken

Additional Context

The zsh _arguments completion function has the following syntax:

_arguments [ARGUMENTS_OPTIONS] [:] COMMAND_ARG...

Where the optional colon is used to disambiguate options consumed by the _arguments function itself.

If this colon is missing and the first COMMAND_ARG conflicts with one of the options supported by _arguments, then it is misinterpreted resulting in missing options or the unknown match specification character seen above.

Debug Output

No response