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: Clarify value parser #4244

Merged
merged 2 commits into from Sep 21, 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
5 changes: 0 additions & 5 deletions Cargo.toml
Expand Up @@ -350,11 +350,6 @@ path = "examples/tutorial_derive/05_01_assert.rs"
required-features = ["derive"]
test = true

[[example]]
name = "custom-bool"
path = "examples/derive_ref/custom-bool.rs"
required-features = ["derive"]

[[example]]
name = "interop_augment_args"
path = "examples/derive_ref/augment_args.rs"
Expand Down
44 changes: 0 additions & 44 deletions examples/derive_ref/custom-bool.md

This file was deleted.

32 changes: 0 additions & 32 deletions examples/derive_ref/custom-bool.rs

This file was deleted.

7 changes: 6 additions & 1 deletion src/_derive/_tutorial.rs
Expand Up @@ -154,9 +154,12 @@
//!
//! ## Validation
//!
//! An appropriate default parser/validator will be selected for the field's type. See
//! [`value_parser!][crate::value_parser!] for more details.
//!
//! ### Enumerated values
//!
//! If you have arguments of specific values you want to test for, you can derive
//! For example, if you have arguments of specific values you want to test for, you can derive
//! [`ValueEnum`][crate::ValueEnum].
//!
//! This allows you specify the valid values for that argument. If the user does not use one of
Expand Down Expand Up @@ -184,6 +187,8 @@
//! ```
#![doc = include_str!("../../examples/tutorial_derive/04_02_validate.md")]
//!
//! See [`Arg::value_parser`][crate::Arg::value_parser] for more details.
//!
//! ### Argument Relations
//!
//! You can declare dependencies or conflicts between [`Arg`][crate::Arg]s or even
Expand Down
23 changes: 12 additions & 11 deletions src/_derive/mod.rs
Expand Up @@ -186,7 +186,6 @@
//! - `value_parser [= <expr>]`: [`Arg::value_parser`][crate::Arg::value_parser]
//! - When not present: will auto-select an implementation based on the field type using
//! [`value_parser!`][crate::value_parser!]
//! - To register a custom type's [`ValueParser`][crate::builder::ValueParser], implement [`ValueParserFactory`][crate::builder::ValueParserFactory]
//! - `action [= <expr>]`: [`Arg::action`][crate::Arg::action]
//! - When not present: will auto-select an action based on the field type
//! - `help = <expr>`: [`Arg::help`][crate::Arg::help]
Expand Down Expand Up @@ -254,20 +253,22 @@
//!
//! `clap` assumes some intent based on the type used:
//!
//! | Type | Effect | Implies |
//! |---------------------|--------------------------------------|--------------------------------------------------------------------------|
//! | `bool` | flag | `.action(ArgAction::SetTrue) |
//! | `Option<T>` | optional argument | `.action(ArgAction::Set).required(false)` |
//! | `Option<Option<T>>` | optional value for optional argument | `.action(ArgAction::Set).required(false).min_values(0).max_values(1)` |
//! | `T` | required argument | `.action(ArgAction::Set).required(!has_default)` |
//! | `Vec<T>` | `0..` occurrences of argument | `.action(ArgAction::Append).required(false).multiple_occurrences(true)` |
//! | `Option<Vec<T>>` | `0..` occurrences of argument | `.action(ArgAction::Append).required(false).multiple_occurrences(true)` |
//! | Type | Effect | Implies |
//! |---------------------|--------------------------------------|-------------------------------------------------------------|
//! | `bool` | flag | `.action(ArgAction::SetTrue) |
//! | `Option<T>` | optional argument | `.action(ArgAction::Set).required(false)` |
//! | `Option<Option<T>>` | optional value for optional argument | `.action(ArgAction::Set).required(false).num_args(0..=1)` |
//! | `T` | required argument | `.action(ArgAction::Set).required(!has_default)` |
//! | `Vec<T>` | `0..` occurrences of argument | `.action(ArgAction::Append).required(false).num_args(1..)` |
//! | `Option<Vec<T>>` | `0..` occurrences of argument | `.action(ArgAction::Append).required(false).num_args(1..)` |
//!
//! In addition, [`.value_parser(value_parser!(T))`][crate::value_parser!] is called for each
//! field.
//!
//! Notes:
//! - For custom type behavior, you can override the implied attributes/settings and/or set additional ones
//! - For example, see [custom-bool](./custom-bool.md)
//! - `Option<Vec<T>>` will be `None` instead of `vec![]` if no arguments are provided.
//! - This gives the user some flexibility in designing their argument, like with `min_values(0)`
//! - This gives the user some flexibility in designing their argument, like with `num_args(0..)`
//!
//! ## Doc Comments
//!
Expand Down
4 changes: 4 additions & 0 deletions src/_tutorial.rs
Expand Up @@ -143,6 +143,8 @@
//!
//! ## Validation
//!
//! By default, arguments are assumed to be `String`s and only UTF-8 validation is performed.
//!
//! ### Enumerated values
//!
//! If you have arguments of specific values you want to test for, you can use the
Expand Down Expand Up @@ -183,6 +185,8 @@
//! ```
#![doc = include_str!("../examples/tutorial_builder/04_02_validate.md")]
//!
//! See [`Arg::value_parser`][crate::Arg::value_parser] for more details.
//!
//! ### Argument Relations
//!
//! You can declare dependencies or conflicts between [`Arg`][crate::Arg]s or even
Expand Down
21 changes: 11 additions & 10 deletions src/builder/arg.rs
Expand Up @@ -867,21 +867,22 @@ impl Arg {
self
}

