diff --git a/clap_builder/src/builder/arg.rs b/clap_builder/src/builder/arg.rs index 5634078c500..e5ec9c0692d 100644 --- a/clap_builder/src/builder/arg.rs +++ b/clap_builder/src/builder/arg.rs @@ -109,14 +109,15 @@ impl Arg { /// ``` /// [`Arg::action(ArgAction::Set)`]: Arg::action() pub fn new(id: impl Into) -> Self { - Arg::default().id(id) + let mut arg = Arg::default(); + arg.id(id); + arg } /// Set the identifier used for referencing this argument in the clap API. /// /// See [`Arg::new`] for more details. - #[must_use] - pub fn id(mut self, id: impl Into) -> Self { + pub fn id(&mut self, id: impl Into) -> &mut Self { self.id = id.into(); self } @@ -167,8 +168,7 @@ impl Arg { /// assert_eq!(m.get_one::("host").map(String::as_str), Some("wikipedia.org")); /// ``` #[inline] - #[must_use] - pub fn short(mut self, s: impl IntoResettable) -> Self { + pub fn short(&mut self, s: impl IntoResettable) -> &mut Self { if let Some(s) = s.into_resettable().into_option() { debug_assert!(s != '-', "short option name cannot be `-`"); self.short = Some(s); @@ -209,8 +209,7 @@ impl Arg { /// assert_eq!(m.get_one::("cfg").map(String::as_str), Some("file.toml")); /// ``` #[inline] - #[must_use] - pub fn long(mut self, l: impl IntoResettable) -> Self { + pub fn long(&mut self, l: impl IntoResettable) -> &mut Self { self.long = l.into_resettable().into_option(); self } @@ -235,8 +234,7 @@ impl Arg { /// ]); /// assert_eq!(m.get_one::("test").unwrap(), "cool"); /// ``` - #[must_use] - pub fn alias(mut self, name: impl IntoResettable) -> Self { + pub fn alias(&mut self, name: impl IntoResettable) -> &mut Self { if let Some(name) = name.into_resettable().into_option() { self.aliases.push((name, false)); } else { @@ -265,8 +263,7 @@ impl Arg { /// ]); /// assert_eq!(m.get_one::("test").unwrap(), "cool"); /// ``` - #[must_use] - pub fn short_alias(mut self, name: impl IntoResettable) -> Self { + pub fn short_alias(&mut self, name: impl IntoResettable) -> &mut Self { if let Some(name) = name.into_resettable().into_option() { debug_assert!(name != '-', "short alias name cannot be `-`"); self.short_aliases.push((name, false)); @@ -298,8 +295,7 @@ impl Arg { /// ]); /// assert_eq!(m.get_flag("test"), true); /// ``` - #[must_use] - pub fn aliases(mut self, names: impl IntoIterator>) -> Self { + pub fn aliases(&mut self, names: impl IntoIterator>) -> &mut Self { self.aliases .extend(names.into_iter().map(|x| (x.into(), false))); self @@ -327,8 +323,7 @@ impl Arg { /// ]); /// assert_eq!(m.get_flag("test"), true); /// ``` - #[must_use] - pub fn short_aliases(mut self, names: impl IntoIterator) -> Self { + pub fn short_aliases(&mut self, names: impl IntoIterator) -> &mut Self { for s in names { debug_assert!(s != '-', "short alias name cannot be `-`"); self.short_aliases.push((s, false)); @@ -356,8 +351,7 @@ impl Arg { /// assert_eq!(m.get_one::("test").unwrap(), "coffee"); /// ``` /// [`Command::alias`]: Arg::alias() - #[must_use] - pub fn visible_alias(mut self, name: impl IntoResettable) -> Self { + pub fn visible_alias(&mut self, name: impl IntoResettable) -> &mut Self { if let Some(name) = name.into_resettable().into_option() { self.aliases.push((name, true)); } else { @@ -385,8 +379,7 @@ impl Arg { /// ]); /// assert_eq!(m.get_one::("test").unwrap(), "coffee"); /// ``` - #[must_use] - pub fn visible_short_alias(mut self, name: impl IntoResettable) -> Self { + pub fn visible_short_alias(&mut self, name: impl IntoResettable) -> &mut Self { if let Some(name) = name.into_resettable().into_option() { debug_assert!(name != '-', "short alias name cannot be `-`"); self.short_aliases.push((name, true)); @@ -416,8 +409,10 @@ impl Arg { /// assert_eq!(m.get_flag("test"), true); /// ``` /// [`Command::aliases`]: Arg::aliases() - #[must_use] - pub fn visible_aliases(mut self, names: impl IntoIterator>) -> Self { + pub fn visible_aliases( + &mut self, + names: impl IntoIterator>, + ) -> &mut Self { self.aliases .extend(names.into_iter().map(|n| (n.into(), true))); self @@ -442,8 +437,7 @@ impl Arg { /// ]); /// assert_eq!(m.get_flag("test"), true); /// ``` - #[must_use] - pub fn visible_short_aliases(mut self, names: impl IntoIterator) -> Self { + pub fn visible_short_aliases(&mut self, names: impl IntoIterator) -> &mut Self { for n in names { debug_assert!(n != '-', "short alias name cannot be `-`"); self.short_aliases.push((n, true)); @@ -504,8 +498,7 @@ impl Arg { /// [`Arg::num_args(true)`]: Arg::num_args() /// [`Command`]: crate::Command #[inline] - #[must_use] - pub fn index(mut self, idx: impl IntoResettable) -> Self { + pub fn index(&mut self, idx: impl IntoResettable) -> &mut Self { self.index = idx.into_resettable().into_option(); self } @@ -534,7 +527,7 @@ impl Arg { /// assert_eq!(trail, ["arg1", "-r", "val1"]); /// ``` /// [`Arg::num_args(..)`]: crate::Arg::num_args() - pub fn trailing_var_arg(self, yes: bool) -> Self { + pub fn trailing_var_arg(&mut self, yes: bool) -> &mut Self { if yes { self.setting(ArgSettings::TrailingVarArg) } else { @@ -622,8 +615,7 @@ impl Arg { /// [index]: Arg::index() /// [`UnknownArgument`]: crate::error::ErrorKind::UnknownArgument #[inline] - #[must_use] - pub fn last(self, yes: bool) -> Self { + pub fn last(&mut self, yes: bool) -> &mut Self { if yes { self.setting(ArgSettings::Last) } else { @@ -688,8 +680,7 @@ impl Arg { /// assert_eq!(res.unwrap_err().kind(), ErrorKind::MissingRequiredArgument); /// ``` #[inline] - #[must_use] - pub fn required(self, yes: bool) -> Self { + pub fn required(&mut self, yes: bool) -> &mut Self { if yes { self.setting(ArgSettings::Required) } else { @@ -754,8 +745,7 @@ impl Arg { /// [`Arg::requires(name)`]: Arg::requires() /// [Conflicting]: Arg::conflicts_with() /// [override]: Arg::overrides_with() - #[must_use] - pub fn requires(mut self, arg_id: impl IntoResettable) -> Self { + pub fn requires(&mut self, arg_id: impl IntoResettable) -> &mut Self { if let Some(arg_id) = arg_id.into_resettable().into_option() { self.requires.push((ArgPredicate::IsPresent, arg_id)); } else { @@ -798,8 +788,7 @@ impl Arg { /// assert_eq!(res.unwrap_err().kind(), ErrorKind::ArgumentConflict); /// ``` #[inline] - #[must_use] - pub fn exclusive(self, yes: bool) -> Self { + pub fn exclusive(&mut self, yes: bool) -> &mut Self { if yes { self.setting(ArgSettings::Exclusive) } else { @@ -842,8 +831,7 @@ impl Arg { /// /// [`Subcommand`]: crate::Subcommand #[inline] - #[must_use] - pub fn global(self, yes: bool) -> Self { + pub fn global(&mut self, yes: bool) -> &mut Self { if yes { self.setting(ArgSettings::Global) } else { @@ -857,15 +845,13 @@ impl Arg { } #[inline] - #[must_use] - pub(crate) fn setting(mut self, setting: ArgSettings) -> Self { + pub(crate) fn setting(&mut self, setting: ArgSettings) -> &mut Self { self.settings.set(setting); self } #[inline] - #[must_use] - pub(crate) fn unset_setting(mut self, setting: ArgSettings) -> Self { + pub(crate) fn unset_setting(&mut self, setting: ArgSettings) -> &mut Self { self.settings.unset(setting); self } @@ -903,8 +889,7 @@ impl Arg { /// ); /// ``` #[inline] - #[must_use] - pub fn action(mut self, action: impl IntoResettable) -> Self { + pub fn action(&mut self, action: impl IntoResettable) -> &mut Self { self.action = action.into_resettable().into_option(); self } @@ -966,7 +951,7 @@ impl Arg { /// .expect("required"); /// assert_eq!(port, 3001); /// ``` - pub fn value_parser(mut self, parser: impl IntoResettable) -> Self { + pub fn value_parser(&mut self, parser: impl IntoResettable) -> &mut Self { self.value_parser = parser.into_resettable().into_option(); self } @@ -1122,8 +1107,7 @@ impl Arg { /// assert_eq!(m.get_one::("word").unwrap(), "word"); /// ``` #[inline] - #[must_use] - pub fn num_args(mut self, qty: impl IntoResettable) -> Self { + pub fn num_args(&mut self, qty: impl IntoResettable) -> &mut Self { self.num_vals = qty.into_resettable().into_option(); self } @@ -1133,7 +1117,7 @@ impl Arg { feature = "deprecated", deprecated(since = "4.0.0", note = "Replaced with `Arg::num_args`") )] - pub fn number_of_values(self, qty: usize) -> Self { + pub fn number_of_values(&mut self, qty: usize) -> &mut Self { self.num_args(qty) } @@ -1186,8 +1170,7 @@ impl Arg { /// [positional]: Arg::index() /// [`Arg::action(ArgAction::Set)`]: Arg::action() #[inline] - #[must_use] - pub fn value_name(mut self, name: impl IntoResettable) -> Self { + pub fn value_name(&mut self, name: impl IntoResettable) -> &mut Self { if let Some(name) = name.into_resettable().into_option() { self.value_names([name]) } else { @@ -1252,8 +1235,7 @@ impl Arg { /// [`Arg::num_args`]: Arg::num_args() /// [`Arg::action(ArgAction::Set)`]: Arg::action() /// [`Arg::num_args(1..)`]: Arg::num_args() - #[must_use] - pub fn value_names(mut self, names: impl IntoIterator>) -> Self { + pub fn value_names(&mut self, names: impl IntoIterator>) -> &mut Self { self.val_names = names.into_iter().map(|s| s.into()).collect(); self } @@ -1289,8 +1271,7 @@ impl Arg { /// .value_hint(ValueHint::CommandWithArguments) /// ); /// ``` - #[must_use] - pub fn value_hint(mut self, value_hint: impl IntoResettable) -> Self { + pub fn value_hint(&mut self, value_hint: impl IntoResettable) -> &mut Self { self.value_hint = value_hint.into_resettable().into_option(); self } @@ -1346,8 +1327,7 @@ impl Arg { /// assert_eq!(&*matched_vals, &["TeSt123", "teST123", "tESt321"]); /// ``` #[inline] - #[must_use] - pub fn ignore_case(self, yes: bool) -> Self { + pub fn ignore_case(&mut self, yes: bool) -> &mut Self { if yes { self.setting(ArgSettings::IgnoreCase) } else { @@ -1410,8 +1390,7 @@ impl Arg { /// ``` /// [`Arg::num_args(1)`]: Arg::num_args() #[inline] - #[must_use] - pub fn allow_hyphen_values(self, yes: bool) -> Self { + pub fn allow_hyphen_values(&mut self, yes: bool) -> &mut Self { if yes { self.setting(ArgSettings::AllowHyphenValues) } else { @@ -1441,7 +1420,7 @@ impl Arg { /// assert_eq!(m.get_one::("num").unwrap(), "-20"); /// ``` #[inline] - pub fn allow_negative_numbers(self, yes: bool) -> Self { + pub fn allow_negative_numbers(&mut self, yes: bool) -> &mut Self { if yes { self.setting(ArgSettings::AllowNegativeNumbers) } else { @@ -1494,8 +1473,7 @@ impl Arg { /// assert_eq!(res.unwrap_err().kind(), ErrorKind::NoEquals); /// ``` #[inline] - #[must_use] - pub fn require_equals(self, yes: bool) -> Self { + pub fn require_equals(&mut self, yes: bool) -> &mut Self { if yes { self.setting(ArgSettings::RequireEquals) } else { @@ -1508,7 +1486,7 @@ impl Arg { feature = "deprecated", deprecated(since = "4.0.0", note = "Replaced with `Arg::value_delimiter`") )] - pub fn use_value_delimiter(mut self, yes: bool) -> Self { + pub fn use_value_delimiter(&mut self, yes: bool) -> &mut Self { if yes { self.val_delim.get_or_insert(','); } else { @@ -1544,8 +1522,7 @@ impl Arg { /// [`Arg::value_delimiter(',')`]: Arg::value_delimiter() /// [`Arg::action(ArgAction::Set)`]: Arg::action() #[inline] - #[must_use] - pub fn value_delimiter(mut self, d: impl IntoResettable) -> Self { + pub fn value_delimiter(&mut self, d: impl IntoResettable) -> &mut Self { self.val_delim = d.into_resettable().into_option(); self } @@ -1599,8 +1576,7 @@ impl Arg { /// [`num_args(1..)`]: Arg::num_args() /// [`num_args`]: Arg::num_args() #[inline] - #[must_use] - pub fn value_terminator(mut self, term: impl IntoResettable) -> Self { + pub fn value_terminator(&mut self, term: impl IntoResettable) -> &mut Self { self.terminator = term.into_resettable().into_option(); self } @@ -1628,8 +1604,7 @@ impl Arg { /// [`Arg::allow_hyphen_values(true)`]: Arg::allow_hyphen_values() /// [`Arg::last(true)`]: Arg::last() #[inline] - #[must_use] - pub fn raw(mut self, yes: bool) -> Self { + pub fn raw(&mut self, yes: bool) -> &mut Self { if yes { self.num_vals.get_or_insert_with(|| (1..).into()); } @@ -1693,8 +1668,7 @@ impl Arg { /// [`ArgMatches::contains_id`]: crate::ArgMatches::contains_id() /// [`Arg::default_value_if`]: Arg::default_value_if() #[inline] - #[must_use] - pub fn default_value(mut self, val: impl IntoResettable) -> Self { + pub fn default_value(&mut self, val: impl IntoResettable) -> &mut Self { if let Some(val) = val.into_resettable().into_option() { self.default_values([val]) } else { @@ -1704,13 +1678,12 @@ impl Arg { } #[inline] - #[must_use] #[doc(hidden)] #[cfg_attr( feature = "deprecated", deprecated(since = "4.0.0", note = "Replaced with `Arg::default_value`") )] - pub fn default_value_os(self, val: impl Into) -> Self { + pub fn default_value_os(&mut self, val: impl Into) -> &mut Self { self.default_values([val]) } @@ -1720,20 +1693,24 @@ impl Arg { /// /// [`Arg::default_value`]: Arg::default_value() #[inline] - #[must_use] - pub fn default_values(mut self, vals: impl IntoIterator>) -> Self { + pub fn default_values( + &mut self, + vals: impl IntoIterator>, + ) -> &mut Self { self.default_vals = vals.into_iter().map(|s| s.into()).collect(); self } #[inline] - #[must_use] #[doc(hidden)] #[cfg_attr( feature = "deprecated", deprecated(since = "4.0.0", note = "Replaced with `Arg::default_values`") )] - pub fn default_values_os(self, vals: impl IntoIterator>) -> Self { + pub fn default_values_os( + &mut self, + vals: impl IntoIterator>, + ) -> &mut Self { self.default_values(vals) } @@ -1831,8 +1808,7 @@ impl Arg { /// [`Arg::action(ArgAction::Set)`]: Arg::action() /// [`Arg::default_value`]: Arg::default_value() #[inline] - #[must_use] - pub fn default_missing_value(mut self, val: impl IntoResettable) -> Self { + pub fn default_missing_value(&mut self, val: impl IntoResettable) -> &mut Self { if let Some(val) = val.into_resettable().into_option() { self.default_missing_values_os([val]) } else { @@ -1848,8 +1824,7 @@ impl Arg { /// [`Arg::default_missing_value`]: Arg::default_missing_value() /// [`OsStr`]: std::ffi::OsStr #[inline] - #[must_use] - pub fn default_missing_value_os(self, val: impl Into) -> Self { + pub fn default_missing_value_os(&mut self, val: impl Into) -> &mut Self { self.default_missing_values_os([val]) } @@ -1859,8 +1834,10 @@ impl Arg { /// /// [`Arg::default_missing_value`]: Arg::default_missing_value() #[inline] - #[must_use] - pub fn default_missing_values(self, vals: impl IntoIterator>) -> Self { + pub fn default_missing_values( + &mut self, + vals: impl IntoIterator>, + ) -> &mut Self { self.default_missing_values_os(vals) } @@ -1871,11 +1848,10 @@ impl Arg { /// [`Arg::default_missing_values`]: Arg::default_missing_values() /// [`OsStr`]: std::ffi::OsStr #[inline] - #[must_use] pub fn default_missing_values_os( - mut self, + &mut self, vals: impl IntoIterator>, - ) -> Self { + ) -> &mut Self { self.default_missing_vals = vals.into_iter().map(|s| s.into()).collect(); self } @@ -2034,8 +2010,7 @@ impl Arg { /// [`Arg::value_delimiter(',')`]: Arg::value_delimiter() #[cfg(feature = "env")] #[inline] - #[must_use] - pub fn env(mut self, name: impl IntoResettable) -> Self { + pub fn env(&mut self, name: impl IntoResettable) -> &mut Self { if let Some(name) = name.into_resettable().into_option() { let value = env::var_os(&name); self.env = Some((name, value)); @@ -2051,7 +2026,7 @@ impl Arg { feature = "deprecated", deprecated(since = "4.0.0", note = "Replaced with `Arg::env`") )] - pub fn env_os(self, name: impl Into) -> Self { + pub fn env_os(&mut self, name: impl Into) -> &mut Self { self.env(name) } } @@ -2103,8 +2078,7 @@ impl Arg { /// ``` /// [`Arg::long_help`]: Arg::long_help() #[inline] - #[must_use] - pub fn help(mut self, h: impl IntoResettable) -> Self { + pub fn help(&mut self, h: impl IntoResettable) -> &mut Self { self.help = h.into_resettable().into_option(); self } @@ -2167,8 +2141,7 @@ impl Arg { /// ``` /// [`Arg::help`]: Arg::help() #[inline] - #[must_use] - pub fn long_help(mut self, h: impl IntoResettable) -> Self { + pub fn long_help(&mut self, h: impl IntoResettable) -> &mut Self { self.long_help = h.into_resettable().into_option(); self } @@ -2234,8 +2207,7 @@ impl Arg { /// [positional arguments]: Arg::index() /// [index]: Arg::index() #[inline] - #[must_use] - pub fn display_order(mut self, ord: impl IntoResettable) -> Self { + pub fn display_order(&mut self, ord: impl IntoResettable) -> &mut Self { self.disp_ord = ord.into_resettable().into_option(); self } @@ -2244,8 +2216,7 @@ impl Arg { /// /// [current]: crate::Command::next_help_heading #[inline] - #[must_use] - pub fn help_heading(mut self, heading: impl IntoResettable) -> Self { + pub fn help_heading(&mut self, heading: impl IntoResettable) -> &mut Self { self.help_heading = Some(heading.into_resettable().into_option()); self } @@ -2296,8 +2267,7 @@ impl Arg { /// on a line after the option /// ``` #[inline] - #[must_use] - pub fn next_line_help(self, yes: bool) -> Self { + pub fn next_line_help(&mut self, yes: bool) -> &mut Self { if yes { self.setting(ArgSettings::NextLineHelp) } else { @@ -2340,8 +2310,7 @@ impl Arg { /// -V, --version Print version information /// ``` #[inline] - #[must_use] - pub fn hide(self, yes: bool) -> Self { + pub fn hide(&mut self, yes: bool) -> &mut Self { if yes { self.setting(ArgSettings::Hidden) } else { @@ -2374,8 +2343,7 @@ impl Arg { /// If we were to run the above program with `--help` the `[values: fast, slow]` portion of /// the help text would be omitted. #[inline] - #[must_use] - pub fn hide_possible_values(self, yes: bool) -> Self { + pub fn hide_possible_values(&mut self, yes: bool) -> &mut Self { if yes { self.setting(ArgSettings::HidePossibleValues) } else { @@ -2406,8 +2374,7 @@ impl Arg { /// If we were to run the above program with `--help` the `[default: localhost]` portion of /// the help text would be omitted. #[inline] - #[must_use] - pub fn hide_default_value(self, yes: bool) -> Self { + pub fn hide_default_value(&mut self, yes: bool) -> &mut Self { if yes { self.setting(ArgSettings::HideDefaultValue) } else { @@ -2436,8 +2403,7 @@ impl Arg { /// text would be omitted. #[cfg(feature = "env")] #[inline] - #[must_use] - pub fn hide_env(self, yes: bool) -> Self { + pub fn hide_env(&mut self, yes: bool) -> &mut Self { if yes { self.setting(ArgSettings::HideEnv) } else { @@ -2467,8 +2433,7 @@ impl Arg { /// `[default: CONNECT=super_secret]` portion of the help text would be omitted. #[cfg(feature = "env")] #[inline] - #[must_use] - pub fn hide_env_values(self, yes: bool) -> Self { + pub fn hide_env_values(&mut self, yes: bool) -> &mut Self { if yes { self.setting(ArgSettings::HideEnvValues) } else { @@ -2551,8 +2516,7 @@ impl Arg { /// -V, --version Print version information /// ``` #[inline] - #[must_use] - pub fn hide_short_help(self, yes: bool) -> Self { + pub fn hide_short_help(&mut self, yes: bool) -> &mut Self { if yes { self.setting(ArgSettings::HiddenShortHelp) } else { @@ -2628,8 +2592,7 @@ impl Arg { /// -V, --version Print version information /// ``` #[inline] - #[must_use] - pub fn hide_long_help(self, yes: bool) -> Self { + pub fn hide_long_help(&mut self, yes: bool) -> &mut Self { if yes { self.setting(ArgSettings::HiddenLongHelp) } else { @@ -2676,8 +2639,7 @@ impl Arg { /// ``` /// /// [`ArgGroup`]: crate::ArgGroup - #[must_use] - pub fn group(mut self, group_id: impl IntoResettable) -> Self { + pub fn group(&mut self, group_id: impl IntoResettable) -> &mut Self { if let Some(group_id) = group_id.into_resettable().into_option() { self.groups.push(group_id); } else { @@ -2723,8 +2685,7 @@ impl Arg { /// ``` /// /// [`ArgGroup`]: crate::ArgGroup - #[must_use] - pub fn groups(mut self, group_ids: impl IntoIterator>) -> Self { + pub fn groups(&mut self, group_ids: impl IntoIterator>) -> &mut Self { self.groups.extend(group_ids.into_iter().map(Into::into)); self } @@ -2844,13 +2805,12 @@ impl Arg { /// ``` /// [`Arg::action(ArgAction::Set)`]: Arg::action() /// [`Arg::default_value`]: Arg::default_value() - #[must_use] pub fn default_value_if( - mut self, + &mut self, arg_id: impl Into, predicate: impl Into, default: impl IntoResettable, - ) -> Self { + ) -> &mut Self { self.default_vals_ifs.push(( arg_id.into(), predicate.into(), @@ -2859,18 +2819,17 @@ impl Arg { self } - #[must_use] #[doc(hidden)] #[cfg_attr( feature = "deprecated", deprecated(since = "4.0.0", note = "Replaced with `Arg::default_value_if`") )] pub fn default_value_if_os( - self, + &mut self, arg_id: impl Into, predicate: impl Into, default: impl IntoResettable, - ) -> Self { + ) -> &mut Self { self.default_value_if(arg_id, predicate, default) } @@ -2960,9 +2919,8 @@ impl Arg { /// ``` /// [`Arg::action(ArgAction::Set)`]: Arg::action() /// [`Arg::default_value_if`]: Arg::default_value_if() - #[must_use] pub fn default_value_ifs( - mut self, + &mut self, ifs: impl IntoIterator< Item = ( impl Into, @@ -2970,21 +2928,20 @@ impl Arg { impl IntoResettable, ), >, - ) -> Self { + ) -> &mut Self { for (arg, predicate, default) in ifs { - self = self.default_value_if(arg, predicate, default); + self.default_value_if(arg, predicate, default); } self } - #[must_use] #[doc(hidden)] #[cfg_attr( feature = "deprecated", deprecated(since = "4.0.0", note = "Replaced with `Arg::default_value_ifs`") )] pub fn default_value_ifs_os( - self, + &mut self, ifs: impl IntoIterator< Item = ( impl Into, @@ -2992,7 +2949,7 @@ impl Arg { impl IntoResettable, ), >, - ) -> Self { + ) -> &mut Self { self.default_value_ifs(ifs) } @@ -3052,8 +3009,7 @@ impl Arg { /// assert_eq!(res.unwrap_err().kind(), ErrorKind::MissingRequiredArgument); /// ``` /// [required]: Arg::required() - #[must_use] - pub fn required_unless_present(mut self, arg_id: impl IntoResettable) -> Self { + pub fn required_unless_present(&mut self, arg_id: impl IntoResettable) -> &mut Self { if let Some(arg_id) = arg_id.into_resettable().into_option() { self.r_unless.push(arg_id); } else { @@ -3132,11 +3088,10 @@ impl Arg { /// [required]: Arg::required() /// [`Arg::required_unless_present_any`]: Arg::required_unless_present_any() /// [`Arg::required_unless_present_all(names)`]: Arg::required_unless_present_all() - #[must_use] pub fn required_unless_present_all( - mut self, + &mut self, names: impl IntoIterator>, - ) -> Self { + ) -> &mut Self { self.r_unless_all.extend(names.into_iter().map(Into::into)); self } @@ -3213,11 +3168,10 @@ impl Arg { /// [required]: Arg::required() /// [`Arg::required_unless_present_any(names)`]: Arg::required_unless_present_any() /// [`Arg::required_unless_present_all`]: Arg::required_unless_present_all() - #[must_use] pub fn required_unless_present_any( - mut self, + &mut self, names: impl IntoIterator>, - ) -> Self { + ) -> &mut Self { self.r_unless.extend(names.into_iter().map(Into::into)); self } @@ -3303,8 +3257,7 @@ impl Arg { /// [`Arg::requires(name)`]: Arg::requires() /// [Conflicting]: Arg::conflicts_with() /// [required]: Arg::required() - #[must_use] - pub fn required_if_eq(mut self, arg_id: impl Into, val: impl Into) -> Self { + pub fn required_if_eq(&mut self, arg_id: impl Into, val: impl Into) -> &mut Self { self.r_ifs.push((arg_id.into(), val.into())); self } @@ -3385,11 +3338,10 @@ impl Arg { /// [`Arg::requires(name)`]: Arg::requires() /// [Conflicting]: Arg::conflicts_with() /// [required]: Arg::required() - #[must_use] pub fn required_if_eq_any( - mut self, + &mut self, ifs: impl IntoIterator, impl Into)>, - ) -> Self { + ) -> &mut Self { self.r_ifs .extend(ifs.into_iter().map(|(id, val)| (id.into(), val.into()))); self @@ -3469,11 +3421,10 @@ impl Arg { /// assert_eq!(res.unwrap_err().kind(), ErrorKind::MissingRequiredArgument); /// ``` /// [required]: Arg::required() - #[must_use] pub fn required_if_eq_all( - mut self, + &mut self, ifs: impl IntoIterator, impl Into)>, - ) -> Self { + ) -> &mut Self { self.r_ifs_all .extend(ifs.into_iter().map(|(id, val)| (id.into(), val.into()))); self @@ -3537,8 +3488,11 @@ impl Arg { /// [`Arg::requires(name)`]: Arg::requires() /// [Conflicting]: Arg::conflicts_with() /// [override]: Arg::overrides_with() - #[must_use] - pub fn requires_if(mut self, val: impl Into, arg_id: impl Into) -> Self { + pub fn requires_if( + &mut self, + val: impl Into, + arg_id: impl Into, + ) -> &mut Self { self.requires.push((val.into(), arg_id.into())); self } @@ -3616,11 +3570,10 @@ impl Arg { /// [`Arg::requires(name)`]: Arg::requires() /// [Conflicting]: Arg::conflicts_with() /// [override]: Arg::overrides_with() - #[must_use] pub fn requires_ifs( - mut self, + &mut self, ifs: impl IntoIterator, impl Into)>, - ) -> Self { + ) -> &mut Self { self.requires .extend(ifs.into_iter().map(|(val, arg)| (val.into(), arg.into()))); self @@ -3631,7 +3584,7 @@ impl Arg { feature = "deprecated", deprecated(since = "4.0.0", note = "Replaced with `Arg::requires_ifs`") )] - pub fn requires_all(self, ids: impl IntoIterator>) -> Self { + pub fn requires_all(&mut self, ids: impl IntoIterator>) -> &mut Self { self.requires_ifs(ids.into_iter().map(|id| (ArgPredicate::IsPresent, id))) } @@ -3683,8 +3636,7 @@ impl Arg { /// /// [`Arg::conflicts_with_all(names)`]: Arg::conflicts_with_all() /// [`Arg::exclusive(true)`]: Arg::exclusive() - #[must_use] - pub fn conflicts_with(mut self, arg_id: impl IntoResettable) -> Self { + pub fn conflicts_with(&mut self, arg_id: impl IntoResettable) -> &mut Self { if let Some(arg_id) = arg_id.into_resettable().into_option() { self.blacklist.push(arg_id); } else { @@ -3739,8 +3691,10 @@ impl Arg { /// ``` /// [`Arg::conflicts_with`]: Arg::conflicts_with() /// [`Arg::exclusive(true)`]: Arg::exclusive() - #[must_use] - pub fn conflicts_with_all(mut self, names: impl IntoIterator>) -> Self { + pub fn conflicts_with_all( + &mut self, + names: impl IntoIterator>, + ) -> &mut Self { self.blacklist.extend(names.into_iter().map(Into::into)); self } @@ -3776,8 +3730,7 @@ impl Arg { /// // was never used because it was overridden with color /// assert!(!m.get_flag("flag")); /// ``` - #[must_use] - pub fn overrides_with(mut self, arg_id: impl IntoResettable) -> Self { + pub fn overrides_with(&mut self, arg_id: impl IntoResettable) -> &mut Self { if let Some(arg_id) = arg_id.into_resettable().into_option() { self.overrides.push(arg_id); } else { @@ -3817,8 +3770,10 @@ impl Arg { /// assert!(!m.get_flag("debug")); /// assert!(!m.get_flag("flag")); /// ``` - #[must_use] - pub fn overrides_with_all(mut self, names: impl IntoIterator>) -> Self { + pub fn overrides_with_all( + &mut self, + names: impl IntoIterator>, + ) -> &mut Self { self.overrides.extend(names.into_iter().map(Into::into)); self } @@ -4403,6 +4358,12 @@ impl Arg { } } +impl From<&'_ mut Arg> for Arg { + fn from(a: &mut Arg) -> Self { + a.clone() + } +} + impl From<&'_ Arg> for Arg { fn from(a: &Arg) -> Self { a.clone() diff --git a/clap_builder/src/builder/arg_group.rs b/clap_builder/src/builder/arg_group.rs index d3a37c1bfb3..642f6afc359 100644 --- a/clap_builder/src/builder/arg_group.rs +++ b/clap_builder/src/builder/arg_group.rs @@ -110,7 +110,9 @@ impl ArgGroup { /// # ; /// ``` pub fn new(id: impl Into) -> Self { - ArgGroup::default().id(id) + let mut group = ArgGroup::default(); + group.id(id); + group } /// Sets the group name. @@ -123,8 +125,7 @@ impl ArgGroup { /// ArgGroup::default().id("config") /// # ; /// ``` - #[must_use] - pub fn id(mut self, id: impl Into) -> Self { + pub fn id(&mut self, id: impl Into) -> &mut Self { self.id = id.into(); self } @@ -153,8 +154,7 @@ impl ArgGroup { /// assert!(m.contains_id("flag")); /// ``` /// [argument]: crate::Arg - #[must_use] - pub fn arg(mut self, arg_id: impl IntoResettable) -> Self { + pub fn arg(&mut self, arg_id: impl IntoResettable) -> &mut Self { if let Some(arg_id) = arg_id.into_resettable().into_option() { self.args.push(arg_id); } else { @@ -186,10 +186,9 @@ impl ArgGroup { /// assert!(m.contains_id("flag")); /// ``` /// [arguments]: crate::Arg - #[must_use] - pub fn args(mut self, ns: impl IntoIterator>) -> Self { + pub fn args(&mut self, ns: impl IntoIterator>) -> &mut Self { for n in ns { - self = self.arg(n); + self.arg(n); } self } @@ -260,8 +259,7 @@ impl ArgGroup { /// /// [`Arg`]: crate::Arg #[inline] - #[must_use] - pub fn multiple(mut self, yes: bool) -> Self { + pub fn multiple(&mut self, yes: bool) -> &mut Self { self.multiple = yes; self } @@ -323,8 +321,7 @@ impl ArgGroup { /// [`ArgGroup::multiple`]: ArgGroup::multiple() /// [`Command`]: crate::Command #[inline] - #[must_use] - pub fn required(mut self, yes: bool) -> Self { + pub fn required(&mut self, yes: bool) -> &mut Self { self.required = yes; self } @@ -364,8 +361,7 @@ impl ArgGroup { /// ``` /// [required group]: ArgGroup::required() /// [argument requirement rules]: crate::Arg::requires() - #[must_use] - pub fn requires(mut self, id: impl IntoResettable) -> Self { + pub fn requires(&mut self, id: impl IntoResettable) -> &mut Self { if let Some(id) = id.into_resettable().into_option() { self.requires.push(id); } else { @@ -412,10 +408,9 @@ impl ArgGroup { /// ``` /// [required group]: ArgGroup::required() /// [argument requirement rules]: crate::Arg::requires_ifs() - #[must_use] - pub fn requires_all(mut self, ns: impl IntoIterator>) -> Self { + pub fn requires_all(&mut self, ns: impl IntoIterator>) -> &mut Self { for n in ns { - self = self.requires(n); + self.requires(n); } self } @@ -453,8 +448,7 @@ impl ArgGroup { /// assert_eq!(err.kind(), ErrorKind::ArgumentConflict); /// ``` /// [argument exclusion rules]: crate::Arg::conflicts_with() - #[must_use] - pub fn conflicts_with(mut self, id: impl IntoResettable) -> Self { + pub fn conflicts_with(&mut self, id: impl IntoResettable) -> &mut Self { if let Some(id) = id.into_resettable().into_option() { self.conflicts.push(id); } else { @@ -500,10 +494,9 @@ impl ArgGroup { /// ``` /// /// [argument exclusion rules]: crate::Arg::conflicts_with_all() - #[must_use] - pub fn conflicts_with_all(mut self, ns: impl IntoIterator>) -> Self { + pub fn conflicts_with_all(&mut self, ns: impl IntoIterator>) -> &mut Self { for n in ns { - self = self.conflicts_with(n); + self.conflicts_with(n); } self } @@ -524,6 +517,12 @@ impl ArgGroup { } } +impl From<&'_ mut ArgGroup> for ArgGroup { + fn from(g: &mut ArgGroup) -> Self { + g.clone() + } +} + impl From<&'_ ArgGroup> for ArgGroup { fn from(g: &ArgGroup) -> Self { g.clone() diff --git a/clap_builder/src/builder/command.rs b/clap_builder/src/builder/command.rs index b1623cf8c51..03f1c3714ab 100644 --- a/clap_builder/src/builder/command.rs +++ b/clap_builder/src/builder/command.rs @@ -164,8 +164,7 @@ impl Command { /// # ; /// ``` /// [argument]: Arg - #[must_use] - pub fn arg(mut self, a: impl Into) -> Self { + pub fn arg(&mut self, a: impl Into) -> &mut Self { let arg = a.into(); self.arg_internal(arg); self @@ -200,10 +199,9 @@ impl Command { /// # ; /// ``` /// [arguments]: Arg - #[must_use] - pub fn args(mut self, args: impl IntoIterator>) -> Self { + pub fn args(&mut self, args: impl IntoIterator>) -> &mut Self { for arg in args { - self = self.arg(arg); + self.arg(arg); } self } @@ -236,19 +234,19 @@ impl Command { /// let res = cmd.try_get_matches_from_mut(vec!["foo", "-B"]); /// assert!(res.is_ok()); /// ``` - #[must_use] #[cfg_attr(debug_assertions, track_caller)] - pub fn mut_arg(mut self, arg_id: impl AsRef, f: F) -> Self + pub fn mut_arg(&mut self, arg_id: impl AsRef, f: F) -> &mut Self where - F: FnOnce(Arg) -> Arg, + F: FnOnce(&mut Arg) -> &mut Arg, { let id = arg_id.as_ref(); let a = self .args - .remove_by_name(id) + .args_mut() + .find(|a| a.get_id() == id) .unwrap_or_else(|| panic!("Argument `{id}` is undefined")); - self.args.push(f(a)); + f(a); self } @@ -288,9 +286,8 @@ impl Command { /// let res = cmd.try_get_matches_from_mut(vec!["foo", "--prefix-bar"]); /// assert!(res.is_ok()); /// ``` - #[must_use] #[cfg_attr(debug_assertions, track_caller)] - pub fn mut_args(mut self, f: F) -> Self + pub fn mut_args(&mut self, f: F) -> &mut Self where F: FnMut(Arg) -> Arg, { @@ -326,21 +323,16 @@ impl Command { /// let res = cmd.try_get_matches_from_mut(vec!["foo", "bar"]); /// assert!(res.is_ok()); /// ``` - #[must_use] - pub fn mut_subcommand(mut self, name: impl AsRef, f: F) -> Self + pub fn mut_subcommand(&mut self, name: impl AsRef, f: F) -> &mut Self where - F: FnOnce(Self) -> Self, + F: FnOnce(&mut Self) -> &mut Self, { let name = name.as_ref(); - let pos = self.subcommands.iter().position(|s| s.name == name); - - let subcmd = if let Some(idx) = pos { - self.subcommands.remove(idx) + if let Some(subcmd) = self.subcommands.iter_mut().find(|s| s.name == name) { + f(subcmd); } else { panic!("Command `{name}` is undefined") - }; - - self.subcommands.push(f(subcmd)); + } self } @@ -377,8 +369,7 @@ impl Command { /// # ; /// ``` #[inline] - #[must_use] - pub fn group(mut self, group: impl Into) -> Self { + pub fn group(&mut self, group: impl Into) -> &mut Self { self.groups.push(group.into()); self } @@ -406,10 +397,9 @@ impl Command { /// ]) /// # ; /// ``` - #[must_use] - pub fn groups(mut self, groups: impl IntoIterator>) -> Self { + pub fn groups(&mut self, groups: impl IntoIterator>) -> &mut Self { for g in groups.into_iter() { - self = self.group(g.into()); + self.group(g.into()); } self } @@ -436,13 +426,12 @@ impl Command { /// # ; /// ``` #[inline] - #[must_use] - pub fn subcommand(self, subcmd: impl Into) -> Self { + pub fn subcommand(&mut self, subcmd: impl Into) -> &mut Self { let subcmd = subcmd.into(); self.subcommand_internal(subcmd) } - fn subcommand_internal(mut self, mut subcmd: Self) -> Self { + fn subcommand_internal(&mut self, mut subcmd: Self) -> &mut Self { if let Some(current_disp_ord) = self.current_disp_ord.as_mut() { let current = *current_disp_ord; subcmd.disp_ord.get_or_insert(current); @@ -467,10 +456,9 @@ impl Command { /// # ; /// ``` /// [`IntoIterator`]: std::iter::IntoIterator - #[must_use] - pub fn subcommands(mut self, subcmds: impl IntoIterator>) -> Self { + pub fn subcommands(&mut self, subcmds: impl IntoIterator>) -> &mut Self { for subcmd in subcmds { - self = self.subcommand(subcmd); + self.subcommand(subcmd); } self } @@ -494,7 +482,7 @@ impl Command { /// ) /// # ; /// ``` - pub fn defer(mut self, deferred: fn(Command) -> Command) -> Self { + pub fn defer(&mut self, deferred: fn(Command) -> Command) -> &mut Self { self.deferred = Some(deferred); self } @@ -1027,7 +1015,7 @@ impl Command { /// ``` /// [`try_get_matches_from_mut`]: crate::Command::try_get_matches_from_mut() #[inline] - pub fn no_binary_name(self, yes: bool) -> Self { + pub fn no_binary_name(&mut self, yes: bool) -> &mut Self { if yes { self.global_setting(AppSettings::NoBinaryName) } else { @@ -1059,7 +1047,7 @@ impl Command { /// assert_eq!(m.get_one::("stuff"), None); /// ``` #[inline] - pub fn ignore_errors(self, yes: bool) -> Self { + pub fn ignore_errors(&mut self, yes: bool) -> &mut Self { if yes { self.global_setting(AppSettings::IgnoreErrors) } else { @@ -1079,7 +1067,7 @@ impl Command { /// /// [`Arg::overrides_with("foo")`]: crate::Arg::overrides_with() #[inline] - pub fn args_override_self(self, yes: bool) -> Self { + pub fn args_override_self(&mut self, yes: bool) -> &mut Self { if yes { self.global_setting(AppSettings::AllArgsOverrideSelf) } else { @@ -1108,7 +1096,7 @@ impl Command { /// /// [`Arg::value_delimiter(None)`]: crate::Arg::value_delimiter() #[inline] - pub fn dont_delimit_trailing_values(self, yes: bool) -> Self { + pub fn dont_delimit_trailing_values(&mut self, yes: bool) -> &mut Self { if yes { self.global_setting(AppSettings::DontDelimitTrailingValues) } else { @@ -1134,8 +1122,7 @@ impl Command { /// [`ColorChoice::Auto`]: crate::ColorChoice::Auto #[cfg(feature = "color")] #[inline] - #[must_use] - pub fn color(self, color: ColorChoice) -> Self { + pub fn color(&mut self, color: ColorChoice) -> &mut Self { let cmd = self .unset_global_setting(AppSettings::ColorAuto) .unset_global_setting(AppSettings::ColorAlways) @@ -1169,8 +1156,7 @@ impl Command { /// ``` #[cfg(feature = "color")] #[inline] - #[must_use] - pub fn styles(mut self, styles: Styles) -> Self { + pub fn styles(&mut self, styles: Styles) -> &mut Self { self.app_ext.set(styles); self } @@ -1199,9 +1185,8 @@ impl Command { /// # ; /// ``` #[inline] - #[must_use] #[cfg(any(not(feature = "unstable-v5"), feature = "wrap_help"))] - pub fn term_width(mut self, width: usize) -> Self { + pub fn term_width(&mut self, width: usize) -> &mut Self { self.app_ext.set(TermWidth(width)); self } @@ -1229,9 +1214,8 @@ impl Command { /// # ; /// ``` #[inline] - #[must_use] #[cfg(any(not(feature = "unstable-v5"), feature = "wrap_help"))] - pub fn max_term_width(mut self, width: usize) -> Self { + pub fn max_term_width(&mut self, width: usize) -> &mut Self { self.app_ext.set(MaxTermWidth(width)); self } @@ -1252,7 +1236,7 @@ impl Command { /// assert_eq!(res.unwrap_err().kind(), ErrorKind::UnknownArgument); /// ``` #[inline] - pub fn disable_version_flag(self, yes: bool) -> Self { + pub fn disable_version_flag(&mut self, yes: bool) -> &mut Self { if yes { self.global_setting(AppSettings::DisableVersionFlag) } else { @@ -1282,7 +1266,7 @@ impl Command { /// /// [`subcommands`]: crate::Command::subcommand() #[inline] - pub fn propagate_version(self, yes: bool) -> Self { + pub fn propagate_version(&mut self, yes: bool) -> &mut Self { if yes { self.global_setting(AppSettings::PropagateVersion) } else { @@ -1304,7 +1288,7 @@ impl Command { /// .get_matches(); /// ``` #[inline] - pub fn next_line_help(self, yes: bool) -> Self { + pub fn next_line_help(&mut self, yes: bool) -> &mut Self { if yes { self.global_setting(AppSettings::NextLineHelp) } else { @@ -1330,7 +1314,7 @@ impl Command { /// assert_eq!(res.unwrap_err().kind(), ErrorKind::UnknownArgument); /// ``` #[inline] - pub fn disable_help_flag(self, yes: bool) -> Self { + pub fn disable_help_flag(&mut self, yes: bool) -> &mut Self { if yes { self.global_setting(AppSettings::DisableHelpFlag) } else { @@ -1361,7 +1345,7 @@ impl Command { /// /// [`subcommand`]: crate::Command::subcommand() #[inline] - pub fn disable_help_subcommand(self, yes: bool) -> Self { + pub fn disable_help_subcommand(&mut self, yes: bool) -> &mut Self { if yes { self.global_setting(AppSettings::DisableHelpSubcommand) } else { @@ -1383,7 +1367,7 @@ impl Command { /// .get_matches(); /// ``` #[inline] - pub fn disable_colored_help(self, yes: bool) -> Self { + pub fn disable_colored_help(&mut self, yes: bool) -> &mut Self { if yes { self.global_setting(AppSettings::DisableColoredHelp) } else { @@ -1429,7 +1413,7 @@ impl Command { /// # .get_matches(); ///``` #[inline] - pub fn help_expected(self, yes: bool) -> Self { + pub fn help_expected(&mut self, yes: bool) -> &mut Self { if yes { self.global_setting(AppSettings::HelpExpected) } else { @@ -1442,7 +1426,7 @@ impl Command { feature = "deprecated", deprecated(since = "4.0.0", note = "This is now the default") )] - pub fn dont_collapse_args_in_usage(self, _yes: bool) -> Self { + pub fn dont_collapse_args_in_usage(&mut self, _yes: bool) -> &mut Self { self } @@ -1455,7 +1439,7 @@ impl Command { /// /// **NOTE:** This choice is propagated to all child subcommands. #[inline] - pub fn hide_possible_values(self, yes: bool) -> Self { + pub fn hide_possible_values(&mut self, yes: bool) -> &mut Self { if yes { self.global_setting(AppSettings::HidePossibleValues) } else { @@ -1476,7 +1460,7 @@ impl Command { /// /// [aliases]: crate::Command::aliases() #[inline] - pub fn infer_long_args(self, yes: bool) -> Self { + pub fn infer_long_args(&mut self, yes: bool) -> &mut Self { if yes { self.global_setting(AppSettings::InferLongArgs) } else { @@ -1518,7 +1502,7 @@ impl Command { /// [positional/free arguments]: crate::Arg::index() /// [aliases]: crate::Command::aliases() #[inline] - pub fn infer_subcommands(self, yes: bool) -> Self { + pub fn infer_subcommands(&mut self, yes: bool) -> &mut Self { if yes { self.global_setting(AppSettings::InferSubcommands) } else { @@ -1543,8 +1527,7 @@ impl Command { /// /// // continued logic goes here, such as `cmd.get_matches()` etc. /// ``` - #[must_use] - pub fn name(mut self, name: impl Into) -> Self { + pub fn name(&mut self, name: impl Into) -> &mut Self { self.name = name.into(); self } @@ -1570,8 +1553,7 @@ impl Command { /// .bin_name("my_binary") /// # ; /// ``` - #[must_use] - pub fn bin_name(mut self, name: impl IntoResettable) -> Self { + pub fn bin_name(&mut self, name: impl IntoResettable) -> &mut Self { self.bin_name = name.into_resettable().into_option(); self } @@ -1587,8 +1569,7 @@ impl Command { /// .display_name("my_program") /// # ; /// ``` - #[must_use] - pub fn display_name(mut self, name: impl IntoResettable) -> Self { + pub fn display_name(&mut self, name: impl IntoResettable) -> &mut Self { self.display_name = name.into_resettable().into_option(); self } @@ -1611,8 +1592,7 @@ impl Command { /// .author("Me, me@mymain.com") /// # ; /// ``` - #[must_use] - pub fn author(mut self, author: impl IntoResettable) -> Self { + pub fn author(&mut self, author: impl IntoResettable) -> &mut Self { self.author = author.into_resettable().into_option(); self } @@ -1635,8 +1615,7 @@ impl Command { /// .about("Does really amazing things for great people") /// # ; /// ``` - #[must_use] - pub fn about(mut self, about: impl IntoResettable) -> Self { + pub fn about(&mut self, about: impl IntoResettable) -> &mut Self { self.about = about.into_resettable().into_option(); self } @@ -1661,8 +1640,7 @@ impl Command { /// # ; /// ``` /// [`Command::about`]: Command::about() - #[must_use] - pub fn long_about(mut self, long_about: impl IntoResettable) -> Self { + pub fn long_about(&mut self, long_about: impl IntoResettable) -> &mut Self { self.long_about = long_about.into_resettable().into_option(); self } @@ -1684,8 +1662,7 @@ impl Command { /// # ; /// ``` /// - #[must_use] - pub fn after_help(mut self, help: impl IntoResettable) -> Self { + pub fn after_help(&mut self, help: impl IntoResettable) -> &mut Self { self.after_help = help.into_resettable().into_option(); self } @@ -1707,8 +1684,7 @@ impl Command { /// like, for real, be careful with this!") /// # ; /// ``` - #[must_use] - pub fn after_long_help(mut self, help: impl IntoResettable) -> Self { + pub fn after_long_help(&mut self, help: impl IntoResettable) -> &mut Self { self.after_long_help = help.into_resettable().into_option(); self } @@ -1728,8 +1704,7 @@ impl Command { /// .before_help("Some info I'd like to appear before the help info") /// # ; /// ``` - #[must_use] - pub fn before_help(mut self, help: impl IntoResettable) -> Self { + pub fn before_help(&mut self, help: impl IntoResettable) -> &mut Self { self.before_help = help.into_resettable().into_option(); self } @@ -1749,8 +1724,7 @@ impl Command { /// .before_long_help("Some verbose and long info I'd like to appear before the help info") /// # ; /// ``` - #[must_use] - pub fn before_long_help(mut self, help: impl IntoResettable) -> Self { + pub fn before_long_help(&mut self, help: impl IntoResettable) -> &mut Self { self.before_long_help = help.into_resettable().into_option(); self } @@ -1772,8 +1746,7 @@ impl Command { /// .version("v0.1.24") /// # ; /// ``` - #[must_use] - pub fn version(mut self, ver: impl IntoResettable) -> Self { + pub fn version(&mut self, ver: impl IntoResettable) -> &mut Self { self.version = ver.into_resettable().into_option(); self } @@ -1800,8 +1773,7 @@ impl Command { /// binary: myprog") /// # ; /// ``` - #[must_use] - pub fn long_version(mut self, ver: impl IntoResettable) -> Self { + pub fn long_version(&mut self, ver: impl IntoResettable) -> &mut Self { self.long_version = ver.into_resettable().into_option(); self } @@ -1845,8 +1817,7 @@ impl Command { /// ``` /// /// [`ArgMatches::usage`]: ArgMatches::usage() - #[must_use] - pub fn override_usage(mut self, usage: impl IntoResettable) -> Self { + pub fn override_usage(&mut self, usage: impl IntoResettable) -> &mut Self { self.usage_str = usage.into_resettable().into_option(); self } @@ -1883,8 +1854,7 @@ impl Command { /// work Do some work") /// # ; /// ``` - #[must_use] - pub fn override_help(mut self, help: impl IntoResettable) -> Self { + pub fn override_help(&mut self, help: impl IntoResettable) -> &mut Self { self.help_str = help.into_resettable().into_option(); self } @@ -1954,38 +1924,33 @@ impl Command { /// [`Command::after_long_help`]: Command::after_long_help() /// [`Command::before_help`]: Command::before_help() /// [`Command::before_long_help`]: Command::before_long_help() - #[must_use] #[cfg(feature = "help")] - pub fn help_template(mut self, s: impl IntoResettable) -> Self { + pub fn help_template(&mut self, s: impl IntoResettable) -> &mut Self { self.template = s.into_resettable().into_option(); self } #[inline] - #[must_use] - pub(crate) fn setting(mut self, setting: AppSettings) -> Self { + pub(crate) fn setting(&mut self, setting: AppSettings) -> &mut Self { self.settings.set(setting); self } #[inline] - #[must_use] - pub(crate) fn unset_setting(mut self, setting: AppSettings) -> Self { + pub(crate) fn unset_setting(&mut self, setting: AppSettings) -> &mut Self { self.settings.unset(setting); self } #[inline] - #[must_use] - pub(crate) fn global_setting(mut self, setting: AppSettings) -> Self { + pub(crate) fn global_setting(&mut self, setting: AppSettings) -> &mut Self { self.settings.set(setting); self.g_settings.set(setting); self } #[inline] - #[must_use] - pub(crate) fn unset_global_setting(mut self, setting: AppSettings) -> Self { + pub(crate) fn unset_global_setting(&mut self, setting: AppSettings) -> &mut Self { self.settings.unset(setting); self.g_settings.unset(setting); self @@ -2003,8 +1968,7 @@ impl Command { /// [`Command::arg`]: Command::arg() /// [`Arg::help_heading`]: crate::Arg::help_heading() #[inline] - #[must_use] - pub fn next_help_heading(mut self, heading: impl IntoResettable) -> Self { + pub fn next_help_heading(&mut self, heading: impl IntoResettable) -> &mut Self { self.current_help_heading = heading.into_resettable().into_option(); self } @@ -2013,8 +1977,7 @@ impl Command { /// /// This will be used for any arg that hasn't had [`Arg::display_order`] called. #[inline] - #[must_use] - pub fn next_display_order(mut self, disp_ord: impl IntoResettable) -> Self { + pub fn next_display_order(&mut self, disp_ord: impl IntoResettable) -> &mut Self { self.current_disp_ord = disp_ord.into_resettable().into_option(); self } @@ -2035,7 +1998,7 @@ impl Command { /// [`subcommands`]: crate::Command::subcommand() /// [`Arg::default_value`]: crate::Arg::default_value() #[inline] - pub fn arg_required_else_help(self, yes: bool) -> Self { + pub fn arg_required_else_help(&mut self, yes: bool) -> &mut Self { if yes { self.setting(AppSettings::ArgRequiredElseHelp) } else { @@ -2048,7 +2011,7 @@ impl Command { feature = "deprecated", deprecated(since = "4.0.0", note = "Replaced with `Arg::allow_hyphen_values`") )] - pub fn allow_hyphen_values(self, yes: bool) -> Self { + pub fn allow_hyphen_values(&mut self, yes: bool) -> &mut Self { if yes { self.setting(AppSettings::AllowHyphenValues) } else { @@ -2061,7 +2024,7 @@ impl Command { feature = "deprecated", deprecated(since = "4.0.0", note = "Replaced with `Arg::allow_negative_numbers`") )] - pub fn allow_negative_numbers(self, yes: bool) -> Self { + pub fn allow_negative_numbers(&mut self, yes: bool) -> &mut Self { if yes { self.setting(AppSettings::AllowNegativeNumbers) } else { @@ -2074,7 +2037,7 @@ impl Command { feature = "deprecated", deprecated(since = "4.0.0", note = "Replaced with `Arg::trailing_var_arg`") )] - pub fn trailing_var_arg(self, yes: bool) -> Self { + pub fn trailing_var_arg(&mut self, yes: bool) -> &mut Self { if yes { self.setting(AppSettings::TrailingVarArg) } else { @@ -2194,7 +2157,7 @@ impl Command { /// /// [required]: crate::Arg::required() #[inline] - pub fn allow_missing_positional(self, yes: bool) -> Self { + pub fn allow_missing_positional(&mut self, yes: bool) -> &mut Self { if yes { self.setting(AppSettings::AllowMissingPositional) } else { @@ -2231,8 +2194,7 @@ impl Command { /// assert!(sync_matches.get_flag("search")); /// ``` /// [`Arg::short`]: Arg::short() - #[must_use] - pub fn short_flag(mut self, short: impl IntoResettable) -> Self { + pub fn short_flag(&mut self, short: impl IntoResettable) -> &mut Self { self.short_flag = short.into_resettable().into_option(); self } @@ -2270,8 +2232,7 @@ impl Command { /// ``` /// /// [`Arg::long`]: Arg::long() - #[must_use] - pub fn long_flag(mut self, long: impl Into) -> Self { + pub fn long_flag(&mut self, long: impl Into) -> &mut Self { self.long_flag = Some(long.into()); self } @@ -2302,8 +2263,7 @@ impl Command { /// assert_eq!(m.subcommand_name(), Some("test")); /// ``` /// [`Command::visible_alias`]: Command::visible_alias() - #[must_use] - pub fn alias(mut self, name: impl IntoResettable) -> Self { + pub fn alias(&mut self, name: impl IntoResettable) -> &mut Self { if let Some(name) = name.into_resettable().into_option() { self.aliases.push((name, false)); } else { @@ -2329,8 +2289,7 @@ impl Command { /// .get_matches_from(vec!["myprog", "-d"]); /// assert_eq!(m.subcommand_name(), Some("test")); /// ``` - #[must_use] - pub fn short_flag_alias(mut self, name: impl IntoResettable) -> Self { + pub fn short_flag_alias(&mut self, name: impl IntoResettable) -> &mut Self { if let Some(name) = name.into_resettable().into_option() { debug_assert!(name != '-', "short alias name cannot be `-`"); self.short_flag_aliases.push((name, false)); @@ -2357,8 +2316,7 @@ impl Command { /// .get_matches_from(vec!["myprog", "--testing"]); /// assert_eq!(m.subcommand_name(), Some("test")); /// ``` - #[must_use] - pub fn long_flag_alias(mut self, name: impl IntoResettable) -> Self { + pub fn long_flag_alias(&mut self, name: impl IntoResettable) -> &mut Self { if let Some(name) = name.into_resettable().into_option() { self.long_flag_aliases.push((name, false)); } else { @@ -2396,8 +2354,7 @@ impl Command { /// assert_eq!(m.subcommand_name(), Some("test")); /// ``` /// [`Command::visible_aliases`]: Command::visible_aliases() - #[must_use] - pub fn aliases(mut self, names: impl IntoIterator>) -> Self { + pub fn aliases(&mut self, names: impl IntoIterator>) -> &mut Self { self.aliases .extend(names.into_iter().map(|n| (n.into(), false))); self @@ -2423,8 +2380,7 @@ impl Command { /// .get_matches_from(vec!["myprog", "-a"]); /// assert_eq!(m.subcommand_name(), Some("test")); /// ``` - #[must_use] - pub fn short_flag_aliases(mut self, names: impl IntoIterator) -> Self { + pub fn short_flag_aliases(&mut self, names: impl IntoIterator) -> &mut Self { for s in names { debug_assert!(s != '-', "short alias name cannot be `-`"); self.short_flag_aliases.push((s, false)); @@ -2452,10 +2408,12 @@ impl Command { /// .get_matches_from(vec!["myprog", "--testing"]); /// assert_eq!(m.subcommand_name(), Some("test")); /// ``` - #[must_use] - pub fn long_flag_aliases(mut self, names: impl IntoIterator>) -> Self { + pub fn long_flag_aliases( + &mut self, + names: impl IntoIterator>, + ) -> &mut Self { for s in names { - self = self.long_flag_alias(s) + self.long_flag_alias(s); } self } @@ -2488,8 +2446,7 @@ impl Command { /// assert_eq!(m.subcommand_name(), Some("test")); /// ``` /// [`Command::alias`]: Command::alias() - #[must_use] - pub fn visible_alias(mut self, name: impl IntoResettable) -> Self { + pub fn visible_alias(&mut self, name: impl IntoResettable) -> &mut Self { if let Some(name) = name.into_resettable().into_option() { self.aliases.push((name, true)); } else { @@ -2518,8 +2475,7 @@ impl Command { /// assert_eq!(m.subcommand_name(), Some("test")); /// ``` /// [`Command::short_flag_alias`]: Command::short_flag_alias() - #[must_use] - pub fn visible_short_flag_alias(mut self, name: impl IntoResettable) -> Self { + pub fn visible_short_flag_alias(&mut self, name: impl IntoResettable) -> &mut Self { if let Some(name) = name.into_resettable().into_option() { debug_assert!(name != '-', "short alias name cannot be `-`"); self.short_flag_aliases.push((name, true)); @@ -2549,8 +2505,7 @@ impl Command { /// assert_eq!(m.subcommand_name(), Some("test")); /// ``` /// [`Command::long_flag_alias`]: Command::long_flag_alias() - #[must_use] - pub fn visible_long_flag_alias(mut self, name: impl IntoResettable) -> Self { + pub fn visible_long_flag_alias(&mut self, name: impl IntoResettable) -> &mut Self { if let Some(name) = name.into_resettable().into_option() { self.long_flag_aliases.push((name, true)); } else { @@ -2587,8 +2542,10 @@ impl Command { /// assert_eq!(m.subcommand_name(), Some("test")); /// ``` /// [`Command::alias`]: Command::alias() - #[must_use] - pub fn visible_aliases(mut self, names: impl IntoIterator>) -> Self { + pub fn visible_aliases( + &mut self, + names: impl IntoIterator>, + ) -> &mut Self { self.aliases .extend(names.into_iter().map(|n| (n.into(), true))); self @@ -2610,8 +2567,10 @@ impl Command { /// assert_eq!(m.subcommand_name(), Some("test")); /// ``` /// [`Command::short_flag_aliases`]: Command::short_flag_aliases() - #[must_use] - pub fn visible_short_flag_aliases(mut self, names: impl IntoIterator) -> Self { + pub fn visible_short_flag_aliases( + &mut self, + names: impl IntoIterator, + ) -> &mut Self { for s in names { debug_assert!(s != '-', "short alias name cannot be `-`"); self.short_flag_aliases.push((s, true)); @@ -2635,13 +2594,12 @@ impl Command { /// assert_eq!(m.subcommand_name(), Some("test")); /// ``` /// [`Command::long_flag_aliases`]: Command::long_flag_aliases() - #[must_use] pub fn visible_long_flag_aliases( - mut self, + &mut self, names: impl IntoIterator>, - ) -> Self { + ) -> &mut Self { for s in names { - self = self.visible_long_flag_alias(s); + self.visible_long_flag_alias(s); } self } @@ -2692,8 +2650,7 @@ impl Command { /// -V, --version Print version /// ``` #[inline] - #[must_use] - pub fn display_order(mut self, ord: impl IntoResettable) -> Self { + pub fn display_order(&mut self, ord: impl IntoResettable) -> &mut Self { self.disp_ord = ord.into_resettable().into_option(); self } @@ -2714,7 +2671,7 @@ impl Command { /// /// [`subcommand`]: crate::Command::subcommand() #[inline] - pub fn hide(self, yes: bool) -> Self { + pub fn hide(&mut self, yes: bool) -> &mut Self { if yes { self.setting(AppSettings::Hidden) } else { @@ -2741,7 +2698,7 @@ impl Command { /// ``` /// /// [`subcommand`]: crate::Command::subcommand() - pub fn subcommand_required(self, yes: bool) -> Self { + pub fn subcommand_required(&mut self, yes: bool) -> &mut Self { if yes { self.setting(AppSettings::SubcommandRequired) } else { @@ -2789,7 +2746,7 @@ impl Command { /// [`subcommand`]: crate::Command::subcommand() /// [`ArgMatches`]: crate::ArgMatches /// [`ErrorKind::UnknownArgument`]: crate::error::ErrorKind::UnknownArgument - pub fn allow_external_subcommands(self, yes: bool) -> Self { + pub fn allow_external_subcommands(&mut self, yes: bool) -> &mut Self { if yes { self.setting(AppSettings::AllowExternalSubcommands) } else { @@ -2857,9 +2814,9 @@ impl Command { /// /// [`subcommands`]: crate::Command::subcommand() pub fn external_subcommand_value_parser( - mut self, + &mut self, parser: impl IntoResettable, - ) -> Self { + ) -> &mut Self { self.external_value_parser = parser.into_resettable().into_option(); self } @@ -2887,7 +2844,7 @@ impl Command { /// ``` /// /// [`subcommands`]: crate::Command::subcommand() - pub fn args_conflicts_with_subcommands(self, yes: bool) -> Self { + pub fn args_conflicts_with_subcommands(&mut self, yes: bool) -> &mut Self { if yes { self.setting(AppSettings::ArgsNegateSubcommands) } else { @@ -2947,7 +2904,7 @@ impl Command { /// ); /// assert!(matches.subcommand_matches("sub").is_some()); /// ``` - pub fn subcommand_precedence_over_arg(self, yes: bool) -> Self { + pub fn subcommand_precedence_over_arg(&mut self, yes: bool) -> &mut Self { if yes { self.setting(AppSettings::SubcommandPrecedenceOverArg) } else { @@ -3002,7 +2959,7 @@ impl Command { /// /// [`Arg::required(true)`]: crate::Arg::required() /// [`subcommands`]: crate::Command::subcommand() - pub fn subcommand_negates_reqs(self, yes: bool) -> Self { + pub fn subcommand_negates_reqs(&mut self, yes: bool) -> &mut Self { if yes { self.setting(AppSettings::SubcommandsNegateReqs) } else { @@ -3128,7 +3085,7 @@ impl Command { /// [`Command::subcommand_value_name`]: crate::Command::subcommand_value_name /// [`Command::subcommand_help_heading`]: crate::Command::subcommand_help_heading #[inline] - pub fn multicall(self, yes: bool) -> Self { + pub fn multicall(&mut self, yes: bool) -> &mut Self { if yes { self.setting(AppSettings::Multicall) } else { @@ -3196,8 +3153,7 @@ impl Command { /// -h, --help Print help /// -V, --version Print version /// ``` - #[must_use] - pub fn subcommand_value_name(mut self, value_name: impl IntoResettable) -> Self { + pub fn subcommand_value_name(&mut self, value_name: impl IntoResettable) -> &mut Self { self.subcommand_value_name = value_name.into_resettable().into_option(); self } @@ -3262,8 +3218,7 @@ impl Command { /// -h, --help Print help /// -V, --version Print version /// ``` - #[must_use] - pub fn subcommand_help_heading(mut self, heading: impl IntoResettable) -> Self { + pub fn subcommand_help_heading(&mut self, heading: impl IntoResettable) -> &mut Self { self.subcommand_heading = heading.into_resettable().into_option(); self } @@ -4301,16 +4256,13 @@ impl Command { if !self.is_disable_help_flag_set() { debug!("Command::_check_help_and_version: Building default --help"); - let mut arg = Arg::new(Id::HELP) - .short('h') - .long("help") - .action(ArgAction::Help); + let mut arg = Arg::new(Id::HELP); + arg.short('h').long("help").action(ArgAction::Help); if self.long_help_exists { - arg = arg - .help("Print help (see more with '--help')") + arg.help("Print help (see more with '--help')") .long_help("Print help (see a summary with '-h')"); } else { - arg = arg.help("Print help"); + arg.help("Print help"); } // Avoiding `arg_internal` to not be sensitive to `next_help_heading` / // `next_display_order` @@ -4318,8 +4270,8 @@ impl Command { } if !self.is_disable_version_flag_set() { debug!("Command::_check_help_and_version: Building default --version"); - let arg = Arg::new(Id::VERSION) - .short('V') + let mut arg = Arg::new(Id::VERSION); + arg.short('V') .long("version") .action(ArgAction::Version) .help("Print version"); @@ -4334,27 +4286,33 @@ impl Command { let mut help_subcmd = if expand_help_tree { // Slow code path to recursively clone all other subcommand subtrees under help - let help_subcmd = Command::new("help") + let mut help_subcmd = Command::new("help"); + help_subcmd .about(help_about) .global_setting(AppSettings::DisableHelpSubcommand) .subcommands(self.get_subcommands().map(Command::_copy_subtree_for_help)); - let mut help_help_subcmd = Command::new("help").about(help_about); + let mut help_help_subcmd = Command::new("help"); + help_help_subcmd.about(help_about); help_help_subcmd.version = None; help_help_subcmd.long_version = None; - help_help_subcmd = help_help_subcmd + help_help_subcmd .setting(AppSettings::DisableHelpFlag) .setting(AppSettings::DisableVersionFlag); - help_subcmd.subcommand(help_help_subcmd) + help_subcmd.subcommand(help_help_subcmd); + help_subcmd } else { - Command::new("help").about(help_about).arg( - Arg::new("subcommand") - .action(ArgAction::Append) + let mut help_subcmd = Command::new("help"); + help_subcmd.about(help_about).arg({ + let mut sub = Arg::new("subcommand"); + sub.action(ArgAction::Append) .num_args(..) .value_name("COMMAND") - .help("Print help for the subcommand(s)"), - ) + .help("Print help for the subcommand(s)"); + sub + }); + help_subcmd }; self._propagate_subcommand(&mut help_subcmd); @@ -4362,7 +4320,7 @@ impl Command { // advertise it to the user help_subcmd.version = None; help_subcmd.long_version = None; - help_subcmd = help_subcmd + help_subcmd .setting(AppSettings::DisableHelpFlag) .setting(AppSettings::DisableVersionFlag) .unset_global_setting(AppSettings::PropagateVersion); @@ -4372,13 +4330,13 @@ impl Command { } fn _copy_subtree_for_help(&self) -> Command { - let mut cmd = Command::new(self.name.clone()) - .hide(self.is_hide_set()) + let mut cmd = Command::new(self.name.clone()); + cmd.hide(self.is_hide_set()) .global_setting(AppSettings::DisableHelpFlag) .global_setting(AppSettings::DisableVersionFlag) .subcommands(self.get_subcommands().map(Command::_copy_subtree_for_help)); if self.get_about().is_some() { - cmd = cmd.about(self.get_about().unwrap().clone()); + cmd.about(self.get_about().unwrap().clone()); } cmd } @@ -4735,6 +4693,12 @@ impl Index<&'_ Id> for Command { } } +impl From<&'_ mut Command> for Command { + fn from(cmd: &'_ mut Command) -> Self { + cmd.clone() + } +} + impl From<&'_ Command> for Command { fn from(cmd: &'_ Command) -> Self { cmd.clone() diff --git a/clap_builder/src/builder/possible_value.rs b/clap_builder/src/builder/possible_value.rs index de7e543fe81..3a4e91f0e82 100644 --- a/clap_builder/src/builder/possible_value.rs +++ b/clap_builder/src/builder/possible_value.rs @@ -81,8 +81,7 @@ impl PossibleValue { /// # ; /// ``` #[inline] - #[must_use] - pub fn help(mut self, help: impl IntoResettable) -> Self { + pub fn help(&mut self, help: impl IntoResettable) -> &mut Self { self.help = help.into_resettable().into_option(); self } @@ -103,8 +102,7 @@ impl PossibleValue { /// ``` /// [`Arg::hide_possible_values(true)`]: crate::Arg::hide_possible_values() #[inline] - #[must_use] - pub fn hide(mut self, yes: bool) -> Self { + pub fn hide(&mut self, yes: bool) -> &mut Self { self.hide = yes; self } @@ -120,8 +118,7 @@ impl PossibleValue { /// .alias("not-fast") /// # ; /// ``` - #[must_use] - pub fn alias(mut self, name: impl IntoResettable) -> Self { + pub fn alias(&mut self, name: impl IntoResettable) -> &mut Self { if let Some(name) = name.into_resettable().into_option() { self.aliases.push(name); } else { @@ -141,8 +138,7 @@ impl PossibleValue { /// .aliases(["not-fast", "snake-like"]) /// # ; /// ``` - #[must_use] - pub fn aliases(mut self, names: impl IntoIterator>) -> Self { + pub fn aliases(&mut self, names: impl IntoIterator>) -> &mut Self { self.aliases.extend(names.into_iter().map(|a| a.into())); self } diff --git a/clap_builder/src/builder/value_parser.rs b/clap_builder/src/builder/value_parser.rs index 1f0ef925f08..81376e40ce3 100644 --- a/clap_builder/src/builder/value_parser.rs +++ b/clap_builder/src/builder/value_parser.rs @@ -1824,7 +1824,11 @@ impl FalseyValueParser { .iter() .chain(crate::util::FALSE_LITERALS.iter()) .copied() - .map(|l| crate::builder::PossibleValue::new(l).hide(l != "true" && l != "false")) + .map(|l| { + let mut value = crate::builder::PossibleValue::new(l); + value.hide(l != "true" && l != "false"); + value + }) } } @@ -1923,7 +1927,11 @@ impl BoolishValueParser { .iter() .chain(crate::util::FALSE_LITERALS.iter()) .copied() - .map(|l| crate::builder::PossibleValue::new(l).hide(l != "true" && l != "false")) + .map(|l| { + let mut value = crate::builder::PossibleValue::new(l); + value.hide(l != "true" && l != "false"); + value + }) } } diff --git a/clap_builder/src/derive.rs b/clap_builder/src/derive.rs index 7494e8461dc..8c878a81bc9 100644 --- a/clap_builder/src/derive.rs +++ b/clap_builder/src/derive.rs @@ -219,13 +219,13 @@ pub trait Args: FromArgMatches + Sized { /// Append to [`Command`] so it can instantiate `Self`. /// /// See also [`CommandFactory`]. - fn augment_args(cmd: Command) -> Command; + fn augment_args(cmd: &mut Command) -> &mut Command; /// Append to [`Command`] so it can update `self`. /// /// This is used to implement `#[command(flatten)]` /// /// See also [`CommandFactory`]. - fn augment_args_for_update(cmd: Command) -> Command; + fn augment_args_for_update(cmd: &mut Command) -> &mut Command; } /// Parse a sub-command into a user-defined enum. @@ -242,13 +242,13 @@ pub trait Subcommand: FromArgMatches + Sized { /// Append to [`Command`] so it can instantiate `Self`. /// /// See also [`CommandFactory`]. - fn augment_subcommands(cmd: Command) -> Command; + fn augment_subcommands(cmd: &mut Command) -> &mut Command; /// Append to [`Command`] so it can update `self`. /// /// This is used to implement `#[command(flatten)]` /// /// See also [`CommandFactory`]. - fn augment_subcommands_for_update(cmd: Command) -> Command; + fn augment_subcommands_for_update(cmd: &mut Command) -> &mut Command; /// Test whether `Self` can parse a specific subcommand fn has_subcommand(name: &str) -> bool; } @@ -335,19 +335,19 @@ impl FromArgMatches for Box { } impl Args for Box { - fn augment_args(cmd: Command) -> Command { + fn augment_args(cmd: &mut Command) -> &mut Command { ::augment_args(cmd) } - fn augment_args_for_update(cmd: Command) -> Command { + fn augment_args_for_update(cmd: &mut Command) -> &mut Command { ::augment_args_for_update(cmd) } } impl Subcommand for Box { - fn augment_subcommands(cmd: Command) -> Command { + fn augment_subcommands(cmd: &mut Command) -> &mut Command { ::augment_subcommands(cmd) } - fn augment_subcommands_for_update(cmd: Command) -> Command { + fn augment_subcommands_for_update(cmd: &mut Command) -> &mut Command { ::augment_subcommands_for_update(cmd) } fn has_subcommand(name: &str) -> bool { diff --git a/clap_builder/src/macros.rs b/clap_builder/src/macros.rs index 767ea94bc02..9ef517351b7 100644 --- a/clap_builder/src/macros.rs +++ b/clap_builder/src/macros.rs @@ -125,16 +125,17 @@ macro_rules! command { $crate::command!($crate::crate_name!()) }}; ($name:expr) => {{ - let mut cmd = $crate::Command::new($name).version($crate::crate_version!()); + let mut cmd = $crate::Command::new($name); + cmd.version($crate::crate_version!()); let author = $crate::crate_authors!(); if !author.is_empty() { - cmd = cmd.author(author) + cmd.author(author) } let about = $crate::crate_description!(); if !about.is_empty() { - cmd = cmd.about(about) + cmd.about(about) } cmd @@ -196,19 +197,17 @@ macro_rules! arg_impl { debug_assert_eq!($arg.get_value_names(), None, "Flags should precede values"); debug_assert!(!matches!($arg.get_action(), $crate::ArgAction::Append), "Flags should precede `...`"); - let mut arg = $arg; let long = $crate::arg_impl! { @string $long }; - if arg.get_id() == "" { - arg = arg.id(long); + if $arg.get_id() == "" { + $arg.id(long); } let action = $crate::ArgAction::SetTrue; - let arg = arg + $arg .long(long) .action(action); - let arg = $crate::arg_impl! { - @arg (arg) $($tail)* + $crate::arg_impl! { + @arg ($arg) $($tail)* }; - arg }}; ( @arg @@ -219,19 +218,17 @@ macro_rules! arg_impl { debug_assert_eq!($arg.get_value_names(), None, "Flags should precede values"); debug_assert!(!matches!($arg.get_action(), $crate::ArgAction::Append), "Flags should precede `...`"); - let mut arg = $arg; let long = $crate::arg_impl! { @string $long }; - if arg.get_id() == "" { - arg = arg.id(long); + if $arg.get_id() == "" { + $arg.id(long); } let action = $crate::ArgAction::SetTrue; - let arg = arg + $arg .long(long) .action(action); - let arg = $crate::arg_impl! { - @arg (arg) $($tail)* + $crate::arg_impl! { + @arg ($arg) $($tail)* }; - arg }}; ( @arg @@ -244,13 +241,12 @@ macro_rules! arg_impl { debug_assert!(!matches!($arg.get_action(), $crate::ArgAction::Append), "Flags should precede `...`"); let action = $crate::ArgAction::SetTrue; - let arg = $arg + $arg .short($crate::arg_impl! { @char $short }) .action(action); - let arg = $crate::arg_impl! { - @arg (arg) $($tail)* + $crate::arg_impl! { + @arg ($arg) $($tail)* }; - arg }}; ( @arg @@ -263,13 +259,12 @@ macro_rules! arg_impl { debug_assert!(!matches!($arg.get_action(), $crate::ArgAction::Append), "Flags should precede `...`"); let action = $crate::ArgAction::SetTrue; - let arg = $arg + $arg .short($crate::arg_impl! { @char $short }) .action(action); - let arg = $crate::arg_impl! { - @arg (arg) $($tail)* + $crate::arg_impl! { + @arg ($arg) $($tail)* }; - arg }}; ( @arg @@ -280,23 +275,20 @@ macro_rules! arg_impl { debug_assert!(!matches!($arg.get_action(), $crate::ArgAction::Append), "Flags should precede `...`"); debug_assert_eq!($arg.get_value_names(), None, "Multiple values not yet supported"); - let mut arg = $arg; - - if arg.get_long().is_none() && arg.get_short().is_none() { - arg = arg.required(true); + if $arg.get_long().is_none() && $arg.get_short().is_none() { + $arg.required(true); } let value_name = $crate::arg_impl! { @string $value_name }; - if arg.get_id() == "" { - arg = arg.id(value_name); + if $arg.get_id() == "" { + $arg.id(value_name); } - let arg = arg + $arg .value_name(value_name) .action($crate::ArgAction::Set); - let arg = $crate::arg_impl! { - @arg (arg) $($tail)* + $crate::arg_impl! { + @arg ($arg) $($tail)* }; - arg }}; ( @arg @@ -307,23 +299,20 @@ macro_rules! arg_impl { debug_assert!(!matches!($arg.get_action(), $crate::ArgAction::Append), "Flags should precede `...`"); debug_assert_eq!($arg.get_value_names(), None, "Multiple values not yet supported"); - let mut arg = $arg; - if arg.get_long().is_none() && arg.get_short().is_none() { - arg = arg.required(true); + $arg.required(true); } let value_name = $crate::arg_impl! { @string $value_name }; - if arg.get_id() == "" { - arg = arg.id(value_name); + if $arg.get_id() == "" { + $arg.id(value_name); } - let arg = arg + $arg .value_name(value_name) .action($crate::ArgAction::Set); - let arg = $crate::arg_impl! { - @arg (arg) $($tail)* + $crate::arg_impl! { + @arg ($arg) $($tail)* }; - arg }}; ( @arg @@ -334,25 +323,22 @@ macro_rules! arg_impl { debug_assert!(!matches!($arg.get_action(), $crate::ArgAction::Append), "Flags should precede `...`"); debug_assert_eq!($arg.get_value_names(), None, "Multiple values not yet supported"); - let mut arg = $arg; - - if arg.get_long().is_none() && arg.get_short().is_none() { - arg = arg.required(false); + if $arg.get_long().is_none() && $arg.get_short().is_none() { + $arg.required(false); } else { - arg = arg.num_args(0..=1); + $arg.num_args(0..=1); } let value_name = $crate::arg_impl! { @string $value_name }; - if arg.get_id() == "" { - arg = arg.id(value_name); + if $arg.get_id() == "" { + $arg.id(value_name); } - let arg = arg + $arg .value_name(value_name) .action($crate::ArgAction::Set); - let arg = $crate::arg_impl! { - @arg (arg) $($tail)* + $crate::arg_impl! { + @arg ($arg) $($tail)* }; - arg }}; ( @arg @@ -363,25 +349,22 @@ macro_rules! arg_impl { debug_assert!(!matches!($arg.get_action(), $crate::ArgAction::Append), "Flags should precede `...`"); debug_assert_eq!($arg.get_value_names(), None, "Multiple values not yet supported"); - let mut arg = $arg; - - if arg.get_long().is_none() && arg.get_short().is_none() { - arg = arg.required(false); + if $arg.get_long().is_none() && $arg.get_short().is_none() { + $arg.required(false); } else { - arg = arg.num_args(0..=1); + $arg.num_args(0..=1); } let value_name = $crate::arg_impl! { @string $value_name }; - if arg.get_id() == "" { - arg = arg.id(value_name); + if $arg.get_id() == "" { + $arg.id(value_name); } - let arg = arg + $arg .value_name(value_name) .action($crate::ArgAction::Set); - let arg = $crate::arg_impl! { - @arg (arg) $($tail)* + $crate::arg_impl! { + @arg ($arg) $($tail)* }; - arg }}; ( @arg @@ -389,7 +372,7 @@ macro_rules! arg_impl { ... $($tail:tt)* ) => {{ - let arg = match $arg.get_action() { + match $arg.get_action() { $crate::ArgAction::Set => { if $arg.get_long().is_none() && $arg.get_short().is_none() { $arg.num_args(1..) @@ -405,24 +388,22 @@ macro_rules! arg_impl { action => { panic!("Unexpected action {action:?}") } + } + $crate::arg_impl! { + @arg ($arg) $($tail)* }; - let arg = $crate::arg_impl! { - @arg (arg) $($tail)* - }; - arg }}; ( @arg ($arg:expr) $help:literal ) => {{ - $arg.help($help) + $arg.help($help); }}; ( @arg ($arg:expr) ) => {{ - $arg }}; } @@ -515,15 +496,15 @@ macro_rules! arg_impl { #[macro_export] macro_rules! arg { ( $name:ident: $($tail:tt)+ ) => {{ - let arg = $crate::Arg::new($crate::arg_impl! { @string $name }); - let arg = $crate::arg_impl! { + let mut arg = $crate::Arg::new($crate::arg_impl! { @string $name }); + $crate::arg_impl! { @arg (arg) $($tail)+ }; arg }}; ( $($tail:tt)+ ) => {{ - let arg = $crate::Arg::default(); - let arg = $crate::arg_impl! { + let mut arg = $crate::Arg::default(); + $crate::arg_impl! { @arg (arg) $($tail)+ }; debug_assert_ne!(arg.get_id(), "", "Without a value or long flag, the `name:` prefix is required"); diff --git a/clap_builder/src/mkeymap.rs b/clap_builder/src/mkeymap.rs index 301c7bc566c..50b7eb57bb8 100644 --- a/clap_builder/src/mkeymap.rs +++ b/clap_builder/src/mkeymap.rs @@ -141,16 +141,6 @@ impl MKeyMap { append_keys(&mut self.keys, arg, i); } } - - /// Remove an arg in the graph by Id, usually used by `mut_arg`. Return - /// `Some(arg)` if removed. - pub(crate) fn remove_by_name(&mut self, name: &str) -> Option { - self.args - .iter() - .position(|arg| arg.id == name) - // since it's a cold function, using this wouldn't hurt much - .map(|i| self.args.remove(i)) - } } impl Index<&'_ KeyType> for MKeyMap { diff --git a/clap_derive/src/derives/args.rs b/clap_derive/src/derives/args.rs index 20164ff2e24..67d90cce6e7 100644 --- a/clap_derive/src/derives/args.rs +++ b/clap_derive/src/derives/args.rs @@ -212,8 +212,8 @@ pub fn gen_augment( }; Some(quote! { - let #app_var = <#subcmd_type as clap::Subcommand>::augment_subcommands( #app_var ); - let #app_var = #app_var + <#subcmd_type as clap::Subcommand>::augment_subcommands( #app_var ); + #app_var #implicit_methods #override_methods; }) @@ -228,17 +228,17 @@ pub fn gen_augment( let next_display_order = item.next_display_order(); if override_required { Some(quote_spanned! { kind.span()=> - let #app_var = #app_var + #app_var #next_help_heading #next_display_order; - let #app_var = <#inner_type as clap::Args>::augment_args_for_update(#app_var); + <#inner_type as clap::Args>::augment_args_for_update(#app_var); }) } else { Some(quote_spanned! { kind.span()=> - let #app_var = #app_var + #app_var #next_help_heading #next_display_order; - let #app_var = <#inner_type as clap::Args>::augment_args(#app_var); + <#inner_type as clap::Args>::augment_args(#app_var); }) } } @@ -343,17 +343,18 @@ pub fn gen_augment( }; Some(quote_spanned! { field.span()=> - let #app_var = #app_var.arg({ + #app_var.arg({ #deprecations + let mut arg = clap::Arg::new(#id); #[allow(deprecated)] - let arg = clap::Arg::new(#id) + arg. #implicit_methods; - let arg = arg + arg #explicit_methods; - let arg = arg + arg #override_methods; arg @@ -420,7 +421,7 @@ pub fn gen_augment( }; Ok(quote! {{ #deprecations - let #app_var = #app_var + #app_var #initial_app_methods #group_app_methods ; diff --git a/clap_derive/src/derives/into_app.rs b/clap_derive/src/derives/into_app.rs index 0bd636245a9..86148f8b8a0 100644 --- a/clap_derive/src/derives/into_app.rs +++ b/clap_derive/src/derives/into_app.rs @@ -51,13 +51,15 @@ pub fn gen_for_struct( #[automatically_derived] impl #impl_generics clap::CommandFactory for #item_name #ty_generics #where_clause { fn command<'b>() -> clap::Command { - let #app_var = clap::Command::new(#name); - ::augment_args(#app_var) + let mut #app_var = clap::Command::new(#name); + ::augment_args(&mut #app_var); + #app_var } fn command_for_update<'b>() -> clap::Command { - let #app_var = clap::Command::new(#name); - ::augment_args_for_update(#app_var) + let mut #app_var = clap::Command::new(#name); + ::augment_args_for_update(&mut #app_var); + #app_var } } }; @@ -98,17 +100,20 @@ pub fn gen_for_enum( #[automatically_derived] impl #impl_generics clap::CommandFactory for #item_name #ty_generics #where_clause { fn command<'b>() -> clap::Command { - let #app_var = clap::Command::new(#name) + let mut #app_var = clap::Command::new(#name); + #app_var .subcommand_required(true) .arg_required_else_help(true); - ::augment_subcommands(#app_var) + ::augment_subcommands(&mut #app_var); + #app_var } fn command_for_update<'b>() -> clap::Command { - let #app_var = clap::Command::new(#name); - ::augment_subcommands_for_update(#app_var) + let &mut #app_var = clap::Command::new(#name); + ::augment_subcommands_for_update(&mut #app_var) .subcommand_required(false) - .arg_required_else_help(false) + .arg_required_else_help(false); + #app_var } } }) diff --git a/clap_derive/src/derives/subcommand.rs b/clap_derive/src/derives/subcommand.rs index 125a40f6105..c4dbd6a11f7 100644 --- a/clap_derive/src/derives/subcommand.rs +++ b/clap_derive/src/derives/subcommand.rs @@ -174,7 +174,7 @@ fn gen_augment( })?; let subcommand = quote_spanned! { kind.span()=> #deprecations - let #app_var = #app_var + #app_var .external_subcommand_value_parser(clap::value_parser!(#subty)); }; Some(subcommand) @@ -193,18 +193,18 @@ fn gen_augment( let subcommand = if override_required { quote! { #deprecations - let #app_var = #app_var + #app_var #next_help_heading #next_display_order; - let #app_var = <#ty as clap::Subcommand>::augment_subcommands_for_update(#app_var); + <#ty as clap::Subcommand>::augment_subcommands_for_update(#app_var); } } else { quote! { #deprecations - let #app_var = #app_var + #app_var #next_help_heading #next_display_order; - let #app_var = <#ty as clap::Subcommand>::augment_subcommands(#app_var); + <#ty as clap::Subcommand>::augment_subcommands(#app_var); } }; Some(subcommand) @@ -260,14 +260,14 @@ fn gen_augment( quote!() }; let subcommand = quote! { - let #app_var = #app_var.subcommand({ + #app_var.subcommand({ #deprecations; - let #subcommand_var = clap::Command::new(#name); - let #subcommand_var = #subcommand_var + let mut #subcommand_var = clap::Command::new(#name); + #subcommand_var .subcommand_required(true) .arg_required_else_help(true); - let #subcommand_var = #subcommand_var #initial_app_methods; - let #subcommand_var = #arg_block; + #subcommand_var #initial_app_methods; + #arg_block; #subcommand_var #final_from_attrs #override_methods }); }; @@ -327,9 +327,9 @@ fn gen_augment( }; let name = item.cased_name(); let subcommand = quote! { - let #app_var = #app_var.subcommand({ + #app_var.subcommand({ #deprecations - let #subcommand_var = clap::Command::new(#name); + let mut #subcommand_var = clap::Command::new(#name); #sub_augment }); }; @@ -348,7 +348,7 @@ fn gen_augment( let final_app_methods = parent_item.final_top_level_methods(); Ok(quote! { #deprecations; - let #app_var = #app_var #initial_app_methods; + #app_var #initial_app_methods; #( #subcommands )*; #app_var #final_app_methods }) diff --git a/src/bin/stdio-fixture.rs b/src/bin/stdio-fixture.rs index efd6c817bdf..bcb1d133eae 100644 --- a/src/bin/stdio-fixture.rs +++ b/src/bin/stdio-fixture.rs @@ -1,7 +1,7 @@ fn main() { #[allow(unused_mut)] - let mut cmd = clap::Command::new("stdio-fixture") - .version("1.0") + let mut cmd = clap::Command::new("stdio-fixture"); + cmd.version("1.0") .long_version("1.0 - a2132c") .arg_required_else_help(true) .subcommand(clap::Command::new("more")) @@ -20,7 +20,7 @@ fn main() { .usage(styling::AnsiColor::Green.on_default() | styling::Effects::BOLD) .literal(styling::AnsiColor::Blue.on_default() | styling::Effects::BOLD) .placeholder(styling::AnsiColor::Cyan.on_default()); - cmd = cmd.styles(styles); + cmd.styles(styles); } cmd.get_matches(); }