add date formatting validation

This commit is contained in:
dvvvvvv 2019-11-07 11:59:20 +09:00 committed by Abin Simon
parent c4c9ee01f4
commit 698bc2ef3b
2 changed files with 16 additions and 0 deletions

View file

@ -18,6 +18,7 @@ path = "src/main.rs"
[build-dependencies] [build-dependencies]
clap = "2.33.*" clap = "2.33.*"
version_check = "0.9.*" version_check = "0.9.*"
time = "0.1.*"
[dependencies] [dependencies]
ansi_term = "0.12.*" ansi_term = "0.12.*"

View file

@ -1,5 +1,6 @@
use clap::{App, Arg}; use clap::{App, Arg};
pub fn build() -> App<'static, 'static> { pub fn build() -> App<'static, 'static> {
App::new("lsd") App::new("lsd")
.version(crate_version!()) .version(crate_version!())
@ -134,6 +135,7 @@ pub fn build() -> App<'static, 'static> {
.arg( .arg(
Arg::with_name("date") Arg::with_name("date")
.long("date") .long("date")
.validator(validate_date_argument)
.default_value("date") .default_value("date")
.multiple(true) .multiple(true)
.number_of_values(1) .number_of_values(1)
@ -202,3 +204,16 @@ pub fn build() -> App<'static, 'static> {
.help("Do not display files/directories with names matching the glob pattern(s)"), .help("Do not display files/directories with names matching the glob pattern(s)"),
) )
} }
fn validate_date_argument(arg: String) -> Result<(), String> {
use std::error::Error;
if arg.starts_with("+") {
time::now().strftime(&arg).map(drop).map_err(|err| err.description().to_string())
} else if &arg == "date" {
Result::Ok(())
} else if &arg == "relative" {
Result::Ok(())
} else {
Result::Err("possible values: date, relative".to_owned())
}
}