From 41926728922264559b694acd89dc68e91d1dd850 Mon Sep 17 00:00:00 2001 From: Sebastian Wiesner Date: Mon, 28 Nov 2022 20:36:15 +0100 Subject: [PATCH] Generate completions per subcommand We used to generate completions for the top-level command, but that's not correct in a multicall binary: It includes the subcommands as completable arguments, whereas we actually need to split completions per subcommand. Hence, iterate over the two relevant subcommands aka binary names, and separately generate the completion for each corresponding subcommand. This corrects completions for mdcat, and as a bonus we now also generate completions for mdless which we didn't have before. Closes GH-214 --- CHANGELOG.md | 7 +++++++ build.rs | 14 ++++++-------- src/bin/mdcat/args.rs | 2 +- 3 files changed, 14 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0ecabc89..8c46cfe2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,8 +8,15 @@ To publish a new release run `scripts/release` from the project directory. ## [Unreleased] +### Added +- Generate completions for `mdless` (see [GH-216]). + ### Fixed - Include generated shell completions in release artifacts. +- Fix completions for mdcat (see [GH-214] and [GH-216]) + +[GH-214]: https://github.com/lunaryorn/mdcat/issues/214 +[GH-216]: https://github.com/lunaryorn/mdcat/pull/216 ## [0.29.0] – 2022-10-21 diff --git a/build.rs b/build.rs index 410840ed..2c49dd9c 100644 --- a/build.rs +++ b/build.rs @@ -18,14 +18,12 @@ fn gen_completions>(out_dir: P) -> Result<()> { let completions = out_dir.as_ref().join("completions"); std::fs::create_dir_all(&completions).expect("Failed to create $OUT_DIR/completions"); - - for shell in [Shell::Bash, Shell::Zsh, Shell::Fish, Shell::PowerShell] { - generate_to( - shell, - &mut mdcat::Args::command(), - "mdcat", - &completions - )?; + for program in ["mdcat", "mdless"] { + let mut command = mdcat::Args::command(); + let subcommand = command.find_subcommand_mut(program).unwrap(); + for shell in [Shell::Bash, Shell::Zsh, Shell::Fish, Shell::PowerShell] { + generate_to(shell, subcommand, program, &completions)?; + } } Ok(()) diff --git a/src/bin/mdcat/args.rs b/src/bin/mdcat/args.rs index e48c0ed7..503439b4 100644 --- a/src/bin/mdcat/args.rs +++ b/src/bin/mdcat/args.rs @@ -25,7 +25,7 @@ You can obtain one at http://mozilla.org/MPL/2.0/." } #[derive(Debug, clap::Parser)] -#[command(multicall = true)] +#[command(multicall = true, arg_required_else_help = true, subcommand_required = true)] pub struct Args { #[command(subcommand)] pub command: Command,