improve arg comments, rename arg Match to Command

This commit is contained in:
Clément DOUIN
2021-09-18 15:15:36 +02:00
parent e41a71648c
commit 8e2a703e2b
10 changed files with 122 additions and 95 deletions
+12 -10
View File
@@ -1,24 +1,26 @@
//! Module related to completion arguments.
//! Module related to completion CLI.
//!
//! This module provides subcommands and an argument matcher related to completion.
//! 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;
/// Enumeration of all possible matches.
pub enum Match<'a> {
type OptionShell<'a> = Option<&'a str>;
/// Completion commands.
pub enum Command<'a> {
/// Generate completion script for the given shell slice.
Generate(&'a str),
Generate(OptionShell<'a>),
}
/// Completion arg matcher.
pub fn matches<'a>(m: &'a ArgMatches) -> Result<Option<Match<'a>>> {
/// Completion command matcher.
pub fn matches<'a>(m: &'a ArgMatches) -> Result<Option<Command<'a>>> {
if let Some(m) = m.subcommand_matches("completion") {
debug!("completion command matched");
let shell = m.value_of("shell").unwrap();
debug!("shell: {}", shell);
return Ok(Some(Match::Generate(shell)));
let shell = m.value_of("shell");
debug!("shell: `{:?}`", shell);
return Ok(Some(Command::Generate(shell)));
};
Ok(None)
+2 -2
View File
@@ -7,8 +7,8 @@ use clap::{App, Shell};
use std::{io, str::FromStr};
/// Generate completion script from the given [`clap::App`] for the given shell slice.
pub fn generate<'a>(shell: &'a str, mut app: App<'a, 'a>) -> Result<()> {
let shell = Shell::from_str(shell)
pub fn generate<'a>(shell: Option<&'a str>, mut app: App<'a, 'a>) -> Result<()> {
let shell = Shell::from_str(shell.unwrap_or_default())
.map_err(|err| anyhow!(err))
.context("cannot parse shell")?;
app.gen_completions_to("himalaya", shell, &mut io::stdout());