I added a --times N flag that prints the greeting N times, one per line. It works together with --shout. cargo test passes, and cargo run -- --times 3 Ada prints Hello, Ada! three times.
- Parsing:
Optionshas a newtimes: usizefield, defaulting to 1.--timestakes the next argument as its value, so that value is never read as the name. - Errors: A missing or non-numeric value makes
parse_argsreturn anErr.mainprintserror: ...to stderr and exits with status 2. - API change:
parse_argsnow returnsResult<Options, String>, so I added.unwrap()to the existing parsing tests. - Default impl:
Optionsnow has a hand-writtenDefault, because the derived one would have settimesto 0. - Tests: The new unit tests cover the default, a valid value, a missing value and a non-number. The integration tests run
--times 3 Adaand--times 2 --shout Ada. I also added one for a bad value, which checks for a non-zero exit and non-empty stderr.
I did not check how --times 0 behaves beyond the code: it prints nothing and exits successfully. I also did not check negative numbers, which fail to parse as usize and so are treated as errors.