mirror of
https://github.com/lsd-rs/lsd
synced 2024-12-14 06:02:36 +00:00
add date formatting validation
This commit is contained in:
parent
c4c9ee01f4
commit
698bc2ef3b
2 changed files with 16 additions and 0 deletions
|
@ -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.*"
|
||||||
|
|
15
src/app.rs
15
src/app.rs
|
@ -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())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue