You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

66 lines
1.8 KiB

use std::{fmt,error};
/// Print help message then exit with code `2`.
pub fn usage() -> !
{
eprintln!("uasge: {} <output file> <input files...>", std::env::args().next().unwrap());
eprintln!();
eprintln!("mapcat v{}: mmap() based file concatenation", env!("CARGO_PKG_VERSION"));
eprintln!(" by {} with <3 (licensed GPL3+)", env!("CARGO_PKG_AUTHORS"));
eprintln!();
eprintln!("NOTES:");
eprintln!("The output file is required, if one is not provided, we will just print this message and exit with error code `2`. If there are no input files, it will just create an empty output and return with error code `0` (unless there were other errors)");
eprintln!("Output files are always clobbered regardless if there are any inputs, or if the concatenation operation succeeds / fails");
eprintln!();
#[cfg(feature="threads")] eprintln!(" (compiled with write parallelisation enabled)");
std::process::exit(2)
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Operation
{
pub output: String,
pub inputs: Vec<String>,
}
/// Parse the arguments passed to the process
#[inline] pub fn parse_args() -> Result<Operation, ArgParseError>
{
parse(std::env::args().skip(1))
}
fn parse<J: Into<String>, I: IntoIterator<Item = J>>(args: I) -> Result<Operation, ArgParseError>
{
let mut args = args.into_iter().map(Into::into);
let output = args.next().ok_or(ArgParseError::NoOutput)?;
Ok(
Operation {
output,
inputs: args.collect(),
}
)
}
#[derive(Debug)]
pub enum ArgParseError
{
NoOutput,
}
impl error::Error for ArgParseError{}
impl fmt::Display for ArgParseError
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result
{
match self {
Self::NoOutput => write!(f, "output file was not provided"),
}
}
}