Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: Provide a hybrid-flag example #3837

Merged
merged 2 commits into from Jun 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/builder/action.rs
Expand Up @@ -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
///
Expand Down Expand Up @@ -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
///
Expand Down Expand Up @@ -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
///
Expand Down
82 changes: 51 additions & 31 deletions src/builder/arg.rs
Expand Up @@ -2220,59 +2220,79 @@ impl<'help> Arg<'help> {
///
/// # Examples
///
/// Here is an implementation of the common POSIX style `--color` argument.
///
/// For POSIX style `--color`:
/// ```rust
/// # use clap::{Command, Arg, ValueSource};
///
/// macro_rules! cmd {
/// () => {{
/// Command::new("prog")
/// .arg(Arg::new("color").long("color")
/// .value_name("WHEN")
/// .value_parser(["always", "auto", "never"])
/// .default_value("auto")
/// .overrides_with("color")
/// .min_values(0)
/// .require_equals(true)
/// .default_missing_value("always")
/// .help("Specify WHEN to colorize output.")
/// )
/// }};
/// fn cli() -> Command<'static> {
/// Command::new("prog")
/// .arg(Arg::new("color").long("color")
/// .value_name("WHEN")
/// .value_parser(["always", "auto", "never"])
/// .default_value("auto")
/// .min_values(0)
/// .require_equals(true)
/// .default_missing_value("always")
/// .help("Specify WHEN to colorize output.")
/// )
/// }
///
/// let mut m;
///
/// // first, we'll provide no arguments
///
/// m = cmd!().get_matches_from(vec![
/// let m = cli().get_matches_from(vec![
/// "prog"
/// ]);
///
/// assert_eq!(m.value_of("color"), Some("auto"));
/// assert!(m.contains_id("color"));
/// assert_eq!(m.value_source("color"), Some(ValueSource::DefaultValue));
///
/// // next, we'll provide a runtime value to override the default (as usually done).
///
/// m = cmd!().get_matches_from(vec![
/// let m = cli().get_matches_from(vec![
/// "prog", "--color=never"
/// ]);
///
/// assert_eq!(m.value_of("color"), Some("never"));
/// assert!(m.contains_id("color"));
/// assert_eq!(m.value_source("color"), Some(ValueSource::CommandLine));
///
/// // finally, we will use the shortcut and only provide the argument without a value.
///
/// m = cmd!().get_matches_from(vec![
/// let m = cli().get_matches_from(vec![
/// "prog", "--color"
/// ]);
///
/// assert_eq!(m.value_of("color"), Some("always"));
/// assert!(m.contains_id("color"));
/// 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()
Expand Down