Skip to content

Commit

Permalink
docs: Prefer get_flag over get_one::<bool>
Browse files Browse the repository at this point in the history
Inspired by #4654
  • Loading branch information
epage committed Jan 23, 2023
1 parent 74a82d7 commit 9d1de20
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 74 deletions.
2 changes: 1 addition & 1 deletion examples/tutorial_builder/01_quick.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ fn main() {
// matches just as you would the top level cmd
if let Some(matches) = matches.subcommand_matches("test") {
// "$ myapp test" was run
if *matches.get_one::<bool>("list").expect("defaulted by clap") {
if matches.get_flag("list") {
// "$ myapp test -l" was run
println!("Printing testing lists...");
} else {
Expand Down
16 changes: 8 additions & 8 deletions src/builder/action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,15 +99,15 @@ pub enum ArgAction {
/// let matches = cmd.clone().try_get_matches_from(["mycmd", "--flag"]).unwrap();
/// assert!(matches.contains_id("flag"));
/// assert_eq!(
/// matches.get_one::<bool>("flag").copied(),
/// Some(true)
/// matches.get_flag("flag"),
/// true
/// );
///
/// let matches = cmd.try_get_matches_from(["mycmd"]).unwrap();
/// assert!(matches.contains_id("flag"));
/// assert_eq!(
/// matches.get_one::<bool>("flag").copied(),
/// Some(false)
/// matches.get_flag("flag"),
/// false
/// );
/// ```
///
Expand Down Expand Up @@ -172,15 +172,15 @@ pub enum ArgAction {
/// let matches = cmd.clone().try_get_matches_from(["mycmd", "--flag"]).unwrap();
/// assert!(matches.contains_id("flag"));
/// assert_eq!(
/// matches.get_one::<bool>("flag").copied(),
/// Some(false)
/// matches.get_flag("flag"),
/// false
/// );
///
/// let matches = cmd.try_get_matches_from(["mycmd"]).unwrap();
/// assert!(matches.contains_id("flag"));
/// assert_eq!(
/// matches.get_one::<bool>("flag").copied(),
/// Some(true)
/// matches.get_flag("flag"),
/// true
/// );
/// ```
SetFalse,
Expand Down
28 changes: 14 additions & 14 deletions src/builder/arg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ impl Arg {
/// .get_matches_from(vec![
/// "prog", "--do-tests"
/// ]);
/// assert_eq!(*m.get_one::<bool>("test").expect("defaulted by clap"), true);
/// assert_eq!(m.get_flag("test"), true);
/// ```
#[must_use]
pub fn aliases(mut self, names: impl IntoIterator<Item = impl Into<Str>>) -> Self {
Expand Down Expand Up @@ -314,7 +314,7 @@ impl Arg {
/// .get_matches_from(vec![
/// "prog", "-s"
/// ]);
/// assert_eq!(*m.get_one::<bool>("test").expect("defaulted by clap"), true);
/// assert_eq!(m.get_flag("test"), true);
/// ```
#[must_use]
pub fn short_aliases(mut self, names: impl IntoIterator<Item = char>) -> Self {
Expand Down Expand Up @@ -399,7 +399,7 @@ impl Arg {
/// .get_matches_from(vec![
/// "prog", "--awesome"
/// ]);
/// assert_eq!(*m.get_one::<bool>("test").expect("defaulted by clap"), true);
/// assert_eq!(m.get_flag("test"), true);
/// ```
/// [`Command::aliases`]: Arg::aliases()
#[must_use]
Expand All @@ -425,7 +425,7 @@ impl Arg {
/// .get_matches_from(vec![
/// "prog", "-t"
/// ]);
/// assert_eq!(*m.get_one::<bool>("test").expect("defaulted by clap"), true);
/// assert_eq!(m.get_flag("test"), true);
/// ```
#[must_use]
pub fn visible_short_aliases(mut self, names: impl IntoIterator<Item = char>) -> Self {
Expand Down Expand Up @@ -807,7 +807,7 @@ impl Arg {
///
/// assert_eq!(m.subcommand_name(), Some("do-stuff"));
/// let sub_m = m.subcommand_matches("do-stuff").unwrap();
/// assert_eq!(*sub_m.get_one::<bool>("verb").expect("defaulted by clap"), true);
/// assert_eq!(sub_m.get_flag("verb"), true);
/// ```
///
/// [`Subcommand`]: crate::Subcommand
Expand Down Expand Up @@ -1900,9 +1900,9 @@ impl Arg {
/// "prog"
/// ]);
///
/// assert!(*m.get_one::<bool>("true_flag").unwrap());
/// assert!(!*m.get_one::<bool>("false_flag").unwrap());
/// assert!(!*m.get_one::<bool>("absent_flag").unwrap());
/// assert!(m.get_flag("true_flag"));
/// assert!(!m.get_flag("false_flag"));
/// assert!(!m.get_flag("absent_flag"));
/// ```
///
/// In this example, we show the variable coming from an option on the CLI:
Expand Down Expand Up @@ -3639,10 +3639,10 @@ impl Arg {
/// "prog", "-f", "-d", "-c"]);
/// // ^~~~~~~~~~~~^~~~~ flag is overridden by color
///
/// assert!(*m.get_one::<bool>("color").unwrap());
/// assert!(*m.get_one::<bool>("debug").unwrap()); // even though flag conflicts with debug, it's as if flag
/// assert!(m.get_flag("color"));
/// assert!(m.get_flag("debug")); // even though flag conflicts with debug, it's as if flag
/// // was never used because it was overridden with color
/// assert!(!*m.get_one::<bool>("flag").unwrap());
/// assert!(!m.get_flag("flag"));
/// ```
#[must_use]
pub fn overrides_with(mut self, arg_id: impl IntoResettable<Id>) -> Self {
Expand Down Expand Up @@ -3678,11 +3678,11 @@ impl Arg {
/// "prog", "-f", "-d", "-c"]);
/// // ^~~~~~^~~~~~~~~ flag and debug are overridden by color
///
/// assert!(*m.get_one::<bool>("color").unwrap()); // even though flag conflicts with color, it's as if flag
/// assert!(m.get_flag("color")); // even though flag conflicts with color, it's as if flag
/// // and debug were never used because they were overridden
/// // with color
/// assert!(!*m.get_one::<bool>("debug").unwrap());
/// assert!(!*m.get_one::<bool>("flag").unwrap());
/// assert!(!m.get_flag("debug"));
/// assert!(!m.get_flag("flag"));
/// ```
#[must_use]
pub fn overrides_with_all(mut self, names: impl IntoIterator<Item = impl Into<Id>>) -> Self {
Expand Down
16 changes: 8 additions & 8 deletions src/builder/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ impl Command {
///
/// fn main() {
/// let m = cmd().get_matches_from(vec!["foo", "-b"]);
/// println!("{}", *m.get_one::<bool>("bar").expect("defaulted by clap"));
/// println!("{}", m.get_flag("bar"));
/// }
/// ```
pub fn debug_assert(mut self) {
Expand Down Expand Up @@ -963,7 +963,7 @@ impl Command {
/// assert!(r.is_ok(), "unexpected error: {:?}", r);
/// let m = r.unwrap();
/// assert_eq!(m.get_one::<String>("config").unwrap(), "file");
/// assert!(*m.get_one::<bool>("f").expect("defaulted"));
/// assert!(m.get_flag("f"));
/// assert_eq!(m.get_one::<String>("stuff"), None);
/// ```
#[inline]
Expand Down Expand Up @@ -1943,8 +1943,8 @@ impl Command {
/// .replace("--save-all", &["--save-context", "--save-runtime"])
/// .get_matches_from(vec!["cmd", "--save-all"]);
///
/// assert!(*m.get_one::<bool>("save-context").expect("defaulted by clap"));
/// assert!(*m.get_one::<bool>("save-runtime").expect("defaulted by clap"));
/// assert!(m.get_flag("save-context"));
/// assert!(m.get_flag("save-runtime"));
/// ```
///
/// This can also be used with options, for example if our application with
Expand All @@ -1969,8 +1969,8 @@ impl Command {
/// .replace("--save-all", &["--save-context", "--save-runtime", "--format=json"])
/// .get_matches_from(vec!["cmd", "--save-all"]);
///
/// assert!(*m.get_one::<bool>("save-context").expect("defaulted by clap"));
/// assert!(*m.get_one::<bool>("save-runtime").expect("defaulted by clap"));
/// assert!(m.get_flag("save-context"));
/// assert!(m.get_flag("save-runtime"));
/// assert_eq!(m.get_one::<String>("format").unwrap(), "json");
/// ```
///
Expand Down Expand Up @@ -2191,7 +2191,7 @@ impl Command {
///
/// assert_eq!(matches.subcommand_name().unwrap(), "sync");
/// let sync_matches = matches.subcommand_matches("sync").unwrap();
/// assert!(*sync_matches.get_one::<bool>("search").expect("defaulted by clap"));
/// assert!(sync_matches.get_flag("search"));
/// ```
/// [`Arg::short`]: Arg::short()
#[must_use]
Expand Down Expand Up @@ -2228,7 +2228,7 @@ impl Command {
///
/// assert_eq!(matches.subcommand_name().unwrap(), "sync");
/// let sync_matches = matches.subcommand_matches("sync").unwrap();
/// assert!(*sync_matches.get_one::<bool>("search").expect("defaulted by clap"));
/// assert!(sync_matches.get_flag("search"));
/// ```
///
/// [`Arg::long`]: Arg::long()
Expand Down
6 changes: 3 additions & 3 deletions src/derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ use std::ffi::OsString;
/// impl From<ArgMatches> for Context {
/// fn from(m: ArgMatches) -> Self {
/// Context {
/// verbose: *m.get_one::<bool>("verbose").expect("defaulted_by_clap"),
/// verbose: m.get_flag("verbose"),
/// name: m.get_one::<String>("name").cloned(),
/// }
/// }
Expand Down Expand Up @@ -201,7 +201,7 @@ pub trait FromArgMatches: Sized {
/// fn from(m: ArgMatches) -> Self {
/// Context {
/// name: m.get_one::<String>("name").unwrap().clone(),
/// debug: *m.get_one::<bool>("debug").expect("defaulted by clap"),
/// debug: m.get_flag("debug"),
/// }
/// }
/// }
Expand Down Expand Up @@ -235,7 +235,7 @@ pub trait FromArgMatches: Sized {
/// fn from(m: ArgMatches) -> Self {
/// Context {
/// name: m.get_one::<String>("name").unwrap().to_string(),
/// debug: *m.get_one::<bool>("debug").expect("defaulted by clap"),
/// debug: m.get_flag("debug"),
/// }
/// }
/// }
Expand Down
2 changes: 1 addition & 1 deletion src/parser/matches/arg_matches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1005,7 +1005,7 @@ impl ArgMatches {
/// ]);
///
/// // Both parent commands, and child subcommands can have arguments present at the same times
/// assert!(*app_m.get_one::<bool>("debug").expect("defaulted by clap"));
/// assert!(app_m.get_flag("debug"));
///
/// // Get the subcommand's ArgMatches instance
/// if let Some(sub_m) = app_m.subcommand_matches("test") {
Expand Down
Loading

0 comments on commit 9d1de20

Please sign in to comment.