Manually implement clap::Args for ExpirationArg.

This commit is contained in:
Justus Winter 2024-12-04 10:36:33 +01:00
parent f0e73deb7f
commit c6eb28eb1b
No known key found for this signature in database
GPG Key ID: 686F55B4AB2B3386

View File

@ -15,23 +15,8 @@ use clap::builder::Resettable;
use crate::cli::types::Time;
use crate::Result;
#[derive(clap::Args, Debug)]
#[derive(Debug)]
pub struct ExpirationArg {
#[clap(
long = "expiration",
allow_hyphen_values = true,
value_name = "EXPIRATION",
default_value_t = Expiration::Never,
help =
"Sets the expiration time",
long_help = "\
Sets the expiration time.
EXPIRATION is either an ISO 8601 formatted date with an optional time \
or a custom duration. A duration takes the form `N[ymwds]`, where the \
letters stand for years, months, weeks, days, and seconds, respectively. \
Alternatively, the keyword `never` does not set an expiration time.",
)]
expiration: Expiration,
}
@ -50,6 +35,54 @@ impl ExpirationArg {
}
}
impl clap::Args for ExpirationArg {
fn augment_args(cmd: clap::Command) -> clap::Command {
cmd.arg(
clap::Arg::new("expiration")
.long("expiration")
.allow_hyphen_values(true)
.value_name("EXPIRATION")
.value_parser(Expiration::new)
.default_value(Expiration::Never)
.help("Sets the expiration time")
.long_help("\
Sets the expiration time
EXPIRATION is either an ISO 8601 formatted date with an optional time \
or a custom duration. A duration takes the form `N[ymwds]`, where the \
letters stand for years, months, weeks, days, and seconds, respectively. \
Alternatively, the keyword `never` does not set an expiration time.")
)
}
fn augment_args_for_update(cmd: clap::Command) -> clap::Command {
Self::augment_args(cmd)
}
}
impl clap::FromArgMatches for ExpirationArg {
fn update_from_arg_matches(&mut self, matches: &clap::ArgMatches)
-> clap::error::Result<()>
{
if let Some(v) = matches.get_one::<Expiration>("expiration") {
self.expiration = v.clone();
}
Ok(())
}
fn from_arg_matches(matches: &clap::ArgMatches)
-> clap::error::Result<Self>
{
let mut expiration = ExpirationArg {
expiration: Expiration::Never,
};
expiration.update_from_arg_matches(matches)?;
Ok(expiration)
}
}
/// Expiration information
///
/// This enum tracks expiry information either in the form of a timestamp or