Skip to content

Commit 6f4c341

Browse files
committed
api: Adds the traits to be used with the clap-derive crate to be able to use Custom Derive
Currently to use these traits clap must be built with the `unstable` feature. This does not require a nightly compiler. These traits and APIs may change without warning (hence the `unstable` feature flag). Once they have been stablelized and the `clap-derive` crate is released the `unstable` feature flag will no longer be required.
1 parent 8a26ff0 commit 6f4c341

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

src/lib.rs

+55
Original file line numberDiff line numberDiff line change
@@ -574,3 +574,58 @@ mod map;
574574
const INTERNAL_ERROR_MSG: &'static str = "Fatal internal error. Please consider filing a bug \
575575
report at https://github.com/kbknapp/clap-rs/issues";
576576
const INVALID_UTF8: &'static str = "unexpected invalid UTF-8 code point";
577+
578+
#[cfg(unstable)]
579+
pub use derive::{ArgEnum, ClapApp, IntoApp, FromArgMatches};
580+
581+
#[cfg(unstable)]
582+
mod derive {
583+
/// @TODO @release @docs
584+
pub trait ClapApp: IntoApp + FromArgMatches + Sized {
585+
586+
/// @TODO @release @docs
587+
fn parse() -> Self {
588+
Self::from_argmatches(Self::into_app().get_matches())
589+
}
590+
591+
/// @TODO @release @docs
592+
fn parse_from<I, T>(argv: I) -> Self
593+
where I: IntoIterator<Item = T>,
594+
T: Into<OsString> + Clone
595+
{
596+
Self::from_argmatches(Self::into_app().get_matches_from(argv))
597+
}
598+
599+
/// @TODO @release @docs
600+
fn try_parse() -> Result<Self, clap::Error> {
601+
Self::try_from_argmatches(Self::into_app().get_matches_safe()?)
602+
}
603+
604+
605+
/// @TODO @release @docs
606+
fn try_parse_from<I, T>(argv: I) -> Result<Self, clap::Error>
607+
where I: IntoIterator<Item = T>,
608+
T: Into<OsString> + Clone
609+
{
610+
Self::try_from_argmatches(Self::into_app().get_matches_from_safe(argv)?)
611+
}
612+
}
613+
614+
/// @TODO @release @docs
615+
pub trait IntoApp {
616+
/// @TODO @release @docs
617+
fn into_app<'a, 'b>() -> clap::App<'a, 'b>;
618+
}
619+
620+
/// @TODO @release @docs
621+
pub trait FromArgMatches: Sized {
622+
/// @TODO @release @docs
623+
fn from_argmatches<'a>(matches: clap::ArgMatches<'a>) -> Self;
624+
625+
/// @TODO @release @docs
626+
fn try_from_argmatches<'a>(matches: clap::ArgMatches<'a>) -> Result<Self, clap::Error>;
627+
}
628+
629+
/// @TODO @release @docs
630+
pub trait ArgEnum { }
631+
}

0 commit comments

Comments
 (0)