mirror of
https://github.com/clap-rs/clap
synced 2024-11-10 23:04:23 +00:00
docs: Provide a hybrid-flag example
Between - `ArgAction::SetTrue` storing actual values - `ArgAction::Set` making it easier for derive users to override bool behavior - `Arg::default_missing_value` allowing hybrid-flags - This commit documenting hybrid-flags even further There shouldn't be anything left for #1649 Fixes #1649
This commit is contained in:
parent
c9988db97f
commit
40daa70b89
2 changed files with 42 additions and 3 deletions
|
@ -92,7 +92,8 @@ pub enum ArgAction {
|
|||
///
|
||||
/// If no [`default_value`][super::Arg::default_value] is set, it will be `false`.
|
||||
///
|
||||
/// No value is allowed
|
||||
/// No value is allowed. To optionally accept a value, see
|
||||
/// [`Arg::default_missing_value`][super::Arg::default_missing_value]
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
|
@ -127,7 +128,8 @@ pub enum ArgAction {
|
|||
///
|
||||
/// If no [`default_value`][super::Arg::default_value] is set, it will be `true`.
|
||||
///
|
||||
/// No value is allowed
|
||||
/// No value is allowed. To optionally accept a value, see
|
||||
/// [`Arg::default_missing_value`][super::Arg::default_missing_value]
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
|
@ -162,7 +164,8 @@ pub enum ArgAction {
|
|||
///
|
||||
/// If no [`default_value`][super::Arg::default_value] is set, it will be `0`.
|
||||
///
|
||||
/// No value is allowed
|
||||
/// No value is allowed. To optionally accept a value, see
|
||||
/// [`Arg::default_missing_value`][super::Arg::default_missing_value]
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
|
|
|
@ -2257,6 +2257,42 @@ impl<'help> Arg<'help> {
|
|||
/// assert_eq!(m.value_of("color"), Some("always"));
|
||||
/// assert_eq!(m.value_source("color"), Some(ValueSource::CommandLine));
|
||||
/// ```
|
||||
///
|
||||
/// For bool literals:
|
||||
/// ```rust
|
||||
/// # use clap::{Command, Arg, ValueSource, value_parser};
|
||||
/// fn cli() -> Command<'static> {
|
||||
/// Command::new("prog")
|
||||
/// .arg(Arg::new("create").long("create")
|
||||
/// .value_name("BOOL")
|
||||
/// .value_parser(value_parser!(bool))
|
||||
/// .min_values(0)
|
||||
/// .require_equals(true)
|
||||
/// .default_missing_value("true")
|
||||
/// )
|
||||
/// }
|
||||
///
|
||||
/// // first, we'll provide no arguments
|
||||
/// let m = cli().get_matches_from(vec![
|
||||
/// "prog"
|
||||
/// ]);
|
||||
/// assert_eq!(m.get_one::<bool>("create").copied(), None);
|
||||
///
|
||||
/// // next, we'll provide a runtime value to override the default (as usually done).
|
||||
/// let m = cli().get_matches_from(vec![
|
||||
/// "prog", "--create=false"
|
||||
/// ]);
|
||||
/// assert_eq!(m.get_one::<bool>("create").copied(), Some(false));
|
||||
/// assert_eq!(m.value_source("create"), Some(ValueSource::CommandLine));
|
||||
///
|
||||
/// // finally, we will use the shortcut and only provide the argument without a value.
|
||||
/// let m = cli().get_matches_from(vec![
|
||||
/// "prog", "--create"
|
||||
/// ]);
|
||||
/// assert_eq!(m.get_one::<bool>("create").copied(), Some(true));
|
||||
/// assert_eq!(m.value_source("create"), Some(ValueSource::CommandLine));
|
||||
/// ```
|
||||
///
|
||||
/// [`ArgMatches::value_of`]: ArgMatches::value_of()
|
||||
/// [`Arg::takes_value(true)`]: Arg::takes_value()
|
||||
/// [`Arg::default_value`]: Arg::default_value()
|
||||
|
|
Loading…
Reference in a new issue