From a7e2bb86cbb168a8caa13a7d85278f57cffd6aaa Mon Sep 17 00:00:00 2001 From: Ethan P Date: Wed, 8 May 2019 18:02:16 -0700 Subject: [PATCH] Add validation for --terminal-width option --- src/clap_app.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/clap_app.rs b/src/clap_app.rs index a3db2a13..e00d074d 100644 --- a/src/clap_app.rs +++ b/src/clap_app.rs @@ -331,6 +331,24 @@ pub fn build_app(interactive_output: bool) -> ClapApp<'static, 'static> { .takes_value(true) .value_name("width") .hidden_short_help(true) + .validator( + |t| { + let sign = t.chars().next().unwrap(); + let num = if (sign == '+') || (sign == '-') { + t.chars().skip(1).collect() + } else { + t + }; + + num.parse::() + .map_err(|_e| "must be an offset or number") + .and_then(|v| if v == 0 && sign != '+' && sign != '-' { + Err("terminal width cannot be zero".into()) + } else { + Ok(()) + }) + .map_err(|e| e.to_string()) + }) .help( "Explicitly set the width of the terminal instead of determining it \ automatically. If prefixed with '+' or '-', the value will be treated \