diff options
author | Alexis Bourget <alexis.bourget@gmail.com> | 2020-10-25 15:53:10 +0100 |
---|---|---|
committer | Alexis Bourget <alexis.bourget@gmail.com> | 2020-10-25 15:53:10 +0100 |
commit | b395debce4689f5b8d063a3e43031c606eeb0f01 (patch) | |
tree | a76f5a615c7589712c16935f494319a85cd96b46 | |
parent | e62a3a6f70e1fa66848aa82611b53415b934104b (diff) |
Ensures '--len' does not crash or hang
Fixes #24.
I also simplified some parts of `ìs_stdin`.
-rw-r--r-- | src/lib.rs | 22 |
1 files changed, 8 insertions, 14 deletions
@@ -246,7 +246,7 @@ pub fn run(matches: ArgMatches) -> Result<(), Box<dyn Error>> { } if let Some(length) = matches.value_of(ARG_LEN) { - truncate_len = length.parse::<u64>().unwrap(); + truncate_len = length.parse::<u64>()?; } if let Some(format) = matches.value_of(ARG_FMT) { @@ -330,24 +330,18 @@ pub fn is_stdin(matches: ArgMatches) -> Result<bool, Box<dyn Error>> { dbg!(env::args().len(), matches.args.len()); dbg!(env::args().nth(0).unwrap()); } - if let Some(nth1) = env::args().nth(1) { + if let Some(file) = matches.value_of(ARG_INP) { if DBG > 0 { - dbg!(nth1); + dbg!(file); } - for arg in ARGS.iter() { - if let Some(index) = matches.index_of(arg) { - if let 2 = index { - is_stdin = true; - } - } + is_stdin = false; + } else if let Some(nth1) = env::args().nth(1) { + if DBG > 0 { + dbg!(nth1); } + is_stdin = ARGS.iter().any(|arg| matches.index_of(arg) == Some(2)); } else if matches.args.is_empty() { is_stdin = true; - } else if let Some(file) = matches.value_of(ARG_INP) { - if DBG > 0 { - dbg!(file); - } - is_stdin = false; } if DBG > 0 { dbg!(is_stdin); |