mirror of
https://github.com/clap-rs/clap
synced 2025-01-05 17:28:42 +00:00
bfa02fd418
This ports our example testing over to [trycmd](https://docs.rs/) so we can: - More thoroughly test our examples - Provide always-up-to-date example usage The old way of testing automatically picked up examples. This new way requires we have a `.md` file that uses the example in some way. Notes: - Moved overall example description to the `.md` file - I added cross-linking between related examples - `14_groups` had a redundant paragraph (twice talked about "one and only one"
48 lines
1.2 KiB
Markdown
48 lines
1.2 KiB
Markdown
If you have arguments of specific values you want to test for, you can use the
|
|
.possible_values() method of Arg
|
|
|
|
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
|
|
|
|
For this example, assume you want one positional argument of either "fast" or "slow"
|
|
i.e. the only possible ways to run the program are "myprog fast" or "myprog slow"
|
|
|
|
```bash
|
|
$ 11_only_specific_values fast
|
|
Hare
|
|
$ 11_only_specific_values slow
|
|
Tortoise
|
|
```
|
|
|
|
Anything else will error, guiding the user to a valid value:
|
|
```bash
|
|
$ 11_only_specific_values medium
|
|
? failed
|
|
error: "medium" isn't a valid value for '<MODE>'
|
|
[possible values: fast, slow]
|
|
|
|
USAGE:
|
|
11_only_specific_values[EXE] <MODE>
|
|
|
|
For more information try --help
|
|
```
|
|
|
|
Valid values also get shown in the help:
|
|
```bash
|
|
$ 11_only_specific_values --help
|
|
myapp
|
|
|
|
does awesome things
|
|
|
|
USAGE:
|
|
11_only_specific_values[EXE] <MODE>
|
|
|
|
ARGS:
|
|
<MODE> What mode to run the program in [possible values: fast, slow]
|
|
|
|
OPTIONS:
|
|
-h, --help Print help information
|
|
```
|
|
|
|
For integrating this with enums, see [13_enum_values](13_enum_values.md)
|