using friendly format for claps help message #178
-
use clap::{Parser};
use jiff::{Span, ToSpan};
#[derive(Parser)]
#[command(version, about)]
struct Args {
#[arg(short, long, default_value_t = 5.minutes())]
time: Span,
} when running this with
how can i have it show the friendly format, like |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
You probably need a wrapper type here. Clap is probably just using the standard Example: use clap::Parser;
use jiff::{Span, ToSpan};
#[derive(Parser)]
#[command(version, about)]
struct Args {
#[arg(short, long, default_value_t = FriendlySpan(5.minutes()))]
time: FriendlySpan,
}
fn main() {
let args = Args::parse();
println!("{}", args.time.0);
}
#[derive(Clone, Copy, Debug)]
struct FriendlySpan(Span);
impl std::fmt::Display for FriendlySpan {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "{:#}", self.0)
}
}
impl std::str::FromStr for FriendlySpan {
type Err = jiff::Error;
fn from_str(s: &str) -> Result<FriendlySpan, jiff::Error> {
Ok(FriendlySpan(s.parse()?))
}
} Kinda annoying, but I don't see a simpler way. Caveat emptor is that I'm not an expert in Clap's |
Beta Was this translation helpful? Give feedback.
You probably need a wrapper type here. Clap is probably just using the standard
Display
impl forSpan
, which will emit the ISO 8601 format for interoperability reasons. You can do{span:#}
to get the "alternate" impl, and that will emit the friendly format.Example: