-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6184733
commit 17f9eca
Showing
3 changed files
with
73 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
use crate::{App, ArgMatches, Error}; | ||
use std::ffi::OsString; | ||
|
||
/// This trait is just a convenience on top of FromArgMatches + IntoApp | ||
pub trait Clap: FromArgMatches + IntoApp + Sized { | ||
/// Parse from `std::env::args()`, exit on error | ||
fn parse() -> Self { | ||
let matches = <Self as IntoApp>::into_app().get_matches(); | ||
<Self as FromArgMatches>::from_arg_matches(&matches) | ||
} | ||
|
||
/// Parse from `std::env::args()`, return Err on error. | ||
fn try_parse() -> Result<Self, Error> { | ||
let matches = <Self as IntoApp>::into_app().try_get_matches()?; | ||
Ok(<Self as FromArgMatches>::from_arg_matches(&matches)) | ||
} | ||
|
||
/// Parse from iterator, exit on error | ||
fn parse_from<I, T>(itr: I) -> Self | ||
where | ||
I: IntoIterator<Item = T>, | ||
// TODO (@CreepySkeleton): discover a way to avoid cloning here | ||
T: Into<OsString> + Clone, | ||
{ | ||
let matches = <Self as IntoApp>::into_app().get_matches_from(itr); | ||
<Self as FromArgMatches>::from_arg_matches(&matches) | ||
} | ||
|
||
/// Parse from `std::env::args()`, return Err on error. | ||
fn try_parse_from<I, T>(itr: I) -> Result<Self, Error> | ||
where | ||
I: IntoIterator<Item = T>, | ||
// TODO (@CreepySkeleton): discover a way to avoid cloning here | ||
T: Into<OsString> + Clone, | ||
{ | ||
let matches = <Self as IntoApp>::into_app().try_get_matches_from(itr)?; | ||
Ok(<Self as FromArgMatches>::from_arg_matches(&matches)) | ||
} | ||
} | ||
|
||
/// Build an App according to the struct | ||
/// | ||
/// Also serves for flattening | ||
pub trait IntoApp: Sized { | ||
/// @TODO @release @docs | ||
fn into_app<'b>() -> App<'b>; | ||
/// @TODO @release @docs | ||
fn augment_clap<'b>(app: App<'b>) -> App<'b>; | ||
} | ||
|
||
/// Extract values from ArgMatches into the struct. | ||
pub trait FromArgMatches: Sized { | ||
/// @TODO @release @docs | ||
fn from_arg_matches<'b>(matches: &ArgMatches) -> Self; | ||
} | ||
|
||
/// @TODO @release @docs | ||
pub trait Subcommand: Sized { | ||
/// @TODO @release @docs | ||
fn from_subcommand<'b>(name: &str, matches: Option<&ArgMatches>) -> Option<Self>; | ||
/// @TODO @release @docs | ||
fn augment_subcommands<'b>(app: App<'b>) -> App<'b>; | ||
} | ||
|
||
/// @TODO @release @docs | ||
pub trait ArgEnum {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters