Add validation for --terminal-width option

This commit is contained in:
Ethan P 2019-05-08 18:02:16 -07:00 committed by David Peter
parent 1ce0bc8e0d
commit a7e2bb86cb
1 changed files with 18 additions and 0 deletions

View File

@ -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::<u32>()
.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 \