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

Improved fish shell completion with filename parameters #3644

Open
2 tasks done
Icelk opened this issue Apr 20, 2022 · 0 comments
Open
2 tasks done

Improved fish shell completion with filename parameters #3644

Icelk opened this issue Apr 20, 2022 · 0 comments
Labels
A-completion Area: completion generator C-bug Category: Updating dependencies E-easy Call for participation: Experience needed to fix: Easy / not much

Comments

@Icelk
Copy link

Icelk commented Apr 20, 2022

Please complete the following tasks

Rust Version

rustc 1.60.0 (7737e0b5c 2022-04-04)

Clap Version

3.1.10

Minimal reproducible code

Any program which writes fish completions, has a file path as a unordered argument, and has subcommands.

use clap::{Command, AppSettings, Arg, ValueHint};
use clap_complete::{generate, Generator, Shell};
use std::io;

fn build_cli() -> Command<'static> {
    Command::new("example")
         .arg(Arg::new("file")
             .help("some input file")
             .required(true)
             .multiple(true)
                .value_hint(ValueHint::AnyPath),
        )
       .subcommand(
           Command::new("generator")
       )
}

fn print_completions<G: Generator>(gen: G, cmd: &mut Command) {
    generate(gen, cmd, cmd.get_name().to_string(), &mut io::stdout());
}

fn main() {
    let matches = build_cli().get_matches();

    if let Some(_) = matches.subcommand_matches("generator") {
        let mut cmd = build_cli();
        eprintln!("Generating completion file for {}...", generator);
        print_completions(Shell::Fish, &mut cmd);
    }
}

Steps to reproduce the bug with the above code

cargo run -- generate > ~/.config/fish/completions/<cargo project name>.fish

Actual Behaviour

When trying to complete now, files aren't completed as you'd expect. Fish expects the first argument to be a subcommand, but it can also be a file.

Expected Behaviour

Fish should complete both subcommands and files.

This was solved by prepending the following to the top of the completion file, where chute is my binary name:

complete -c chute -F

This could be done if a file path argument exists and we have subcommands.

Additional Context

No response

Debug Output

No response

@Icelk Icelk added the C-bug Category: Updating dependencies label Apr 20, 2022
@epage epage added A-completion Area: completion generator E-easy Call for participation: Experience needed to fix: Easy / not much labels Apr 20, 2022
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-bug Category: Updating dependencies E-easy Call for participation: Experience needed to fix: Easy / not much
Projects
None yet
Development

No branches or pull requests

2 participants