use crate::{App, ArgMatches, Error};
use std::ffi::OsString;
pub trait Clap: FromArgMatches + IntoApp + Sized {
fn parse() -> Self {
let matches = <Self as IntoApp>::into_app().get_matches();
<Self as FromArgMatches>::from_arg_matches(&matches).expect("IntoApp validated everything")
}
fn try_parse() -> Result<Self, Error> {
let matches = <Self as IntoApp>::into_app().try_get_matches()?;
Ok(<Self as FromArgMatches>::from_arg_matches(&matches)
.expect("IntoApp validated everything"))
}
fn parse_from<I, T>(itr: I) -> Self
where
I: IntoIterator<Item = T>,
T: Into<OsString> + Clone,
{
let matches = <Self as IntoApp>::into_app().get_matches_from(itr);
<Self as FromArgMatches>::from_arg_matches(&matches).expect("IntoApp validated everything")
}
fn try_parse_from<I, T>(itr: I) -> Result<Self, Error>
where
I: IntoIterator<Item = T>,
T: Into<OsString> + Clone,
{
let matches = <Self as IntoApp>::into_app().try_get_matches_from(itr)?;
Ok(<Self as FromArgMatches>::from_arg_matches(&matches)
.expect("IntoApp validated everything"))
}
fn update_from<I, T>(&mut self, itr: I)
where
I: IntoIterator<Item = T>,
T: Into<OsString> + Clone,
{
let matches = <Self as IntoApp>::into_app_for_update().get_matches_from(itr);
<Self as FromArgMatches>::update_from_arg_matches(self, &matches);
}
fn try_update_from<I, T>(&mut self, itr: I) -> Result<(), Error>
where
I: IntoIterator<Item = T>,
T: Into<OsString> + Clone,
{
let matches = <Self as IntoApp>::into_app_for_update().try_get_matches_from(itr)?;
<Self as FromArgMatches>::update_from_arg_matches(self, &matches);
Ok(())
}
}
pub trait IntoApp: Sized {
fn into_app<'help>() -> App<'help>;
fn into_app_for_update<'help>() -> App<'help>;
}
pub trait FromArgMatches: Sized {
fn from_arg_matches(matches: &ArgMatches) -> Option<Self>;
fn update_from_arg_matches(&mut self, matches: &ArgMatches);
}
pub trait Args: FromArgMatches + Sized {
fn augment_args(app: App<'_>) -> App<'_>;
fn augment_args_for_update(app: App<'_>) -> App<'_>;
}
pub trait Subcommand: FromArgMatches + Sized {
fn augment_subcommands(app: App<'_>) -> App<'_>;
fn augment_subcommands_for_update(app: App<'_>) -> App<'_>;
fn has_subcommand(name: &str) -> bool;
}
pub trait ArgEnum: Sized {
const VARIANTS: &'static [&'static str];
fn from_str(input: &str, case_insensitive: bool) -> Result<Self, String>;
fn as_arg(&self) -> Option<&'static str>;
}
impl<T: Clap> Clap for Box<T> {
fn parse() -> Self {
Box::new(<T as Clap>::parse())
}
fn try_parse() -> Result<Self, Error> {
<T as Clap>::try_parse().map(Box::new)
}
fn parse_from<I, It>(itr: I) -> Self
where
I: IntoIterator<Item = It>,
It: Into<OsString> + Clone,
{
Box::new(<T as Clap>::parse_from(itr))
}
fn try_parse_from<I, It>(itr: I) -> Result<Self, Error>
where
I: IntoIterator<Item = It>,
It: Into<OsString> + Clone,
{
<T as Clap>::try_parse_from(itr).map(Box::new)
}
}
impl<T: IntoApp> IntoApp for Box<T> {
fn into_app<'help>() -> App<'help> {
<T as IntoApp>::into_app()
}
fn into_app_for_update<'help>() -> App<'help> {
<T as IntoApp>::into_app_for_update()
}
}
impl<T: FromArgMatches> FromArgMatches for Box<T> {
fn from_arg_matches(matches: &ArgMatches) -> Option<Self> {
<T as FromArgMatches>::from_arg_matches(matches).map(Box::new)
}
fn update_from_arg_matches(&mut self, matches: &ArgMatches) {
<T as FromArgMatches>::update_from_arg_matches(self, matches)
}
}
impl<T: Args> Args for Box<T> {
fn augment_args(app: App<'_>) -> App<'_> {
<T as Args>::augment_args(app)
}
fn augment_args_for_update(app: App<'_>) -> App<'_> {
<T as Args>::augment_args_for_update(app)
}
}
impl<T: Subcommand> Subcommand for Box<T> {
fn augment_subcommands(app: App<'_>) -> App<'_> {
<T as Subcommand>::augment_subcommands(app)
}
fn augment_subcommands_for_update(app: App<'_>) -> App<'_> {
<T as Subcommand>::augment_subcommands_for_update(app)
}
fn has_subcommand(name: &str) -> bool {
<T as Subcommand>::has_subcommand(name)
}
}