From e07d5bfb3ed84e0223986a39831e80ca9f7f2466 Mon Sep 17 00:00:00 2001 From: Rico Riedel Date: Sat, 6 Aug 2022 16:14:37 +0200 Subject: [PATCH] Input validation --- Cargo.toml | 2 +- src/main.rs | 29 +++++++++++++++-------------- 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 98c1a52..c5d446a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,7 +5,7 @@ edition = "2021" description = "Wipe the content of your terminal." license = "MIT" repository = "https://github.com/ricoriedel/wipe" -authors = ["Rico Riedel"] +authors = ["Rico Riedel "] [dependencies] clap = { version = "3.2", features = ["derive"] } diff --git a/src/main.rs b/src/main.rs index 7dd2c53..68716f7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -19,7 +19,8 @@ pub use vec::*; use crate::convert::*; use crate::pattern::*; use crate::transform::*; -use clap::{Parser, ValueEnum}; +use clap::builder::NonEmptyStringValueParser; +use clap::{value_parser, Parser, ValueEnum}; use crossterm::style::Color; use crossterm::style::Color::*; use rand::prelude::*; @@ -33,29 +34,29 @@ use std::time::Duration; about = env!("CARGO_PKG_DESCRIPTION"), )] struct Args { - /// Set the duration of the animation [milliseconds] - #[clap(long, default_value_t = 2000)] + /// Set the animation duration [milliseconds] + #[clap(long, default_value_t = 2000, value_parser = value_parser!(u64).range(0..=60_000))] duration: u64, /// Set the frames per second - #[clap(long, default_value_t = 60)] + #[clap(long, default_value_t = 60, value_parser = value_parser!(u64).range(1..=480))] fps: u64, /// Set the chars used to model the pattern - #[clap(long, default_value = ".:+#")] + #[clap(long, default_value = ".:+#", value_parser = NonEmptyStringValueParser::new())] chars: String, /// Set the pattern #[clap(long, value_enum)] char_pattern: Option, - /// Revert the pattern [possible values: true, false] + /// Invert the pattern #[clap(long)] char_invert: Option, /// Swap the x-axis and y-axis of the pattern #[clap(long)] char_swap: Option, - /// Set the count of segments of the pattern [default: 1-4] - #[clap(long)] + /// Set the segment count of the pattern [default: 1-4] + #[clap(long, value_parser = value_parser!(u8).range(1..255))] char_segments: Option, - /// Set the count of slices of the pattern [default: 1-4] - #[clap(long)] + /// Set the slice count of the pattern [default: 1-4] + #[clap(long, value_parser = value_parser!(u8).range(1..255))] char_slices: Option, /// Set the colors used to fill the pattern #[clap(long, value_enum)] @@ -63,17 +64,17 @@ struct Args { /// Set the fill pattern #[clap(long, value_enum)] color_pattern: Option, - /// Choose if the fill pattern should move [possible values: true, false] + /// Choose if the fill pattern should move #[clap(long)] color_shift: Option, - /// Revert the fill pattern + /// Invert the fill pattern #[clap(long)] color_invert: Option, /// Swap the x-axis and y-axis of the fill pattern #[clap(long)] color_swap: Option, - /// Set the count of slices of the fill pattern [default: 1-4] - #[clap(long)] + /// Set the slice count of the fill pattern [default: 1-4] + #[clap(long, value_parser = value_parser!(u8).range(1..255))] color_slices: Option, }