diff --git a/libimagrt/src/lib.rs b/libimagrt/src/lib.rs index 63a49c45..55398013 100644 --- a/libimagrt/src/lib.rs +++ b/libimagrt/src/lib.rs @@ -53,4 +53,5 @@ pub mod configuration; pub mod logger; pub mod runtime; pub mod setup; +pub mod spec; diff --git a/libimagrt/src/runtime.rs b/libimagrt/src/runtime.rs index e0df57fc..0c1b919b 100644 --- a/libimagrt/src/runtime.rs +++ b/libimagrt/src/runtime.rs @@ -36,6 +36,7 @@ use error::MapErrInto; use logger::ImagLogger; use libimagstore::store::Store; +use spec::CliSpec; /// The Runtime object /// @@ -55,7 +56,7 @@ impl<'a> Runtime<'a> { /// and builds the Runtime object with it. /// /// The cli_spec object should be initially build with the ::get_default_cli_builder() function. - pub fn new(mut cli_spec: App<'a, 'a>) -> Result, RuntimeError> { + pub fn new>(mut cli_spec: C) -> Result, RuntimeError> { use std::env; use std::io::stdout; @@ -66,7 +67,7 @@ impl<'a> Runtime<'a> { use configuration::error::ConfigErrorKind; - let matches = cli_spec.clone().get_matches(); + let matches = cli_spec.clone().matches(); let is_debugging = matches.is_present("debugging"); let is_verbose = matches.is_present("verbosity"); @@ -78,8 +79,8 @@ impl<'a> Runtime<'a> { Some(shell) => { debug!("Generating shell completion script, writing to stdout"); let shell = shell.parse::().unwrap(); // clap has our back here. - let appname = String::from(cli_spec.get_name()); - cli_spec.gen_completions_to(appname, shell, &mut stdout()); + let appname = String::from(cli_spec.name()); + cli_spec.completions(appname, shell, &mut stdout()); }, _ => debug!("Not generating shell completion script"), } diff --git a/libimagrt/src/spec.rs b/libimagrt/src/spec.rs new file mode 100644 index 00000000..03aa7a85 --- /dev/null +++ b/libimagrt/src/spec.rs @@ -0,0 +1,27 @@ +use std::io::Write; + +use clap::{App, ArgMatches, Shell}; + +pub trait CliSpec<'a> { + fn name(&self) -> &str; + fn matches(self) -> ArgMatches<'a>; + fn completions>(&mut self, _: S, _: Shell, _: &mut W) {} +} + +impl<'a> CliSpec<'a> for App<'a, 'a> { + fn name(&self) -> &str { + self.get_name() + } + + fn matches(self) -> ArgMatches<'a> { + self.get_matches() + } + + fn completions>(&mut self, + bin_name: S, + for_shell: Shell, + buf: &mut W) { + + self.gen_completions_to(bin_name, for_shell, buf); + } +}