119625d8cSopenharmony_ci//! This module contains traits that are usable with the `#[derive(...)].` 219625d8cSopenharmony_ci//! macros in [`clap_derive`]. 319625d8cSopenharmony_ci 419625d8cSopenharmony_ciuse crate::builder::PossibleValue; 519625d8cSopenharmony_ciuse crate::{ArgMatches, Command, Error}; 619625d8cSopenharmony_ci 719625d8cSopenharmony_ciuse std::ffi::OsString; 819625d8cSopenharmony_ci 919625d8cSopenharmony_ci/// Parse command-line arguments into `Self`. 1019625d8cSopenharmony_ci/// 1119625d8cSopenharmony_ci/// The primary one-stop-shop trait used to create an instance of a `clap` 1219625d8cSopenharmony_ci/// [`Command`], conduct the parsing, and turn the resulting [`ArgMatches`] back 1319625d8cSopenharmony_ci/// into concrete instance of the user struct. 1419625d8cSopenharmony_ci/// 1519625d8cSopenharmony_ci/// This trait is primarily a convenience on top of [`FromArgMatches`] + 1619625d8cSopenharmony_ci/// [`CommandFactory`] which uses those two underlying traits to build the two 1719625d8cSopenharmony_ci/// fundamental functions `parse` which uses the `std::env::args_os` iterator, 1819625d8cSopenharmony_ci/// and `parse_from` which allows the consumer to supply the iterator (along 1919625d8cSopenharmony_ci/// with fallible options for each). 2019625d8cSopenharmony_ci/// 2119625d8cSopenharmony_ci/// See also [`Subcommand`] and [`Args`]. 2219625d8cSopenharmony_ci/// 2319625d8cSopenharmony_ci/// See the [derive reference](crate::_derive) for attributes and best practices. 2419625d8cSopenharmony_ci/// 2519625d8cSopenharmony_ci/// **NOTE:** Deriving requires the [`derive` feature flag][crate::_features] 2619625d8cSopenharmony_ci/// 2719625d8cSopenharmony_ci/// # Examples 2819625d8cSopenharmony_ci/// 2919625d8cSopenharmony_ci/// The following example creates a `Context` struct that would be used 3019625d8cSopenharmony_ci/// throughout the application representing the normalized values coming from 3119625d8cSopenharmony_ci/// the CLI. 3219625d8cSopenharmony_ci/// 3319625d8cSopenharmony_ci#[cfg_attr(not(feature = "derive"), doc = " ```ignore")] 3419625d8cSopenharmony_ci#[cfg_attr(feature = "derive", doc = " ```")] 3519625d8cSopenharmony_ci/// /// My super CLI 3619625d8cSopenharmony_ci/// #[derive(clap::Parser)] 3719625d8cSopenharmony_ci/// #[command(name = "demo")] 3819625d8cSopenharmony_ci/// struct Context { 3919625d8cSopenharmony_ci/// /// More verbose output 4019625d8cSopenharmony_ci/// #[arg(long)] 4119625d8cSopenharmony_ci/// verbose: bool, 4219625d8cSopenharmony_ci/// /// An optional name 4319625d8cSopenharmony_ci/// #[arg(short, long)] 4419625d8cSopenharmony_ci/// name: Option<String>, 4519625d8cSopenharmony_ci/// } 4619625d8cSopenharmony_ci/// ``` 4719625d8cSopenharmony_ci/// 4819625d8cSopenharmony_ci/// The equivalent [`Command`] struct + `From` implementation: 4919625d8cSopenharmony_ci/// 5019625d8cSopenharmony_ci/// ```rust 5119625d8cSopenharmony_ci/// # use clap::{Command, Arg, ArgMatches, ArgAction}; 5219625d8cSopenharmony_ci/// Command::new("demo") 5319625d8cSopenharmony_ci/// .about("My super CLI") 5419625d8cSopenharmony_ci/// .arg(Arg::new("verbose") 5519625d8cSopenharmony_ci/// .long("verbose") 5619625d8cSopenharmony_ci/// .action(ArgAction::SetTrue) 5719625d8cSopenharmony_ci/// .help("More verbose output")) 5819625d8cSopenharmony_ci/// .arg(Arg::new("name") 5919625d8cSopenharmony_ci/// .long("name") 6019625d8cSopenharmony_ci/// .short('n') 6119625d8cSopenharmony_ci/// .help("An optional name") 6219625d8cSopenharmony_ci/// .action(ArgAction::Set)); 6319625d8cSopenharmony_ci/// 6419625d8cSopenharmony_ci/// struct Context { 6519625d8cSopenharmony_ci/// verbose: bool, 6619625d8cSopenharmony_ci/// name: Option<String>, 6719625d8cSopenharmony_ci/// } 6819625d8cSopenharmony_ci/// 6919625d8cSopenharmony_ci/// impl From<ArgMatches> for Context { 7019625d8cSopenharmony_ci/// fn from(m: ArgMatches) -> Self { 7119625d8cSopenharmony_ci/// Context { 7219625d8cSopenharmony_ci/// verbose: m.get_flag("verbose"), 7319625d8cSopenharmony_ci/// name: m.get_one::<String>("name").cloned(), 7419625d8cSopenharmony_ci/// } 7519625d8cSopenharmony_ci/// } 7619625d8cSopenharmony_ci/// } 7719625d8cSopenharmony_ci/// ``` 7819625d8cSopenharmony_ci/// 7919625d8cSopenharmony_cipub trait Parser: FromArgMatches + CommandFactory + Sized { 8019625d8cSopenharmony_ci /// Parse from `std::env::args_os()`, exit on error 8119625d8cSopenharmony_ci fn parse() -> Self { 8219625d8cSopenharmony_ci let mut matches = <Self as CommandFactory>::command().get_matches(); 8319625d8cSopenharmony_ci let res = <Self as FromArgMatches>::from_arg_matches_mut(&mut matches) 8419625d8cSopenharmony_ci .map_err(format_error::<Self>); 8519625d8cSopenharmony_ci match res { 8619625d8cSopenharmony_ci Ok(s) => s, 8719625d8cSopenharmony_ci Err(e) => { 8819625d8cSopenharmony_ci // Since this is more of a development-time error, we aren't doing as fancy of a quit 8919625d8cSopenharmony_ci // as `get_matches` 9019625d8cSopenharmony_ci e.exit() 9119625d8cSopenharmony_ci } 9219625d8cSopenharmony_ci } 9319625d8cSopenharmony_ci } 9419625d8cSopenharmony_ci 9519625d8cSopenharmony_ci /// Parse from `std::env::args_os()`, return Err on error. 9619625d8cSopenharmony_ci fn try_parse() -> Result<Self, Error> { 9719625d8cSopenharmony_ci let mut matches = ok!(<Self as CommandFactory>::command().try_get_matches()); 9819625d8cSopenharmony_ci <Self as FromArgMatches>::from_arg_matches_mut(&mut matches).map_err(format_error::<Self>) 9919625d8cSopenharmony_ci } 10019625d8cSopenharmony_ci 10119625d8cSopenharmony_ci /// Parse from iterator, exit on error 10219625d8cSopenharmony_ci fn parse_from<I, T>(itr: I) -> Self 10319625d8cSopenharmony_ci where 10419625d8cSopenharmony_ci I: IntoIterator<Item = T>, 10519625d8cSopenharmony_ci T: Into<OsString> + Clone, 10619625d8cSopenharmony_ci { 10719625d8cSopenharmony_ci let mut matches = <Self as CommandFactory>::command().get_matches_from(itr); 10819625d8cSopenharmony_ci let res = <Self as FromArgMatches>::from_arg_matches_mut(&mut matches) 10919625d8cSopenharmony_ci .map_err(format_error::<Self>); 11019625d8cSopenharmony_ci match res { 11119625d8cSopenharmony_ci Ok(s) => s, 11219625d8cSopenharmony_ci Err(e) => { 11319625d8cSopenharmony_ci // Since this is more of a development-time error, we aren't doing as fancy of a quit 11419625d8cSopenharmony_ci // as `get_matches_from` 11519625d8cSopenharmony_ci e.exit() 11619625d8cSopenharmony_ci } 11719625d8cSopenharmony_ci } 11819625d8cSopenharmony_ci } 11919625d8cSopenharmony_ci 12019625d8cSopenharmony_ci /// Parse from iterator, return Err on error. 12119625d8cSopenharmony_ci fn try_parse_from<I, T>(itr: I) -> Result<Self, Error> 12219625d8cSopenharmony_ci where 12319625d8cSopenharmony_ci I: IntoIterator<Item = T>, 12419625d8cSopenharmony_ci T: Into<OsString> + Clone, 12519625d8cSopenharmony_ci { 12619625d8cSopenharmony_ci let mut matches = ok!(<Self as CommandFactory>::command().try_get_matches_from(itr)); 12719625d8cSopenharmony_ci <Self as FromArgMatches>::from_arg_matches_mut(&mut matches).map_err(format_error::<Self>) 12819625d8cSopenharmony_ci } 12919625d8cSopenharmony_ci 13019625d8cSopenharmony_ci /// Update from iterator, exit on error 13119625d8cSopenharmony_ci fn update_from<I, T>(&mut self, itr: I) 13219625d8cSopenharmony_ci where 13319625d8cSopenharmony_ci I: IntoIterator<Item = T>, 13419625d8cSopenharmony_ci T: Into<OsString> + Clone, 13519625d8cSopenharmony_ci { 13619625d8cSopenharmony_ci let mut matches = <Self as CommandFactory>::command_for_update().get_matches_from(itr); 13719625d8cSopenharmony_ci let res = <Self as FromArgMatches>::update_from_arg_matches_mut(self, &mut matches) 13819625d8cSopenharmony_ci .map_err(format_error::<Self>); 13919625d8cSopenharmony_ci if let Err(e) = res { 14019625d8cSopenharmony_ci // Since this is more of a development-time error, we aren't doing as fancy of a quit 14119625d8cSopenharmony_ci // as `get_matches_from` 14219625d8cSopenharmony_ci e.exit() 14319625d8cSopenharmony_ci } 14419625d8cSopenharmony_ci } 14519625d8cSopenharmony_ci 14619625d8cSopenharmony_ci /// Update from iterator, return Err on error. 14719625d8cSopenharmony_ci fn try_update_from<I, T>(&mut self, itr: I) -> Result<(), Error> 14819625d8cSopenharmony_ci where 14919625d8cSopenharmony_ci I: IntoIterator<Item = T>, 15019625d8cSopenharmony_ci T: Into<OsString> + Clone, 15119625d8cSopenharmony_ci { 15219625d8cSopenharmony_ci let mut matches = 15319625d8cSopenharmony_ci ok!(<Self as CommandFactory>::command_for_update().try_get_matches_from(itr)); 15419625d8cSopenharmony_ci <Self as FromArgMatches>::update_from_arg_matches_mut(self, &mut matches) 15519625d8cSopenharmony_ci .map_err(format_error::<Self>) 15619625d8cSopenharmony_ci } 15719625d8cSopenharmony_ci} 15819625d8cSopenharmony_ci 15919625d8cSopenharmony_ci/// Create a [`Command`] relevant for a user-defined container. 16019625d8cSopenharmony_ci/// 16119625d8cSopenharmony_ci/// Derived as part of [`Parser`]. 16219625d8cSopenharmony_cipub trait CommandFactory: Sized { 16319625d8cSopenharmony_ci /// Build a [`Command`] that can instantiate `Self`. 16419625d8cSopenharmony_ci /// 16519625d8cSopenharmony_ci /// See [`FromArgMatches::from_arg_matches_mut`] for instantiating `Self`. 16619625d8cSopenharmony_ci fn command() -> Command; 16719625d8cSopenharmony_ci /// Build a [`Command`] that can update `self`. 16819625d8cSopenharmony_ci /// 16919625d8cSopenharmony_ci /// See [`FromArgMatches::update_from_arg_matches_mut`] for updating `self`. 17019625d8cSopenharmony_ci fn command_for_update() -> Command; 17119625d8cSopenharmony_ci} 17219625d8cSopenharmony_ci 17319625d8cSopenharmony_ci/// Converts an instance of [`ArgMatches`] to a user-defined container. 17419625d8cSopenharmony_ci/// 17519625d8cSopenharmony_ci/// Derived as part of [`Parser`], [`Args`], and [`Subcommand`]. 17619625d8cSopenharmony_cipub trait FromArgMatches: Sized { 17719625d8cSopenharmony_ci /// Instantiate `Self` from [`ArgMatches`], parsing the arguments as needed. 17819625d8cSopenharmony_ci /// 17919625d8cSopenharmony_ci /// Motivation: If our application had two CLI options, `--name 18019625d8cSopenharmony_ci /// <STRING>` and the flag `--debug`, we may create a struct as follows: 18119625d8cSopenharmony_ci /// 18219625d8cSopenharmony_ci #[cfg_attr(not(feature = "derive"), doc = " ```ignore")] 18319625d8cSopenharmony_ci #[cfg_attr(feature = "derive", doc = " ```no_run")] 18419625d8cSopenharmony_ci /// struct Context { 18519625d8cSopenharmony_ci /// name: String, 18619625d8cSopenharmony_ci /// debug: bool 18719625d8cSopenharmony_ci /// } 18819625d8cSopenharmony_ci /// ``` 18919625d8cSopenharmony_ci /// 19019625d8cSopenharmony_ci /// We then need to convert the `ArgMatches` that `clap` generated into our struct. 19119625d8cSopenharmony_ci /// `from_arg_matches` serves as the equivalent of: 19219625d8cSopenharmony_ci /// 19319625d8cSopenharmony_ci #[cfg_attr(not(feature = "derive"), doc = " ```ignore")] 19419625d8cSopenharmony_ci #[cfg_attr(feature = "derive", doc = " ```no_run")] 19519625d8cSopenharmony_ci /// # use clap::ArgMatches; 19619625d8cSopenharmony_ci /// # struct Context { 19719625d8cSopenharmony_ci /// # name: String, 19819625d8cSopenharmony_ci /// # debug: bool 19919625d8cSopenharmony_ci /// # } 20019625d8cSopenharmony_ci /// impl From<ArgMatches> for Context { 20119625d8cSopenharmony_ci /// fn from(m: ArgMatches) -> Self { 20219625d8cSopenharmony_ci /// Context { 20319625d8cSopenharmony_ci /// name: m.get_one::<String>("name").unwrap().clone(), 20419625d8cSopenharmony_ci /// debug: m.get_flag("debug"), 20519625d8cSopenharmony_ci /// } 20619625d8cSopenharmony_ci /// } 20719625d8cSopenharmony_ci /// } 20819625d8cSopenharmony_ci /// ``` 20919625d8cSopenharmony_ci fn from_arg_matches(matches: &ArgMatches) -> Result<Self, Error>; 21019625d8cSopenharmony_ci 21119625d8cSopenharmony_ci /// Instantiate `Self` from [`ArgMatches`], parsing the arguments as needed. 21219625d8cSopenharmony_ci /// 21319625d8cSopenharmony_ci /// Motivation: If our application had two CLI options, `--name 21419625d8cSopenharmony_ci /// <STRING>` and the flag `--debug`, we may create a struct as follows: 21519625d8cSopenharmony_ci /// 21619625d8cSopenharmony_ci #[cfg_attr(not(feature = "derive"), doc = " ```ignore")] 21719625d8cSopenharmony_ci #[cfg_attr(feature = "derive", doc = " ```no_run")] 21819625d8cSopenharmony_ci /// struct Context { 21919625d8cSopenharmony_ci /// name: String, 22019625d8cSopenharmony_ci /// debug: bool 22119625d8cSopenharmony_ci /// } 22219625d8cSopenharmony_ci /// ``` 22319625d8cSopenharmony_ci /// 22419625d8cSopenharmony_ci /// We then need to convert the `ArgMatches` that `clap` generated into our struct. 22519625d8cSopenharmony_ci /// `from_arg_matches_mut` serves as the equivalent of: 22619625d8cSopenharmony_ci /// 22719625d8cSopenharmony_ci #[cfg_attr(not(feature = "derive"), doc = " ```ignore")] 22819625d8cSopenharmony_ci #[cfg_attr(feature = "derive", doc = " ```no_run")] 22919625d8cSopenharmony_ci /// # use clap::ArgMatches; 23019625d8cSopenharmony_ci /// # struct Context { 23119625d8cSopenharmony_ci /// # name: String, 23219625d8cSopenharmony_ci /// # debug: bool 23319625d8cSopenharmony_ci /// # } 23419625d8cSopenharmony_ci /// impl From<ArgMatches> for Context { 23519625d8cSopenharmony_ci /// fn from(m: ArgMatches) -> Self { 23619625d8cSopenharmony_ci /// Context { 23719625d8cSopenharmony_ci /// name: m.get_one::<String>("name").unwrap().to_string(), 23819625d8cSopenharmony_ci /// debug: m.get_flag("debug"), 23919625d8cSopenharmony_ci /// } 24019625d8cSopenharmony_ci /// } 24119625d8cSopenharmony_ci /// } 24219625d8cSopenharmony_ci /// ``` 24319625d8cSopenharmony_ci fn from_arg_matches_mut(matches: &mut ArgMatches) -> Result<Self, Error> { 24419625d8cSopenharmony_ci Self::from_arg_matches(matches) 24519625d8cSopenharmony_ci } 24619625d8cSopenharmony_ci 24719625d8cSopenharmony_ci /// Assign values from `ArgMatches` to `self`. 24819625d8cSopenharmony_ci fn update_from_arg_matches(&mut self, matches: &ArgMatches) -> Result<(), Error>; 24919625d8cSopenharmony_ci 25019625d8cSopenharmony_ci /// Assign values from `ArgMatches` to `self`. 25119625d8cSopenharmony_ci fn update_from_arg_matches_mut(&mut self, matches: &mut ArgMatches) -> Result<(), Error> { 25219625d8cSopenharmony_ci self.update_from_arg_matches(matches) 25319625d8cSopenharmony_ci } 25419625d8cSopenharmony_ci} 25519625d8cSopenharmony_ci 25619625d8cSopenharmony_ci/// Parse a set of arguments into a user-defined container. 25719625d8cSopenharmony_ci/// 25819625d8cSopenharmony_ci/// Implementing this trait lets a parent container delegate argument parsing behavior to `Self`. 25919625d8cSopenharmony_ci/// with: 26019625d8cSopenharmony_ci/// - `#[command(flatten)] args: ChildArgs`: Attribute can only be used with struct fields that impl 26119625d8cSopenharmony_ci/// `Args`. 26219625d8cSopenharmony_ci/// - `Variant(ChildArgs)`: No attribute is used with enum variants that impl `Args`. 26319625d8cSopenharmony_ci/// 26419625d8cSopenharmony_ci/// See the [derive reference](crate::_derive) for attributes and best practices. 26519625d8cSopenharmony_ci/// 26619625d8cSopenharmony_ci/// **NOTE:** Deriving requires the [`derive` feature flag][crate::_features] 26719625d8cSopenharmony_ci/// 26819625d8cSopenharmony_ci/// # Example 26919625d8cSopenharmony_ci/// 27019625d8cSopenharmony_ci#[cfg_attr(not(feature = "derive"), doc = " ```ignore")] 27119625d8cSopenharmony_ci#[cfg_attr(feature = "derive", doc = " ```")] 27219625d8cSopenharmony_ci/// #[derive(clap::Parser)] 27319625d8cSopenharmony_ci/// struct Args { 27419625d8cSopenharmony_ci/// #[command(flatten)] 27519625d8cSopenharmony_ci/// logging: LogArgs, 27619625d8cSopenharmony_ci/// } 27719625d8cSopenharmony_ci/// 27819625d8cSopenharmony_ci/// #[derive(clap::Args)] 27919625d8cSopenharmony_ci/// struct LogArgs { 28019625d8cSopenharmony_ci/// #[arg(long, short = 'v', action = clap::ArgAction::Count)] 28119625d8cSopenharmony_ci/// verbose: u8, 28219625d8cSopenharmony_ci/// } 28319625d8cSopenharmony_ci/// ``` 28419625d8cSopenharmony_cipub trait Args: FromArgMatches + Sized { 28519625d8cSopenharmony_ci /// Report the [`ArgGroup::id`][crate::ArgGroup::id] for this set of arguments 28619625d8cSopenharmony_ci fn group_id() -> Option<crate::Id> { 28719625d8cSopenharmony_ci None 28819625d8cSopenharmony_ci } 28919625d8cSopenharmony_ci /// Append to [`Command`] so it can instantiate `Self`. 29019625d8cSopenharmony_ci /// 29119625d8cSopenharmony_ci /// See also [`CommandFactory`]. 29219625d8cSopenharmony_ci fn augment_args(cmd: Command) -> Command; 29319625d8cSopenharmony_ci /// Append to [`Command`] so it can update `self`. 29419625d8cSopenharmony_ci /// 29519625d8cSopenharmony_ci /// This is used to implement `#[command(flatten)]` 29619625d8cSopenharmony_ci /// 29719625d8cSopenharmony_ci /// See also [`CommandFactory`]. 29819625d8cSopenharmony_ci fn augment_args_for_update(cmd: Command) -> Command; 29919625d8cSopenharmony_ci} 30019625d8cSopenharmony_ci 30119625d8cSopenharmony_ci/// Parse a sub-command into a user-defined enum. 30219625d8cSopenharmony_ci/// 30319625d8cSopenharmony_ci/// Implementing this trait lets a parent container delegate subcommand behavior to `Self`. 30419625d8cSopenharmony_ci/// with: 30519625d8cSopenharmony_ci/// - `#[command(subcommand)] field: SubCmd`: Attribute can be used with either struct fields or enum 30619625d8cSopenharmony_ci/// variants that impl `Subcommand`. 30719625d8cSopenharmony_ci/// - `#[command(flatten)] Variant(SubCmd)`: Attribute can only be used with enum variants that impl 30819625d8cSopenharmony_ci/// `Subcommand`. 30919625d8cSopenharmony_ci/// 31019625d8cSopenharmony_ci/// See the [derive reference](crate::_derive) for attributes and best practices. 31119625d8cSopenharmony_ci/// 31219625d8cSopenharmony_ci/// **NOTE:** Deriving requires the [`derive` feature flag][crate::_features] 31319625d8cSopenharmony_ci/// 31419625d8cSopenharmony_ci/// # Example 31519625d8cSopenharmony_ci/// 31619625d8cSopenharmony_ci#[cfg_attr(not(feature = "derive"), doc = " ```ignore")] 31719625d8cSopenharmony_ci#[cfg_attr(feature = "derive", doc = " ```")] 31819625d8cSopenharmony_ci/// #[derive(clap::Parser)] 31919625d8cSopenharmony_ci/// struct Args { 32019625d8cSopenharmony_ci/// #[command(subcommand)] 32119625d8cSopenharmony_ci/// action: Action, 32219625d8cSopenharmony_ci/// } 32319625d8cSopenharmony_ci/// 32419625d8cSopenharmony_ci/// #[derive(clap::Subcommand)] 32519625d8cSopenharmony_ci/// enum Action { 32619625d8cSopenharmony_ci/// Add, 32719625d8cSopenharmony_ci/// Remove, 32819625d8cSopenharmony_ci/// } 32919625d8cSopenharmony_ci/// ``` 33019625d8cSopenharmony_cipub trait Subcommand: FromArgMatches + Sized { 33119625d8cSopenharmony_ci /// Append to [`Command`] so it can instantiate `Self`. 33219625d8cSopenharmony_ci /// 33319625d8cSopenharmony_ci /// See also [`CommandFactory`]. 33419625d8cSopenharmony_ci fn augment_subcommands(cmd: Command) -> Command; 33519625d8cSopenharmony_ci /// Append to [`Command`] so it can update `self`. 33619625d8cSopenharmony_ci /// 33719625d8cSopenharmony_ci /// This is used to implement `#[command(flatten)]` 33819625d8cSopenharmony_ci /// 33919625d8cSopenharmony_ci /// See also [`CommandFactory`]. 34019625d8cSopenharmony_ci fn augment_subcommands_for_update(cmd: Command) -> Command; 34119625d8cSopenharmony_ci /// Test whether `Self` can parse a specific subcommand 34219625d8cSopenharmony_ci fn has_subcommand(name: &str) -> bool; 34319625d8cSopenharmony_ci} 34419625d8cSopenharmony_ci 34519625d8cSopenharmony_ci/// Parse arguments into enums. 34619625d8cSopenharmony_ci/// 34719625d8cSopenharmony_ci/// When deriving [`Parser`], a field whose type implements `ValueEnum` can have the attribute 34819625d8cSopenharmony_ci/// `#[arg(value_enum)]` which will 34919625d8cSopenharmony_ci/// - Call [`EnumValueParser`][crate::builder::EnumValueParser] 35019625d8cSopenharmony_ci/// - Allowing using the `#[arg(default_value_t)]` attribute without implementing `Display`. 35119625d8cSopenharmony_ci/// 35219625d8cSopenharmony_ci/// See the [derive reference](crate::_derive) for attributes and best practices. 35319625d8cSopenharmony_ci/// 35419625d8cSopenharmony_ci/// **NOTE:** Deriving requires the [`derive` feature flag][crate::_features] 35519625d8cSopenharmony_ci/// 35619625d8cSopenharmony_ci/// # Example 35719625d8cSopenharmony_ci/// 35819625d8cSopenharmony_ci#[cfg_attr(not(feature = "derive"), doc = " ```ignore")] 35919625d8cSopenharmony_ci#[cfg_attr(feature = "derive", doc = " ```")] 36019625d8cSopenharmony_ci/// #[derive(clap::Parser)] 36119625d8cSopenharmony_ci/// struct Args { 36219625d8cSopenharmony_ci/// #[arg(value_enum)] 36319625d8cSopenharmony_ci/// level: Level, 36419625d8cSopenharmony_ci/// } 36519625d8cSopenharmony_ci/// 36619625d8cSopenharmony_ci/// #[derive(clap::ValueEnum, Clone)] 36719625d8cSopenharmony_ci/// enum Level { 36819625d8cSopenharmony_ci/// Debug, 36919625d8cSopenharmony_ci/// Info, 37019625d8cSopenharmony_ci/// Warning, 37119625d8cSopenharmony_ci/// Error, 37219625d8cSopenharmony_ci/// } 37319625d8cSopenharmony_ci/// ``` 37419625d8cSopenharmony_cipub trait ValueEnum: Sized + Clone { 37519625d8cSopenharmony_ci /// All possible argument values, in display order. 37619625d8cSopenharmony_ci fn value_variants<'a>() -> &'a [Self]; 37719625d8cSopenharmony_ci 37819625d8cSopenharmony_ci /// Parse an argument into `Self`. 37919625d8cSopenharmony_ci fn from_str(input: &str, ignore_case: bool) -> Result<Self, String> { 38019625d8cSopenharmony_ci Self::value_variants() 38119625d8cSopenharmony_ci .iter() 38219625d8cSopenharmony_ci .find(|v| { 38319625d8cSopenharmony_ci v.to_possible_value() 38419625d8cSopenharmony_ci .expect("ValueEnum::value_variants contains only values with a corresponding ValueEnum::to_possible_value") 38519625d8cSopenharmony_ci .matches(input, ignore_case) 38619625d8cSopenharmony_ci }) 38719625d8cSopenharmony_ci .cloned() 38819625d8cSopenharmony_ci .ok_or_else(|| format!("invalid variant: {input}")) 38919625d8cSopenharmony_ci } 39019625d8cSopenharmony_ci 39119625d8cSopenharmony_ci /// The canonical argument value. 39219625d8cSopenharmony_ci /// 39319625d8cSopenharmony_ci /// The value is `None` for skipped variants. 39419625d8cSopenharmony_ci fn to_possible_value(&self) -> Option<PossibleValue>; 39519625d8cSopenharmony_ci} 39619625d8cSopenharmony_ci 39719625d8cSopenharmony_ciimpl<T: Parser> Parser for Box<T> { 39819625d8cSopenharmony_ci fn parse() -> Self { 39919625d8cSopenharmony_ci Box::new(<T as Parser>::parse()) 40019625d8cSopenharmony_ci } 40119625d8cSopenharmony_ci 40219625d8cSopenharmony_ci fn try_parse() -> Result<Self, Error> { 40319625d8cSopenharmony_ci <T as Parser>::try_parse().map(Box::new) 40419625d8cSopenharmony_ci } 40519625d8cSopenharmony_ci 40619625d8cSopenharmony_ci fn parse_from<I, It>(itr: I) -> Self 40719625d8cSopenharmony_ci where 40819625d8cSopenharmony_ci I: IntoIterator<Item = It>, 40919625d8cSopenharmony_ci It: Into<OsString> + Clone, 41019625d8cSopenharmony_ci { 41119625d8cSopenharmony_ci Box::new(<T as Parser>::parse_from(itr)) 41219625d8cSopenharmony_ci } 41319625d8cSopenharmony_ci 41419625d8cSopenharmony_ci fn try_parse_from<I, It>(itr: I) -> Result<Self, Error> 41519625d8cSopenharmony_ci where 41619625d8cSopenharmony_ci I: IntoIterator<Item = It>, 41719625d8cSopenharmony_ci It: Into<OsString> + Clone, 41819625d8cSopenharmony_ci { 41919625d8cSopenharmony_ci <T as Parser>::try_parse_from(itr).map(Box::new) 42019625d8cSopenharmony_ci } 42119625d8cSopenharmony_ci} 42219625d8cSopenharmony_ci 42319625d8cSopenharmony_ciimpl<T: CommandFactory> CommandFactory for Box<T> { 42419625d8cSopenharmony_ci fn command<'help>() -> Command { 42519625d8cSopenharmony_ci <T as CommandFactory>::command() 42619625d8cSopenharmony_ci } 42719625d8cSopenharmony_ci fn command_for_update<'help>() -> Command { 42819625d8cSopenharmony_ci <T as CommandFactory>::command_for_update() 42919625d8cSopenharmony_ci } 43019625d8cSopenharmony_ci} 43119625d8cSopenharmony_ci 43219625d8cSopenharmony_ciimpl<T: FromArgMatches> FromArgMatches for Box<T> { 43319625d8cSopenharmony_ci fn from_arg_matches(matches: &ArgMatches) -> Result<Self, Error> { 43419625d8cSopenharmony_ci <T as FromArgMatches>::from_arg_matches(matches).map(Box::new) 43519625d8cSopenharmony_ci } 43619625d8cSopenharmony_ci fn from_arg_matches_mut(matches: &mut ArgMatches) -> Result<Self, Error> { 43719625d8cSopenharmony_ci <T as FromArgMatches>::from_arg_matches_mut(matches).map(Box::new) 43819625d8cSopenharmony_ci } 43919625d8cSopenharmony_ci fn update_from_arg_matches(&mut self, matches: &ArgMatches) -> Result<(), Error> { 44019625d8cSopenharmony_ci <T as FromArgMatches>::update_from_arg_matches(self, matches) 44119625d8cSopenharmony_ci } 44219625d8cSopenharmony_ci fn update_from_arg_matches_mut(&mut self, matches: &mut ArgMatches) -> Result<(), Error> { 44319625d8cSopenharmony_ci <T as FromArgMatches>::update_from_arg_matches_mut(self, matches) 44419625d8cSopenharmony_ci } 44519625d8cSopenharmony_ci} 44619625d8cSopenharmony_ci 44719625d8cSopenharmony_ciimpl<T: Args> Args for Box<T> { 44819625d8cSopenharmony_ci fn augment_args(cmd: Command) -> Command { 44919625d8cSopenharmony_ci <T as Args>::augment_args(cmd) 45019625d8cSopenharmony_ci } 45119625d8cSopenharmony_ci fn augment_args_for_update(cmd: Command) -> Command { 45219625d8cSopenharmony_ci <T as Args>::augment_args_for_update(cmd) 45319625d8cSopenharmony_ci } 45419625d8cSopenharmony_ci} 45519625d8cSopenharmony_ci 45619625d8cSopenharmony_ciimpl<T: Subcommand> Subcommand for Box<T> { 45719625d8cSopenharmony_ci fn augment_subcommands(cmd: Command) -> Command { 45819625d8cSopenharmony_ci <T as Subcommand>::augment_subcommands(cmd) 45919625d8cSopenharmony_ci } 46019625d8cSopenharmony_ci fn augment_subcommands_for_update(cmd: Command) -> Command { 46119625d8cSopenharmony_ci <T as Subcommand>::augment_subcommands_for_update(cmd) 46219625d8cSopenharmony_ci } 46319625d8cSopenharmony_ci fn has_subcommand(name: &str) -> bool { 46419625d8cSopenharmony_ci <T as Subcommand>::has_subcommand(name) 46519625d8cSopenharmony_ci } 46619625d8cSopenharmony_ci} 46719625d8cSopenharmony_ci 46819625d8cSopenharmony_cifn format_error<I: CommandFactory>(err: crate::Error) -> crate::Error { 46919625d8cSopenharmony_ci let mut cmd = I::command(); 47019625d8cSopenharmony_ci err.format(&mut cmd) 47119625d8cSopenharmony_ci} 472