diff options
Diffstat (limited to 'src/run/builtin.rs')
| -rw-r--r-- | src/run/builtin.rs | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/src/run/builtin.rs b/src/run/builtin.rs index c9456cd..366aaa5 100644 --- a/src/run/builtin.rs +++ b/src/run/builtin.rs @@ -3,10 +3,49 @@ use std::sync::{Arc, Mutex}; use std::{env::*, fs::OpenOptions, path::PathBuf}; +use pish_derive::FromArgs; + use super::{Builtin, BuiltinError as Error, BuiltinResult as Result}; use crate::parse::CmdDisplay; use crate::*; +pub enum ArgParseError<'a> { + LeftoverArg(&'a [u8]), + MissingArg(&'static str), + MissingArgValue(&'static str), + ArgValueParseError(&'static str, String), +} + +pub trait ArgParse: Sized { + fn parse<'a>(args: &'a [BString]) -> std::result::Result<Self, ArgParseError<'a>>; +} + +fn args<T: ArgParse>(args: &[BString], w: &mut dyn Write) -> std::result::Result<T, Error> { + let err = match T::parse(args) { + Ok(t) => return Ok(t), + Err(e) => e, + }; + + match err { + ArgParseError::LeftoverArg(items) => { + w.write_all(b"leftover argument: ")?; + w.write_all(items)?; + w.write_all(b"\n")?; + } + ArgParseError::MissingArg(arg) => { + write!(w, "argument `{arg}` is missing\n")?; + } + ArgParseError::MissingArgValue(arg) => { + write!(w, "argument `{arg}` is missing its value\n")?; + } + ArgParseError::ArgValueParseError(arg, err) => { + write!(w, "failed to parse value of `{arg}`: {err}")?; + } + } + + Err(Error::Exit(-2)) +} + pub struct cd; impl Builtin for cd { fn name(&self) -> &str { @@ -206,6 +245,15 @@ impl Builtin for builtins { } } +#[derive(FromArgs)] +struct HistoryArgs { + local: bool, + + here: bool, + at: Option<PathBuf>, + // TODO: temporal control, i.e. before & after +} + pub struct history; impl Builtin for history { fn name(&self) -> &str { |
