diff --git a/clap_builder/src/builder/command.rs b/clap_builder/src/builder/command.rs index 36f43a36b9b..6472071fcee 100644 --- a/clap_builder/src/builder/command.rs +++ b/clap_builder/src/builder/command.rs @@ -107,6 +107,8 @@ pub struct Command { external_value_parser: Option, long_help_exists: bool, deferred: Option Command>, + #[cfg(feature = "unstable-ext")] + ext: Extensions, app_ext: Extensions, } @@ -1040,6 +1042,14 @@ impl Command { Usage::new(self).create_usage_with_title(&[]) } + + /// Extend [`Command`] with [`CommandExt`] data + #[cfg(feature = "unstable-ext")] + #[allow(clippy::should_implement_trait)] + pub fn add(mut self, tagged: T) -> Self { + self.ext.set(tagged); + self + } } /// # Application-wide Settings @@ -3965,6 +3975,18 @@ impl Command { pub fn is_multicall_set(&self) -> bool { self.is_set(AppSettings::Multicall) } + + /// Access an [`CommandExt`] + #[cfg(feature = "unstable-ext")] + pub fn get(&self) -> Option<&T> { + self.ext.get::() + } + + /// Remove an [`CommandExt`] + #[cfg(feature = "unstable-ext")] + pub fn remove(mut self) -> Option { + self.ext.remove::() + } } // Internally used only @@ -4884,6 +4906,8 @@ impl Default for Command { external_value_parser: Default::default(), long_help_exists: false, deferred: None, + #[cfg(feature = "unstable-ext")] + ext: Default::default(), app_ext: Default::default(), } } @@ -4909,6 +4933,10 @@ impl fmt::Display for Command { } } +/// User-provided data that can be attached to an [`Arg`] +#[cfg(feature = "unstable-ext")] +pub trait CommandExt: Extension {} + #[allow(dead_code)] // atm dependent on features enabled pub(crate) trait AppExt: Extension {} diff --git a/clap_builder/src/builder/mod.rs b/clap_builder/src/builder/mod.rs index 1645547fc34..9f871d7ff7c 100644 --- a/clap_builder/src/builder/mod.rs +++ b/clap_builder/src/builder/mod.rs @@ -33,6 +33,8 @@ pub use arg::ArgExt; pub use arg_group::ArgGroup; pub use arg_predicate::ArgPredicate; pub use command::Command; +#[cfg(feature = "unstable-ext")] +pub use command::CommandExt; pub use os_str::OsStr; pub use possible_value::PossibleValue; pub use range::ValueRange; @@ -41,9 +43,9 @@ pub use resettable::Resettable; pub use styled_str::StyledStr; pub use styling::Styles; pub use value_hint::ValueHint; +pub use value_parser::BoolValueParser; pub use value_parser::_infer_ValueParser_for; pub use value_parser::impl_prelude; -pub use value_parser::BoolValueParser; pub use value_parser::BoolishValueParser; pub use value_parser::EnumValueParser; pub use value_parser::FalseyValueParser;