aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorOnur Gündüz <ogunduz@gmail.com>2026-09-29 14:36:40 +0200
committerOnur Gündüz <ogunduz@gmail.com>2026-09-29 14:36:40 +0200
commit1f44aafa41174e7e8442179c526ed313d09e0bda (patch)
treeb95219dedb6c9a0ced532c144a0c52aa1ff8ac4b /src
parent0ebdd46766744d649c93f4023d7f20cbfde51c1e (diff)
downloadele-1f44aafa41174e7e8442179c526ed313d09e0bda.tar.gz
ele-1f44aafa41174e7e8442179c526ed313d09e0bda.zip
Correct CLI information flags and reject unknown long options
Diffstat (limited to 'src')
-rw-r--r--src/main.rs29
1 files changed, 27 insertions, 2 deletions
diff --git a/src/main.rs b/src/main.rs
index ca62416..900ff88 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -8,6 +8,7 @@ use std::{
};
const DEFAULT_MAX_RECORD_BYTES: usize = 8 * 1024 * 1024;
+const USAGE: &str = "[-p|--print] [--max-record-bytes N] [--] <expr> [file...]";
struct RecordOutput<W> {
writer: W,
@@ -70,10 +71,18 @@ fn filter_lines(
}
fn usage(program: &str) -> ! {
- eprintln!("usage: {program} [-p|--print] [--max-record-bytes N] [--] <expr> [file...]");
+ eprintln!("usage: {program} {USAGE}");
process::exit(2);
}
+fn print_info(program: &str, message: &str) {
+ let mut out = io::stdout().lock();
+ if let Err(error) = writeln!(out, "{message}").and_then(|()| out.flush()) {
+ eprintln!("{program}: {error}");
+ process::exit(1);
+ }
+}
+
fn main() {
let mut args = std::env::args();
let program = args.next().unwrap();
@@ -90,7 +99,23 @@ fn main() {
.unwrap_or_else(|| usage(&program));
}
"--" => break args.next().unwrap_or_else(|| usage(&program)),
- "-h" | "--help" => usage(&program),
+ "-h" | "--help" => {
+ return print_info(&program, &format!(
+ "usage: {program} {USAGE}\n\n\
+ -p, --print Print the parsed expression.\n\
+ --max-record-bytes N Limit each record, including its newline (default: {DEFAULT_MAX_RECORD_BYTES}).\n\
+ -h, --help Print help without reading input.\n\
+ -V, --version Print the version without reading input.\n\
+ -- End option parsing."
+ ));
+ }
+ "-V" | "--version" => {
+ return print_info(&program, concat!("aip-filter ", env!("CARGO_PKG_VERSION")));
+ }
+ _ if arg.starts_with("--") => {
+ eprintln!("{program}: unknown option: {arg}; use -- before a literal expression");
+ usage(&program);
+ }
_ => break arg,
}
};