From 04af786fb725dedadd782709278921df190c713f Mon Sep 17 00:00:00 2001 From: gennyble Date: Mon, 6 Nov 2023 05:31:38 -0600 Subject: exit with 0 when out of lines --- popline.1 | 10 ++++++++++ src/main.rs | 15 +++++++++++---- 2 files changed, 21 insertions(+), 4 deletions(-) create mode 100644 popline.1 diff --git a/popline.1 b/popline.1 new file mode 100644 index 0000000..2e6062a --- /dev/null +++ b/popline.1 @@ -0,0 +1,10 @@ +.TH POPLINE 1 +.SH NAME +popline \- popfront a file +.SH SYNOPSIS +.B popline +.IR file +.SH DESCRIPTION +.B popline +reads the first line of a file, writes the file to disk without that line, +and prints the line to stdout. \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index b5994e9..c15cc54 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,17 +1,24 @@ +use std::path::PathBuf; + fn main() { + // We don't need anything fancy here now if std::env::args().len() != 2 { - eprintln!("usage: popline \n"); - eprintln!("removes the first line of a file and rewrites it to disk without that line"); + eprintln!( + "usage: popline \n\ + removes the first line of a file,\ + writes it back to disk without that line" + ); std::process::exit(1); } - let file = std::path::PathBuf::from(std::env::args().nth(1).unwrap()); + // A panic is fine, really; it'll be caught while writing + let file = PathBuf::from(std::env::args().nth(1).unwrap()); let string = std::fs::read_to_string(&file).unwrap(); let mut lines = string.lines(); let line = match lines.next() { None => { - std::process::exit(1); + std::process::exit(0); } Some(line) => line, }; -- cgit 1.4.1-3-g733a5