clap/examples/11_only_specific_values.md
Ed Page bfa02fd418 test: More thoroughly test examples
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"
2021-11-23 13:13:41 -06:00

1.2 KiB

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"

$ 11_only_specific_values fast
Hare
$ 11_only_specific_values slow
Tortoise

Anything else will error, guiding the user to a valid value:

$ 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:

$ 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