/// Specify the type of the argument.
/// Specify the typed behavior of the argument.
///
/// This allows parsing and validating a value before storing it into
/// [`ArgMatches`][crate::ArgMatches].
/// [`ArgMatches`][crate::ArgMatches] as the given type.
///
/// Possible value parsers include:
/// - [`value_parser!(T)`][crate::value_parser!] for auto-selecting a value parser for a given type
/// - Or [range expressions like `0..=1`][std::ops::RangeBounds] as a shorthand for [`RangedI64ValueParser`][crate::builder::RangedI64ValueParser]
/// - `Fn(&str) -> Result<T, E>`
/// - `[&str]` and [`PossibleValuesParser`][crate::builder::PossibleValuesParser] for static enumerated values
/// - [`BoolishValueParser`][crate::builder::BoolishValueParser], and [`FalseyValueParser`][crate::builder::FalseyValueParser] for alternative `bool` implementations
/// - [`NonEmptyStringValueParser`][crate::builder::NonEmptyStringValueParser] for basic validation for strings
/// - or any other [`TypedValueParser`][crate::builder::TypedValueParser] implementation
///
/// The default value is [`ValueParser::string`][crate::builder::ValueParser::string].
///
/// See also
/// - [`value_parser!`][crate::value_parser!] for auto-selecting a value parser for a given type
/// - [`BoolishValueParser`][crate::builder::BoolishValueParser], and [`FalseyValueParser`][crate::builder::FalseyValueParser] for alternative `bool` implementations
/// - [`NonEmptyStringValueParser`][crate::builder::NonEmptyStringValueParser] for basic validation for strings
/// - [`RangedI64ValueParser`][crate::builder::RangedI64ValueParser] and [`RangedU64ValueParser`][crate::builder::RangedU64ValueParser] for numeric ranges
/// - [`EnumValueParser`][crate::builder::EnumValueParser] and [`PossibleValuesParser`][crate::builder::PossibleValuesParser] for static enumerated values
/// - or any other [`TypedValueParser`][crate::builder::TypedValueParser] implementation
///
/// ```rust
/// # use clap::ArgAction;
/// let mut cmd = clap::Command::new("raw")
Expand Down
17 changes: 11 additions & 6 deletions src/builder/value_parser.rs
Expand Up @@ -73,9 +73,8 @@ enum ValueParserInner {
impl ValueParser {
/// Custom parser for argument values
///
/// To create a custom parser, see [`TypedValueParser`]
///
/// Pre-existing implementations include:
/// Pre-existing [`TypedValueParser`] implementations include:
/// - `Fn(&str) -> Result<T, E>`
/// - [`EnumValueParser`] and [`PossibleValuesParser`] for static enumerated values
/// - [`BoolishValueParser`] and [`FalseyValueParser`] for alternative `bool` implementations
/// - [`RangedI64ValueParser`] and [`RangedU64ValueParser`]
Expand Down Expand Up @@ -989,7 +988,8 @@ impl<E: crate::ValueEnum + Clone + Send + Sync + 'static> Default for EnumValueP
/// Verify the value is from an enumerated set of [`PossibleValue`][crate::builder::PossibleValue].
///
/// See also:
/// - [`EnumValueParser`]
/// - [`EnumValueParser`] for directly supporting `enum`s
/// - [`TypedValueParser::map`] for adapting values to a more specialized type
///
/// # Example
///
Expand Down Expand Up @@ -2083,7 +2083,12 @@ pub mod via_prelude {

/// Select a [`ValueParser`] implementation from the intended type
///
/// To register a custom type with this macro, implement [`ValueParserFactory`].
/// Supported types
/// - [`ValueParserFactory` types][ValueParserFactory], including
/// - [Native types][ValueParser]: `bool`, `String`, `OsString`, `PathBuf`
/// - [Ranged numeric types][RangedI64ValueParser]: `u8`, `i8`, `u16`, `i16, `u32`, `i32`, `u64`, `i64`
/// - [`ValueEnum` types][crate::ValueEnum]
/// - [`FromStr` types][std::str::FromStr], including usize, isize
///
/// # Example
///
Expand All @@ -2104,7 +2109,7 @@ pub mod via_prelude {
/// assert_eq!(port, Path::new("file.txt"));
/// ```
///
/// Supported types:
/// Example mappings:
/// ```rust
/// // Built-in types
/// let parser = clap::value_parser!(String);
Expand Down