pub(crate) const FOLLOW_INTERVAL_FLOOR_SECONDS: u64 = 1;
#[derive(Debug, clap::Args)]
pub struct FlockArgs {
#[arg(long, action = clap::ArgAction::SetTrue)]
pub follow: bool,
#[arg(
long,
default_value_t = FOLLOW_INTERVAL_FLOOR_SECONDS,
value_name = "SECONDS",
value_parser = clap::value_parser!(u64).range(FOLLOW_INTERVAL_FLOOR_SECONDS..),
requires = "follow"
)]
pub interval: u64,
}
#[derive(Debug, clap::Args)]
pub struct FoldArgs {
pub name: String,
}
#[derive(Debug, clap::Args)]
pub struct LookoutArgs {
#[arg(long)]
pub read_only: bool,
}
#[cfg(test)]
mod tests {
use super::*;
use crate::cli::{Cli, Commands};
#[test]
fn flock_follows_only_when_asked_and_defaults_to_a_one_second_interval() {
use clap::Parser;
let Commands::Flock(args) = Cli::try_parse_from(["shep", "flock"]).unwrap().command else {
panic!("expected flock")
};
assert!(!args.follow, "a bare listing is still one moment");
assert_eq!(args.interval, FOLLOW_INTERVAL_FLOOR_SECONDS);
let Commands::Flock(args) =
Cli::try_parse_from(["shep", "flock", "--follow", "--interval", "5"])
.unwrap()
.command
else {
panic!("expected flock")
};
assert!(args.follow);
assert_eq!(args.interval, 5);
assert!(
Cli::try_parse_from(["shep", "flock", "--interval", "5"]).is_err(),
"an interval with nothing to pace is a usage error"
);
}
#[test]
fn a_zero_interval_is_refused() {
use clap::Parser;
assert!(Cli::try_parse_from(["shep", "flock", "--follow", "--interval", "0"]).is_err());
}
#[test]
fn actions_are_on_unless_read_only_says_otherwise() {
use clap::Parser;
let Commands::Lookout(default) = Cli::try_parse_from(["shep", "lookout"]).unwrap().command
else {
panic!("lookout parses to its own variant")
};
assert!(!default.read_only);
let Commands::Lookout(flagged) = Cli::try_parse_from(["shep", "lookout", "--read-only"])
.unwrap()
.command
else {
panic!("lookout parses to its own variant")
};
assert!(flagged.read_only);
}
#[test]
fn allow_control_no_longer_parses() {
use clap::Parser;
assert!(Cli::try_parse_from(["shep", "lookout", "--allow-control"]).is_err());
}
}