Debian Codegraph @

third_party/rust/clap/src/_derive/_tutorial/chapter_3.rs

raw
1 //! ## Validation
2 //!
3 //! 1. [Enumerated values](#enumerated-values)
4 //! 2. [Validated values](#validated-values)
5 //! 3. [Argument Relations](#argument-relations)
6 //! 4. [Custom Validation](#custom-validation)
7 //!
8 //! An appropriate default parser/validator will be selected for the field's type. See
9 //! [`value_parser!`][crate::value_parser!] for more details.
10 //!
11 //! ### Enumerated values
12 //!
13 //! For example, if you have arguments of specific values you want to test for, you can derive
14 //! [`ValueEnum`][super#valueenum-attributes]
15 //! (any [`PossibleValue`] builder function can be used as a `#[value]` attribute on enum variants).
16 //!
17 //! This allows you specify the valid values for that argument. If the user does not use one of
18 //! those specific values, they will receive a graceful exit with error message informing them
19 //! of the mistake, and what the possible valid values are
20 //!
21 //! ```rust
22 #![doc = include_str!("../../../examples/tutorial_derive/04_01_enum.rs")]
23 //! ```
24 #![doc = include_str!("../../../examples/tutorial_derive/04_01_enum.md")]
25 //!
26 //! ### Validated values
27 //!
28 //! More generally, you can validate and parse into any data type with [`Arg::value_parser`].
29 //!
30 //! ```rust
31 #![doc = include_str!("../../../examples/tutorial_derive/04_02_parse.rs")]
32 //! ```
33 #![doc = include_str!("../../../examples/tutorial_derive/04_02_parse.md")]
34 //!
35 //! A [custom parser][TypedValueParser] can be used to improve the error messages or provide additional validation:
36 //!
37 //! ```rust
38 #![doc = include_str!("../../../examples/tutorial_derive/04_02_validate.rs")]
39 //! ```
40 #![doc = include_str!("../../../examples/tutorial_derive/04_02_validate.md")]
41 //!
42 //! See [`Arg::value_parser`][crate::Arg::value_parser] for more details.
43 //!
44 //! ### Argument Relations
45 //!
46 //! You can declare dependencies or conflicts between [`Arg`][crate::Arg]s or even
47 //! [`ArgGroup`][crate::ArgGroup]s.
48 //!
49 //! [`ArgGroup`][crate::ArgGroup]s make it easier to declare relations instead of having to list
50 //! each individually, or when you want a rule to apply "any but not all" arguments.
51 //!
52 //! Perhaps the most common use of [`ArgGroup`][crate::ArgGroup]s is to require one and *only* one
53 //! argument to be present out of a given set. Imagine that you had multiple arguments, and you
54 //! want one of them to be required, but making all of them required isn't feasible because perhaps
55 //! they conflict with each other.
56 //!
57 //! [`ArgGroup`][crate::ArgGroup]s are automatically created for a `struct` with its
58 //! [`ArgGroup::id`][crate::ArgGroup::id] being the struct's name.
59 //!
60 //! ```rust
61 #![doc = include_str!("../../../examples/tutorial_derive/04_03_relations.rs")]
62 //! ```
63 #![doc = include_str!("../../../examples/tutorial_derive/04_03_relations.md")]
64 //!
65 //! ### Custom Validation
66 //!
67 //! As a last resort, you can create custom errors with the basics of clap's formatting.
68 //!
69 //! ```rust
70 #![doc = include_str!("../../../examples/tutorial_derive/04_04_custom.rs")]
71 //! ```
72 #![doc = include_str!("../../../examples/tutorial_derive/04_04_custom.md")]
73 #![allow(unused_imports)]
74 use crate::builder::*;
75
76 pub use super::chapter_2 as previous;
77 pub use super::chapter_4 as next;
78 pub use crate::_derive::_tutorial as table_of_contents;