can1357/oh-my-pi · error · HeadError

{0}

Error message

{0}

What it means

HeadError::ParseError carries a free-form message (formatted as '{0}') produced when parsing head's command-line-style options fails, such as an invalid count value or malformed flag combination. The message text itself describes the specific parse problem.

Source

Thrown at crates/pi-builtins/src/head.rs:990

		let output_reader = BufReader::new(take_lines(input_reader, 4, b'\n'));
		let mut iter = output_reader.lines().map(|l| l.unwrap());
		assert_eq!(Some(String::from("a")), iter.next());
		assert_eq!(Some(String::from("b")), iter.next());
		assert_eq!(Some(String::from("c")), iter.next());
		assert_eq!(None, iter.next());
	}
}
}

use take::{copy_all_but_n_bytes, copy_all_but_n_lines, take_lines};

#[derive(Error, Debug)]
enum HeadError {
	/// Wrapper around `io::Error`
	#[error("error reading {}: {}", name.quote(), err)]
	Io { name: PathBuf, err: io::Error },

	#[error("{0}")]
	ParseError(String),

	#[error("number of -bytes or -lines is too large")]
	NumTooLarge(#[from] TryFromIntError),


	#[error("{0}")]
	MatchOption(String),
}

type HeadResult<T> = Result<T, HeadError>;

#[derive(Debug, PartialEq)]
enum Mode {
	FirstLines(u64),
	AllButLastLines(u64),
	FirstBytes(u64),
	AllButLastBytes(u64),

View on GitHub (pinned to 9690622007)

Solutions

  1. Read the embedded message (the {0} payload) to identify the exact parse problem
  2. Pass a valid numeric count, e.g. -n 10 or -c 1024
  3. Validate option strings before invoking the builtin in embedded usage
  4. Match only documented flags supported by this head implementation

Example fix

// before
head_opts(["-n", "abc"]);
// after
head_opts(["-n", "10"]);
Defensive patterns

Strategy: validation

Validate before calling

fn validate_head_args(args: &[&str]) -> Result<(), String> {
	let mut i = 0;
	while i < args.len() {
		if args[i] == "-n" || args[i] == "-c" {
			let Some(v) = args.get(i + 1) else { return Err(format!("{} needs a value", args[i])) };
			if v.parse::<u64>().is_err() { return Err(format!("{} expects a number, got '{v}'", args[i])); }
			i += 2;
		} else { i += 1; }
	}
	Ok(())
}

Try / catch

match head_result {
	Err(HeadError::ParseError(msg)) => eprintln!("bad head options: {msg}"),
	Err(other) => return Err(other),
	Ok(v) => /* ... */,
}

Prevention

When it happens

Trigger: Invoking the head builtin with options that fail internal parsing, e.g. a non-numeric -n value, an unrecognized flag, or an argument combination the option parser rejects; the produced message is embedded in this variant.

Common situations: Scripts passing user-supplied counts like '-n abc'; duplicated/conflicting flags; copying CLI syntax that the in-process parser does not accept; forgotten argument values producing leftover tokens.

Related errors


AI-assisted analysis of can1357/oh-my-pi@9690622007 (2026-08-31). Data as JSON: /api/errors/db198d945af4ad97. Report an issue: GitHub.