diff --git a/Cargo.toml b/Cargo.toml index d414ed5..7e8750a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -60,6 +60,12 @@ sysinit = [ "init" ] +# a dummy utility to help people to understand the framework +dummy = ["libmesabox/dummy"] +other = [ + "dummy" +] + # utilities that work on Unix unix = [ "gnu", diff --git a/libmesabox/Cargo.toml b/libmesabox/Cargo.toml index e5f6da4..c9f21b4 100644 --- a/libmesabox/Cargo.toml +++ b/libmesabox/Cargo.toml @@ -58,6 +58,12 @@ sysinit = [ "init" ] +# a dummy utility to help people to understand the framework +dummy = [] +other = [ + "dummy" +] + # utilities that work on Unix unix = [ "gnu", diff --git a/libmesabox/src/other/dummy.rs b/libmesabox/src/other/dummy.rs new file mode 100644 index 0000000..c69c6d2 --- /dev/null +++ b/libmesabox/src/other/dummy.rs @@ -0,0 +1,95 @@ +// +// Copyright (c) 2018, The MesaLock Linux Project Contributors +// All rights reserved. +// +// This work is licensed under the terms of the BSD 3-Clause License. +// For a copy, see the LICENSE file. +// + +use clap::{App, Arg, ArgMatches}; +use std::io::{self, Write}; +use {UtilSetup, ArgsIter, Result, UtilRead, UtilWrite}; + +const NAME: &str = "dummy"; +pub const DESCRIPTION: &str = "A dummy utility to demonstrate the framework"; + +type DummyResult = ::std::result::Result; + +#[derive(Fail, Debug)] +enum DummyError { + #[fail(display = "oh no, something wrong")] + SomethingWrong +} + +struct DummyOptions { + verbose: bool +} + +impl DummyOptions { + fn from_matches(matches: &ArgMatches) -> Self { + let mut options = Self::default(); + + options.verbose = matches.is_present("verbose"); + + options + } +} + +impl Default for DummyOptions { + fn default() -> Self { + Self { + verbose: false + } + } +} + +struct Dummyer +where + O: Write +{ + output: O +} + +impl Dummyer +where + O: Write +{ + fn new(output: O) -> Self { + Dummyer { output } + } + + fn dummy(&mut self, options: &DummyOptions) -> DummyResult<()> { + if options.verbose { + writeln!(self.output, "Hello, world! This is a dummy utility. I am very verbose :)"); + return Err(DummyError::SomethingWrong) + } else { + writeln!(self.output, "Hello, world!"); + } + Ok(()) + } +} + +fn create_app() -> App<'static, 'static> { + util_app!(NAME) + .arg(Arg::with_name("verbose") + .short("v") + .long("verbose") + .help("Say hello in verbose mode")) +} + +pub fn execute(setup: &mut S, args: T) -> Result<()> +where + S: UtilSetup, + T: ArgsIter, +{ + let app = create_app(); + let matches = app.get_matches_from_safe(args)?; + let options = DummyOptions::from_matches(&matches); + + let output = setup.output(); + let mut output = output.lock()?; + + let mut dummyer = Dummyer::new(output); + dummyer.dummy(&options)?; + Ok(()) +} diff --git a/libmesabox/src/util_list.rs b/libmesabox/src/util_list.rs index fab9b1f..c8679bb 100644 --- a/libmesabox/src/util_list.rs +++ b/libmesabox/src/util_list.rs @@ -27,5 +27,8 @@ generate_fns! { }, sysinit { (init, "init") + }, + other { + (dummy, "dummy") } }