Add -y, --yes and -n, --no flags (currently unused)

This commit is contained in:
Vinícius R. Miguel 2021-03-28 23:50:28 -03:00
parent 7e85798381
commit c7cf1112b6
2 changed files with 43 additions and 14 deletions

View File

@ -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<clap::ArgMatches<'static>> for Command {
type Error = crate::Error;

View File

@ -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(())
// }