Trait clap::Parser [−][src]
pub trait Parser: FromArgMatches + IntoApp + Sized {
fn parse() -> Self { ... }
fn try_parse() -> Result<Self, Error> { ... }
fn parse_from<I, T>(itr: I) -> Self
where
I: IntoIterator<Item = T>,
T: Into<OsString> + Clone,
{ ... }
fn try_parse_from<I, T>(itr: I) -> Result<Self, Error>
where
I: IntoIterator<Item = T>,
T: Into<OsString> + Clone,
{ ... }
fn update_from<I, T>(&mut self, itr: I)
where
I: IntoIterator<Item = T>,
T: Into<OsString> + Clone,
{ ... }
fn try_update_from<I, T>(&mut self, itr: I) -> Result<(), Error>
where
I: IntoIterator<Item = T>,
T: Into<OsString> + Clone,
{ ... }
fn clap<'help>() -> App<'help> { ... }
fn from_clap(matches: &ArgMatches) -> Self { ... }
fn from_args() -> Self { ... }
fn from_args_safe() -> Result<Self, Error> { ... }
fn from_iter<I, T>(itr: I) -> Self
where
I: IntoIterator<Item = T>,
T: Into<OsString> + Clone,
{ ... }
fn from_iter_safe<I, T>(itr: I) -> Result<Self, Error>
where
I: IntoIterator<Item = T>,
T: Into<OsString> + Clone,
{ ... }
}
Expand description
Parse command-line arguments into Self
.
The primary one-stop-shop trait used to create an instance of a clap
App
, conduct the parsing, and turn the resulting ArgMatches
back
into concrete instance of the user struct.
This trait is primarily a convenience on top of FromArgMatches
+
IntoApp
which uses those two underlying traits to build the two
fundamental functions parse
which uses the std::env::args_os
iterator,
and parse_from
which allows the consumer to supply the iterator (along
with fallible options for each).
See also Subcommand
and Args
.
See the derive reference for attributes and best practices.
NOTE: Deriving requires the derive
feature flag
Examples
The following example creates a Context
struct that would be used
throughout the application representing the normalized values coming from
the CLI.
/// My super CLI
#[derive(clap::Parser)]
#[clap(name = "demo")]
struct Context {
/// More verbose output
#[clap(long)]
verbose: bool,
/// An optional name
#[clap(short, long)]
name: Option<String>,
}
The equivalent App
struct + From
implementation:
App::new("demo")
.about("My super CLI")
.arg(Arg::new("verbose")
.long("verbose")
.help("More verbose output"))
.arg(Arg::new("name")
.long("name")
.short('n')
.help("An optional name")
.takes_value(true));
struct Context {
verbose: bool,
name: Option<String>,
}
impl From<ArgMatches> for Context {
fn from(m: ArgMatches) -> Self {
Context {
verbose: m.is_present("verbose"),
name: m.value_of("name").map(|n| n.to_owned()),
}
}
}
Provided methods
fn parse_from<I, T>(itr: I) -> Self where
I: IntoIterator<Item = T>,
T: Into<OsString> + Clone,
fn parse_from<I, T>(itr: I) -> Self where
I: IntoIterator<Item = T>,
T: Into<OsString> + Clone,
Parse from iterator, exit on error
fn try_parse_from<I, T>(itr: I) -> Result<Self, Error> where
I: IntoIterator<Item = T>,
T: Into<OsString> + Clone,
fn try_parse_from<I, T>(itr: I) -> Result<Self, Error> where
I: IntoIterator<Item = T>,
T: Into<OsString> + Clone,
Parse from iterator, return Err on error.
fn update_from<I, T>(&mut self, itr: I) where
I: IntoIterator<Item = T>,
T: Into<OsString> + Clone,
fn update_from<I, T>(&mut self, itr: I) where
I: IntoIterator<Item = T>,
T: Into<OsString> + Clone,
Update from iterator, exit on error
fn try_update_from<I, T>(&mut self, itr: I) -> Result<(), Error> where
I: IntoIterator<Item = T>,
T: Into<OsString> + Clone,
fn try_update_from<I, T>(&mut self, itr: I) -> Result<(), Error> where
I: IntoIterator<Item = T>,
T: Into<OsString> + Clone,
Update from iterator, return Err on error.
StructOpt::clap
is replaced with IntoApp::into_app
(derived as part of Parser
)
Deprecated, StructOpt::clap
replaced with IntoApp::into_app
(derive as part of
Parser
)
fn from_clap(matches: &ArgMatches) -> Self
fn from_clap(matches: &ArgMatches) -> Self
StructOpt::from_clap
is replaced with FromArgMatches::from_arg_matches
(derived as part of Parser
)
Deprecated, StructOpt::from_clap
replaced with FromArgMatches::from_arg_matches
(derive as part of
Parser
)
StructOpt::from_args
is replaced with Parser::parse
(note the change in derives)
Deprecated, StructOpt::from_args
replaced with Parser::parse
(note the change in derives)
fn from_args_safe() -> Result<Self, Error>
fn from_args_safe() -> Result<Self, Error>
StructOpt::from_args_safe
is replaced with Parser::try_parse
(note the change in derives)
Deprecated, StructOpt::from_args_safe
replaced with Parser::try_parse
(note the change in derives)
StructOpt::from_iter
is replaced with Parser::parse_from
(note the change in derives)
Deprecated, StructOpt::from_iter
replaced with Parser::parse_from
(note the change in derives)
fn from_iter_safe<I, T>(itr: I) -> Result<Self, Error> where
I: IntoIterator<Item = T>,
T: Into<OsString> + Clone,
fn from_iter_safe<I, T>(itr: I) -> Result<Self, Error> where
I: IntoIterator<Item = T>,
T: Into<OsString> + Clone,
StructOpt::from_iter_safe
is replaced with Parser::try_parse_from
(note the change in derives)
Deprecated, StructOpt::from_iter_safe
replaced with Parser::try_parse_from
(note the
change in derives)