pub trait Parser:
Sized
+ FromArgMatches
+ CommandFactory {
// Provided methods
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 { ... }
}
Expand description
Parse command-line arguments into Self
.
The primary one-stop-shop trait used to create an instance of a clap
Command
, 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
+
CommandFactory
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
.
NOTE: Deriving requires the derive
feature flag
Provided Methods§
sourcefn parse() -> Self
fn parse() -> Self
Parse from std::env::args_os()
, exit on error.
Examples found in repository?
More examples
- examples/tutorial_derive/03_02_option_mult.rs
- examples/tutorial_derive/03_03_positional_mult.rs
- examples/tutorial_derive/03_05_default_values.rs
- examples/tutorial_derive/03_01_flag_bool.rs
- examples/tutorial_derive/03_01_flag_count.rs
- examples/tutorial_derive/03_02_option.rs
- examples/tutorial_derive/03_03_positional.rs
- examples/cargo-example-derive.rs
- examples/tutorial_derive/02_app_settings.rs
- examples/tutorial_derive/02_apps.rs
- examples/tutorial_derive/02_crate.rs
- examples/demo.rs
- examples/tutorial_derive/04_01_enum.rs
- examples/tutorial_derive/03_04_subcommands.rs
- examples/tutorial_derive/03_04_subcommands_alt.rs
- examples/escaped-positional-derive.rs
- examples/tutorial_derive/04_03_relations.rs
- examples/tutorial_derive/01_quick.rs
- examples/tutorial_derive/04_04_custom.rs
- examples/git-derive.rs
sourcefn parse_from<I, T>(itr: I) -> Self
fn parse_from<I, T>(itr: I) -> Self
Parse from iterator, exit on error.
sourcefn try_parse_from<I, T>(itr: I) -> Result<Self, Error>
fn try_parse_from<I, T>(itr: I) -> Result<Self, Error>
Parse from iterator, return Err on error.
Examples found in repository?
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
fn respond(line: &str) -> Result<bool, String> {
let args = shlex::split(line).ok_or("error: Invalid quoting")?;
let cli = Cli::try_parse_from(args).map_err(|e| e.to_string())?;
match cli.command {
Commands::Ping => {
write!(std::io::stdout(), "Pong").map_err(|e| e.to_string())?;
std::io::stdout().flush().map_err(|e| e.to_string())?;
}
Commands::Exit => {
write!(std::io::stdout(), "Exiting ...").map_err(|e| e.to_string())?;
std::io::stdout().flush().map_err(|e| e.to_string())?;
return Ok(true);
}
}
Ok(false)
}
sourcefn update_from<I, T>(&mut self, itr: I)
fn update_from<I, T>(&mut self, itr: I)
Update from iterator, exit on error.
Unlike Parser::parse
, this works with an existing instance of self
.
The assumption is that all required fields are already provided and any Args
or
Subcommand
s provided by the user will modify only what is specified.
sourcefn try_update_from<I, T>(&mut self, itr: I) -> Result<(), Error>
fn try_update_from<I, T>(&mut self, itr: I) -> Result<(), Error>
Update from iterator, return Err on error.