From: Ian Jackson Date: Wed, 5 Aug 2020 23:37:27 +0000 (+0100) Subject: call flag X-Git-Tag: otter-0.2.0~1176 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=277869234a50f64d02c3771dd9f507bf23416f73;p=otter.git call flag --- diff --git a/src/bin/otter.rs b/src/bin/otter.rs index 6505fa9a..48d9655e 100644 --- a/src/bin/otter.rs +++ b/src/bin/otter.rs @@ -2,9 +2,10 @@ use game::imports::*; use argparse::{self,ArgumentParser,action::{TypedAction,ParseResult}}; -use argparse::action::{Action,IArgAction}; +use argparse::action::{Action,IFlagAction,IArgAction}; use std::rc::Rc; use std::cell::RefCell; +use std::cell::Cell; /* use std::cell::Cell; @@ -44,24 +45,37 @@ impl<'a,T> Deref for CellRef<'a,T> { } */ -struct Call; -struct CallIArgAction(RRF); -impl IArgAction for CallIArgAction>> +struct CallAction(F, PhantomData); + +struct CallFlag; + +impl IFlagAction for CallAction>> +where F : FnMut() -> ParseResult { + fn parse_flag(&self) -> ParseResult { + self.0.borrow_mut()() + } +} + +impl ParseResult> TypedAction for CallFlag { + fn bind<'x>(&self, f: Rc>) -> Action<'x> { + Action::Flag(Box::new(CallAction(f.clone(), PhantomData::))) + } +} + +struct CallArg; + +impl IArgAction for CallAction>> where F : FnMut(&str) -> ParseResult { fn parse_arg(&self, arg: &str) -> ParseResult { self.0.borrow_mut()(arg) } } -impl ParseResult> TypedAction for Call { + +impl ParseResult> TypedAction for CallArg { fn bind<'x>(&self, f: Rc>) -> Action<'x> { - Action::Single(Box::new(CallIArgAction(f.clone()))) + Action::Single(Box::new(CallAction(f.clone(), PhantomData::))) } -// fn bind<'x>(&self, t: Rc>) -> Action<'x> { -} -/* (&FnMut( } -*/ -//use structopt::StructOpt; //#[derive(Debug,StructOpt)] //#[structopt(rename_all="kebab-case")] @@ -77,12 +91,11 @@ enum Subcommand { } fn main() { - let mainopts : MainOpts = Default::default(); + let mut mainopts : MainOpts = Default::default(); { let ap = ArgumentParser::new(); + let scope = Cell::from_mut(&mut mainopts.scope); /* - let scope = ap.refer(&mut scope); - scope.add_option( Cell::from_mut(&mut mainopts.scope); ap.refer(&mut &scope).