Skip to content
This repository has been archived by the owner on Mar 31, 2024. It is now read-only.

Commit

Permalink
Generate completions per subcommand
Browse files Browse the repository at this point in the history
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
  • Loading branch information
swsnr committed Nov 28, 2022
1 parent cf25a79 commit 4192672
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Expand Up @@ -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

Expand Down
14 changes: 6 additions & 8 deletions build.rs
Expand Up @@ -18,14 +18,12 @@ fn gen_completions<P: AsRef<Path>>(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(())
Expand Down
2 changes: 1 addition & 1 deletion src/bin/mdcat/args.rs
Expand Up @@ -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,
Expand Down

0 comments on commit 4192672

Please sign in to comment.