use cargo workspace feature (#340)

For now, everything has been moved as it is in the "cli"
workspace. The next step is to separate the "lib" from the "cli".
This commit is contained in:
Clément DOUIN
2022-03-16 09:57:24 +01:00
parent ac8628c08c
commit b2cffd22f1
67 changed files with 226 additions and 252 deletions
+39
View File
@@ -0,0 +1,39 @@
//! Module related to completion CLI.
//!
//! This module provides subcommands and a command matcher related to completion.
use anyhow::Result;
use clap::{self, App, Arg, ArgMatches, Shell, SubCommand};
use log::{debug, info};
type OptionShell<'a> = Option<&'a str>;
/// Completion commands.
pub enum Command<'a> {
/// Generate completion script for the given shell slice.
Generate(OptionShell<'a>),
}
/// Completion command matcher.
pub fn matches<'a>(m: &'a ArgMatches) -> Result<Option<Command<'a>>> {
info!("entering completion command matcher");
if let Some(m) = m.subcommand_matches("completion") {
info!("completion command matched");
let shell = m.value_of("shell");
debug!("shell: {:?}", shell);
return Ok(Some(Command::Generate(shell)));
};
Ok(None)
}
/// Completion subcommands.
pub fn subcmds<'a>() -> Vec<App<'a, 'a>> {
vec![SubCommand::with_name("completion")
.aliases(&["completions", "compl", "compe", "comp"])
.about("Generates the completion script for the given shell")
.args(&[Arg::with_name("shell")
.possible_values(&Shell::variants()[..])
.required(true)])]
}
+21
View File
@@ -0,0 +1,21 @@
//! Module related to completion handling.
//!
//! This module gathers all completion commands.
use anyhow::{anyhow, Context, Result};
use clap::{App, Shell};
use log::{debug, info};
use std::{io, str::FromStr};
/// Generates completion script from the given [`clap::App`] for the given shell slice.
pub fn generate<'a>(mut app: App<'a, 'a>, shell: Option<&'a str>) -> Result<()> {
info!("entering generate completion handler");
let shell = Shell::from_str(shell.unwrap_or_default())
.map_err(|err| anyhow!(err))
.context("cannot parse shell")?;
debug!("shell: {}", shell);
app.gen_completions_to("himalaya", shell, &mut io::stdout());
Ok(())
}
+9
View File
@@ -0,0 +1,9 @@
//! Module related to shell completion.
//!
//! This module allows users to generate autocompletion scripts for their shells. You can see the
//! list of available shells directly on the [clap's docs.rs website].
//!
//! [clap's docs.rs website]: https://docs.rs/clap/2.33.3/clap/enum.Shell.html
pub mod compl_args;
pub mod compl_handlers;