diff --git a/README.md b/README.md index f1ccec9..8a1740c 100644 --- a/README.md +++ b/README.md @@ -241,21 +241,24 @@ systemctl --user start himalaya.service ## Completions ```sh -# For bash shells -himalaya bash-completions +Generates the completion script for the given shell -# For zsh shells -himalaya zsh-completions +USAGE: + himalaya completion -# For fish shells -himalaya fish-completions +FLAGS: + -h, --help Prints help information + -V, --version Prints version information + +ARGS: + [possible values: bash, zsh, fish] ``` -Those commands print the generated scripts to the stdout. You will have +The command prints the generated script to the stdout. You will have to manually save and source them. For example: ```sh -himalaya bash-completions > himalaya-completions.bash +himalaya completion bash > himalaya-completions.bash ``` ```sh diff --git a/src/completion/cli.rs b/src/completion/cli.rs index 056cb6d..5a38279 100644 --- a/src/completion/cli.rs +++ b/src/completion/cli.rs @@ -1,34 +1,27 @@ -use clap::{self, App, ArgMatches, SubCommand}; +use clap::{self, App, Arg, ArgMatches, Shell, SubCommand}; use error_chain::error_chain; use std::io; error_chain! {} pub fn completion_subcmds<'a>() -> Vec> { - vec![ - SubCommand::with_name("bash-completions").about("Generates bash completions script"), - SubCommand::with_name("zsh-completions").about("Generates zsh completions script"), - SubCommand::with_name("fish-completions").about("Generates fish completions script"), - ] + vec![SubCommand::with_name("completion") + .about("Generates the completion script for the given shell") + .args(&[Arg::with_name("shell") + .possible_values(&["bash", "zsh", "fish"]) + .required(true)])] } pub fn completion_matches(mut app: App, matches: &ArgMatches) -> Result { - use clap::Shell::*; - - if matches.is_present("bash-completions") { - app.gen_completions_to("himalaya", Bash, &mut io::stdout()); + if let Some(matches) = matches.subcommand_matches("completion") { + let shell = match matches.value_of("shell").unwrap() { + "fish" => Shell::Fish, + "zsh" => Shell::Zsh, + "bash" | _ => Shell::Bash, + }; + app.gen_completions_to("himalaya", shell, &mut io::stdout()); return Ok(true); - } - - if matches.is_present("zsh-completions") { - app.gen_completions_to("himalaya", Zsh, &mut io::stdout()); - return Ok(true); - } - - if matches.is_present("fish-completions") { - app.gen_completions_to("himalaya", Fish, &mut io::stdout()); - return Ok(true); - } + }; Ok(false) }