on master help sign in
Add validation for --terminal-width option
a7e2bb8Ethan P
src/clap_app.rs+18
@@ -331,6 +331,24 @@ pub fn build_app(interactive_output: bool) -> ClapApp<'static, 'static> {
331 .takes_value(true)
331 .takes_value(true)
332 .value_name("width")
332 .value_name("width")
333 .hidden_short_help(true)
333 .hidden_short_help(true)
334+ .validator(
335+ |t| {
336+ let sign = t.chars().next().unwrap();
337+ let num = if (sign == '+') || (sign == '-') {
338+ t.chars().skip(1).collect()
339+ } else {
340+ t
341+ };
342+
343+ num.parse::<u32>()
344+ .map_err(|_e| "must be an offset or number")
345+ .and_then(|v| if v == 0 && sign != '+' && sign != '-' {
346+ Err("terminal width cannot be zero".into())
347+ } else {
348+ Ok(())
349+ })
350+ .map_err(|e| e.to_string())
351+ })
334 .help(
352 .help(
335 "Explicitly set the width of the terminal instead of determining it \
353 "Explicitly set the width of the terminal instead of determining it \
336 automatically. If prefixed with '+' or '-', the value will be treated \
354 automatically. If prefixed with '+' or '-', the value will be treated \