chapter_0.rs |
## Quick Start
You can create an application declaratively with a `struct` and some
attributes.
First, ensure `clap` is available with the [`derive` feature flag][crate::_features]:
```console
$ cargo add clap --features derive
```
```rust |
792 |
chapter_1.rs |
## Configuring the Parser
You use derive [`Parser`][crate::Parser] to start building a parser.
```rust |
1189 |
chapter_2.rs |
## Adding Arguments
1. [Positionals](#positionals)
2. [Options](#options)
3. [Flags](#flags)
4. [Subcommands](#subcommands)
5. [Defaults](#defaults)
Arguments are inferred from the fields of your struct.
### Positionals
You can have users specify values by their position on the command-line:
```rust |
4006 |
chapter_3.rs |
## Validation
1. [Enumerated values](#enumerated-values)
2. [Validated values](#validated-values)
3. [Argument Relations](#argument-relations)
4. [Custom Validation](#custom-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
For example, if you have arguments of specific values you want to test for, you can derive
[`ValueEnum`][super#valueenum-attributes]
(any [`PossibleValue`] builder function can be used as a `#[value]` attribute on enum variants).
This allows you specify the valid values for that argument. If the user does not use one of
those specific values, they will receive a graceful exit with error message informing them
of the mistake, and what the possible valid values are
```rust |
3235 |
chapter_4.rs |
## Testing
clap reports most development errors as `debug_assert!`s. Rather than checking every
subcommand, you should have a test that calls
[`Command::debug_assert`][crate::Command::debug_assert]:
```rust,no_run |
502 |
chapter_5.rs |
## Next Steps
- [Cookbook][crate::_cookbook] for application-focused examples
- Explore more features in the [Derive reference][super]
- See also [`Command`], [`Arg`], [`ArgGroup`], and [`PossibleValue`] builder functions which
can be used as attributes
For support, see [Discussions](https://github.com/clap-rs/clap/discussions) |
513 |
mod.rs |
# Documentation: Derive Tutorial
1. [Quick Start][chapter_0]
2. [Configuring the Parser][chapter_1]
3. [Adding Arguments][chapter_2]
1. [Positionals][chapter_2#positionals]
2. [Options][chapter_2#options]
3. [Flags][chapter_2#flags]
4. [Subcommands][chapter_2#subcommands]
5. [Defaults][chapter_2#defaults]
4. [Validation][chapter_3]
1. [Enumerated values][chapter_3#enumerated-values]
2. [Validated values][chapter_3#validated-values]
3. [Argument Relations][chapter_3#argument-relations]
4. [Custom Validation][chapter_3#custom-validation]
5. [Testing][chapter_4]
6. [Next Steps][chapter_5] |
1194 |