Skip to content

Commit

Permalink
fix(completion): fix the generation of completion for shells
Browse files Browse the repository at this point in the history
  • Loading branch information
tmorin committed Mar 20, 2023
1 parent 29edcdd commit 7d2472e
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 12 deletions.
20 changes: 8 additions & 12 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ use std::ffi::OsString;
use std::io;
use std::str::FromStr;

use clap_complete::{generate, Shell};
use log::LevelFilter;

use crate::cli::build_cli;
use crate::cmd::execute_completion;
use crate::cmd::execute_diagram_generate;
use crate::cmd::execute_library_generate;

Expand Down Expand Up @@ -80,17 +80,13 @@ where
}
},
Some(("completion", m)) => {
let shell = m
.get_one::<String>("SHELL")
.map(|v| Shell::from_str(v).unwrap())
.unwrap();
generate(
shell,
&mut build_cli(),
"plantuml-generator",
&mut io::stdout(),
);
return 1;
return match execute_completion(m) {
Ok(_) => 0,
Err(e) => {
log::error!("the command failed: {}", e);
2
}
};
}
_ => {
log::warn!("the SUBCOMMAND is missing");
Expand Down
30 changes: 30 additions & 0 deletions src/cmd/completion/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use std::io;

use clap::ArgMatches;
use clap_complete::{generate, Shell};

use crate::cli::build_cli;
use crate::result::Result;

pub fn execute_completion(arg_matches: &ArgMatches) -> Result<()> {
let shell = arg_matches.get_one::<Shell>("SHELL").unwrap();
generate(
*shell,
&mut build_cli(),
"plantuml-generator",
&mut io::stdout(),
);
Ok(())
}

#[cfg(test)]
mod test {
use super::*;

#[test]
fn test_completion() {
let arg_matches =
build_cli().get_matches_from(["plantuml-generator", "-l=Debug", "completion", "bash"]);
execute_completion(arg_matches.subcommand_matches("completion").unwrap()).unwrap();
}
}
2 changes: 2 additions & 0 deletions src/cmd/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
pub use self::completion::execute_completion;
pub use self::diagram::execute_diagram_generate;
pub use self::library::execute_library_generate;

mod completion;
mod diagram;
mod library;

0 comments on commit 7d2472e

Please sign in to comment.