From cbf61eec8e9e04d68b9bdca3eed27e5e4b2aec15 Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Sun, 9 Aug 2020 23:20:07 +0100 Subject: [PATCH] main arguments --- src/bin/otter.rs | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/src/bin/otter.rs b/src/bin/otter.rs index 8e524014..fc7bf5ae 100644 --- a/src/bin/otter.rs +++ b/src/bin/otter.rs @@ -94,13 +94,14 @@ struct MainOpts { } struct Subcommand ( - &'static str, - fn(MainOpts, &[String]), + &'static str, // command + &'static str, // desc + fn(&Subcommand, MainOpts, &[String]), ); inventory::collect!(Subcommand); inventory::submit!{Subcommand( - "create-table", |mainopts, args|{ + "create-table", "Create a new table", |_sc, mainopts, args|{ eprintln!("CREATE-TABLE {:?} {:?}", &mainopts, &args); } )} @@ -113,7 +114,7 @@ fn parse_args( args: Vec, apmaker: &F, completer: &C, - extra_help: Option<&mut dyn FnMut(&mut dyn Write) -> Result<(), io::Error>>, + extra_help: Option<&dyn Fn(&mut dyn Write) -> Result<(), io::Error>>, ) -> T where T: Default, F: Fn(&mut T) -> ArgumentParser, @@ -192,15 +193,25 @@ fn main() { *scope = Some(ManagementScope::Unix { user }); } Ok(()) - }, None); + }, Some(&|w|{ + writeln!(w, "\nSubcommands:")?; + let maxlen = inventory::iter::.into_iter() + .map(|Subcommand(verb,_,_)| verb.len()) + .max().unwrap_or(0); + for Subcommand(verb,desc,_) in inventory::iter:: { + writeln!(w, " {:width$} {}", verb, desc, width=maxlen)?; + } + Ok(()) + })); - let Subcommand(_,call) = inventory::iter::.into_iter() - .filter(|Subcommand(found,_)| found == &ma.subcommand) + let sc = inventory::iter::.into_iter() + .filter(|Subcommand(found,_,_)| found == &ma.subcommand) .next() .unwrap_or_else(||{ eprintln!("subcommand `{}' not recognised", &ma.subcommand); exit(EXIT_USAGE); }); + let Subcommand(_,_,call) = sc; - call(ma.opts, &ma.subargs); + call(sc, ma.opts, &ma.subargs); } -- 2.30.2