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)
}
fn try_parse() -> Result<Self, Error> {
let matches = <Self as IntoApp>::into_app().try_get_matches()?;
Ok(<Self as FromArgMatches>::from_arg_matches(&matches))
}
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)
}
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))
}
}
pub trait IntoApp: Sized {
fn into_app<'b>() -> App<'b>;
fn augment_clap(app: App<'_>) -> App<'_>;
}
pub trait FromArgMatches: Sized {
fn from_arg_matches(matches: &ArgMatches) -> Self;
}
pub trait Subcommand: Sized {
fn from_subcommand(name: &str, matches: Option<&ArgMatches>) -> Option<Self>;
fn augment_subcommands(app: App<'_>) -> App<'_>;
}
pub trait ArgEnum {}
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<'b>() -> App<'b> {
<T as IntoApp>::into_app()
}
fn augment_clap(app: App<'_>) -> App<'_> {
<T as IntoApp>::augment_clap(app)
}
}
impl<T: FromArgMatches> FromArgMatches for Box<T> {
fn from_arg_matches(matches: &ArgMatches) -> Self {
Box::new(<T as FromArgMatches>::from_arg_matches(matches))
}
}
impl<T: Subcommand> Subcommand for Box<T> {
fn from_subcommand(name: &str, matches: Option<&ArgMatches>) -> Option<Self> {
<T as Subcommand>::from_subcommand(name, matches).map(Box::new)
}
fn augment_subcommands(app: App<'_>) -> App<'_> {
<T as Subcommand>::augment_subcommands(app)
}
}