about summary refs log tree commit diff
path: root/examples
diff options
context:
space:
mode:
authorgennyble <gen@nyble.dev>2025-04-29 14:45:01 -0500
committergennyble <gen@nyble.dev>2025-04-29 14:45:01 -0500
commitd7bdd477e266864e45ee33b29adba17bf3f70b20 (patch)
tree142745a311088c6ad274006bfb150f97758deea3 /examples
downloadscurvy-d7bdd477e266864e45ee33b29adba17bf3f70b20.tar.gz
scurvy-d7bdd477e266864e45ee33b29adba17bf3f70b20.zip
initial commit
Diffstat (limited to 'examples')
-rw-r--r--examples/copyline.rs63
1 files changed, 63 insertions, 0 deletions
diff --git a/examples/copyline.rs b/examples/copyline.rs
new file mode 100644
index 0000000..f0c2b67
--- /dev/null
+++ b/examples/copyline.rs
@@ -0,0 +1,63 @@
+use std::{
+	fs::File,
+	io::{self, BufRead, BufReader, ErrorKind, Write},
+	path::PathBuf,
+};
+
+use scurvy::{
+	Argument, Scurvy,
+	formula::{PathFormula, UsizeFormula},
+};
+
+pub fn main() {
+	let args = vec![
+		Argument::new(&["infile", "ifile"]).arg("path"),
+		Argument::new(&["line-number", "ln"]).arg("line#"),
+		Argument::new(&["outfile", "ofile"]).arg("path"),
+	];
+
+	let scurvy = Scurvy::make(args);
+
+	if scurvy.should_print_help() {
+		println!("{HELP_STR}");
+		return;
+	}
+
+	let input = scurvy.parse_req("infile", PathFormula::new());
+	let output = scurvy.parse_req("outfile", PathFormula::new());
+	let line_number = scurvy.parse_req("line-number", UsizeFormula::new().bounds(1..));
+
+	if let Err(e) = do_work(input, output, line_number) {
+		eprintln!("{e}");
+		std::process::exit(-1);
+	}
+}
+
+fn do_work(input: PathBuf, output: PathBuf, line_number: usize) -> io::Result<()> {
+	let bufr = BufReader::new(File::open(input)?);
+	let mut out = File::create(output)?;
+
+	match bufr.lines().nth(line_number) {
+		Some(line) => {
+			out.write_all((line?).as_bytes())?;
+			Ok(())
+		}
+		None => Err(io::Error::from(ErrorKind::UnexpectedEof)),
+	}
+}
+
+// Tab width is usually eight in terminals. Our max line-length for an options
+// name line is 72, the help-text itself is 66
+const HELP_STR: &'static str = "\
+usage: copyline ifile=<path> ofile=<path> ln=<line#>
+
+ARGUMENTS
+	infile=<path> | ifile=<path>
+		Input file path. Where we read the line from
+
+	outfile=<path> | ofile=<path>
+		Output file path. Where we write the line to.
+
+	line-number=<line#> | ln=<line#>
+		The line you want, starting at 1.
+";