From c7cf1112b6c7c161d89f0e401188cdb3c89ebbe7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius=20R=2E=20Miguel?= Date: Sun, 28 Mar 2021 23:50:28 -0300 Subject: [PATCH] Add -y, --yes and -n, --no flags (currently unused) --- src/cli.rs | 41 +++++++++++++++++++++++++++++++++++++++++ src/main.rs | 16 ++-------------- 2 files changed, 43 insertions(+), 14 deletions(-) diff --git a/src/cli.rs b/src/cli.rs index fb49d51..8a46675 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -22,6 +22,16 @@ pub enum CommandKind { ), } +#[derive(PartialEq, Eq, Debug)] +pub enum Flags { + // No flags supplied + None, + // Flag -y, --yes supplied + AlwaysYes, + // Flag -n, --no supplied + AlwaysNo +} + #[derive(PartialEq, Eq, Debug)] pub struct Command { pub kind: CommandKind, @@ -63,12 +73,43 @@ Please relate any issues or contribute at https://github.com/vrmiguel/ouch") .help("The output directory or compressed file.") .takes_value(true), ) + .arg( + Arg::with_name("yes") + .required(false) + .multiple(false) + .long("yes") + .short("y") + .help("Says yes to all confirmation dialogs.") + .conflicts_with("no") + .takes_value(false), + ) + .arg( + Arg::with_name("no") + .required(false) + .multiple(false) + .long("no") + .short("n") + .help("Says no to all confirmation dialogs.") + .conflicts_with("yes") + .takes_value(false), + ) } pub fn get_matches() -> clap::ArgMatches<'static> { clap_app().get_matches() } +pub fn parse_matches(matches: clap::ArgMatches<'static>) -> crate::Result<(Command, Flags)> { + let flag = match (matches.is_present("yes"), matches.is_present("no")) { + (true, true) => unreachable!(), + (true, _) => Flags::AlwaysYes, + (_, true) => Flags::AlwaysNo, + (_, _) => Flags::None + }; + + Ok((Command::try_from(matches)?, flag)) +} + impl TryFrom> for Command { type Error = crate::Error; diff --git a/src/main.rs b/src/main.rs index a13504f..3be1a77 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,24 +9,12 @@ mod file; mod test; mod utils; -use std::convert::TryFrom; - use error::{Error, Result}; use evaluator::Evaluator; fn main() -> crate::Result<()> { let matches = cli::get_matches(); - let command = cli::Command::try_from(matches)?; + // let command = cli::Command::try_from(matches)?; + let (command, _flags) = cli::parse_matches(matches)?; Evaluator::evaluate(command) } - -// fn main() -> crate::Result<()> { -// let dialog = dialogs::Confirmation::new("Do you want to overwrite 'FILE'?", Some("FILE")); - -// match dialog.ask(Some("file.tar.gz"))? { -// true => println!("deleting"), -// false => println!("keeping") -// }; - -// Ok(()) -// }