add completion subcommands (#99)

* add completion subcommands

* improve error mgmt, add entry to readme

Co-authored-by: Clément DOUIN <soywod@users.noreply.github.com>
This commit is contained in:
remche
2021-04-14 10:28:23 +02:00
committed by GitHub
parent 8a617c314e
commit 9287846aed
3 changed files with 78 additions and 4 deletions
+34
View File
@@ -0,0 +1,34 @@
use clap::{self, App, ArgMatches, SubCommand};
use error_chain::error_chain;
use std::io;
error_chain! {}
pub fn completion_subcmds<'a>() -> Vec<App<'a, 'a>> {
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"),
]
}
pub fn completion_matches(mut app: App, matches: &ArgMatches) -> Result<bool> {
use clap::Shell::*;
if matches.is_present("bash-completions") {
app.gen_completions_to("himalaya", Bash, &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)
}
+17 -4
View File
@@ -27,13 +27,17 @@ mod mbox {
pub(crate) mod cli;
pub(crate) mod model;
}
mod completion {
pub(crate) mod cli;
}
use clap;
use clap::{self, App};
use error_chain::error_chain;
use log::{debug, error};
use std::env;
use crate::{
completion::cli::{completion_matches, completion_subcmds},
config::cli::account_arg,
flag::cli::{flag_matches, flag_subcmds},
imap::cli::{imap_matches, imap_subcmds},
@@ -52,12 +56,13 @@ error_chain! {
ImapCli(crate::imap::cli::Error, crate::imap::cli::ErrorKind);
MboxCli(crate::mbox::cli::Error, crate::mbox::cli::ErrorKind);
MsgCli(crate::msg::cli::Error, crate::msg::cli::ErrorKind);
CompletionCli(crate::completion::cli::Error, crate::completion::cli::ErrorKind);
OutputLog(crate::output::log::Error, crate::output::log::ErrorKind);
}
}
fn run() -> Result<()> {
let matches = clap::App::new(env!("CARGO_PKG_NAME"))
fn build_cli() -> App<'static, 'static> {
clap::App::new(env!("CARGO_PKG_NAME"))
.version(env!("CARGO_PKG_VERSION"))
.about(env!("CARGO_PKG_DESCRIPTION"))
.author(env!("CARGO_PKG_AUTHORS"))
@@ -68,7 +73,11 @@ fn run() -> Result<()> {
.subcommands(imap_subcmds())
.subcommands(mbox_subcmds())
.subcommands(msg_subcmds())
.get_matches();
.subcommands(completion_subcmds())
}
fn run() -> Result<()> {
let matches = build_cli().get_matches();
let output_fmt: OutputFmt = matches.value_of("output").unwrap().into();
let log_level: LogLevel = matches.value_of("log").unwrap().into();
@@ -91,6 +100,10 @@ fn run() -> Result<()> {
break;
}
if completion_matches(build_cli(), &matches)? {
break;
}
msg_matches(&matches)?;
break;
}