Add a --whisper flag #83
Mergedg1tbot merged into maing1t-agenton g1t
Approved by g1t-agent
3 files+33−10/3 viewed
| 8 | 8 | | -------- | ----------- | | |
| 9 | 9 | | `NAME` | Optional. The name to greet. Only the first argument is read. Defaults to the `USER` environment variable, then to `world`. | | |
| 10 | 10 | | `--shout` | Upper-cases the greeting. | | |
| 11 | + | | `--whisper` | Lower-cases the greeting and ends it with `...` instead of `!`. With `--shout`, the last one given wins. | | |
| 11 | 12 | | `--version` | Prints the crate version and exits. | | |
| 12 | 13 | | `--times N` | Prints the greeting `N` times. Defaults to 1. | | |
| 13 | 14 | | `--lang LANG` | Greets in the given language. Supported values are `en`, `es` and `fr`. Defaults to `en`. | |
| 28 | 28 | pub name: Option<String>, | |
| 29 | 29 | /// Whether to upper-case the greeting (`--shout`). | |
| 30 | 30 | pub shout: bool, | |
| 31 | + | /// Whether to lower-case the greeting and end it with `...` (`--whisper`). | |
| 32 | + | pub whisper: bool, | |
| 31 | 33 | /// Whether to print the crate version and exit (`--version`). | |
| 32 | 34 | pub version: bool, | |
| 33 | 35 | /// How many times to print the greeting (`--times N`). | |
| 49 | 51 | Options { | |
| 50 | 52 | name: None, | |
| 51 | 53 | shout: false, | |
| 54 | + | whisper: false, | |
| 52 | 55 | version: false, | |
| 53 | 56 | times: 1, | |
| 54 | 57 | lang: Lang::En, | |
| 77 | 80 | while let Some(arg) = args.next() { | |
| 78 | 81 | if arg == "--shout" { | |
| 79 | 82 | options.shout = true; | |
| 83 | + | options.whisper = false; | |
| 84 | + | } else if arg == "--whisper" { | |
| 85 | + | options.whisper = true; | |
| 86 | + | options.shout = false; | |
| 80 | 87 | } else if arg == "--version" { | |
| 81 | 88 | options.version = true; | |
| 82 | 89 | } else if arg == "--quiet" { | |
| 112 | 119 | text.to_uppercase() | |
| 113 | 120 | } | |
| 114 | 121 | ||
| 122 | + | /// `text` lower-cased, with its final exclamation mark replaced by `...`. | |
| 123 | + | pub fn whisper(text: &str) -> String { | |
| 124 | + | let body = text.to_lowercase(); | |
g1t-agent Minor: for | |||
| 125 | + | format!("{}...", body.trim_end_matches('!').trim_end()) | |
| 126 | + | } | |
| 127 | + | ||
| 115 | 128 | /// `text` with its final exclamation mark replaced by three. | |
| 116 | 129 | pub fn loud(text: &str) -> String { | |
| 117 | 130 | format!("{}!!!", text.strip_suffix('!').unwrap_or(text)) | |
| 320 | 333 | } | |
| 321 | 334 | ||
| 322 | 335 | #[test] | |
| 336 | + | fn whisper_lower_cases_and_trails_off() { | |
| 337 | + | assert_eq!(whisper(&hail("Ada")), "hi, ada..."); | |
| 338 | + | assert_eq!(whisper(&greeting_in("Luc", Lang::Fr)), "bonjour, luc..."); | |
| 339 | + | } | |
| 340 | + | ||
| 341 | + | #[test] | |
| 342 | + | fn parse_args_whisper_and_shout_last_wins() { | |
| 343 | + | let o = parse_args(args(&["--whisper", "Ada"])).unwrap(); | |
| 344 | + | assert!(o.whisper && !o.shout); | |
| 345 | + | let o = parse_args(args(&["--shout", "--whisper"])).unwrap(); | |
| 346 | + | assert!(o.whisper && !o.shout); | |
| 347 | + | let o = parse_args(args(&["--whisper", "--shout"])).unwrap(); | |
| 348 | + | assert!(o.shout && !o.whisper); | |
| 349 | + | } | |
| 350 | + | ||
| 351 | + | #[test] | |
| 323 | 352 | fn greets_by_name() { | |
| 324 | 353 | assert_eq!(hail("Ada"), "Hi, Ada!"); | |
| 325 | 354 | } | |
| 1 | 1 | use std::{env, process}; | |
| 2 | 2 | ||
| 3 | − | use hello::{greeting_in, loud, parse_args, part, shout, wave}; | |
| 3 | + | use hello::{greeting_in, loud, parse_args, part, shout, wave, whisper}; | |
| 4 | 4 | ||
| 5 | 5 | fn main() { | |
| 6 | 6 | let options = match parse_args(env::args().skip(1)) { | |
| 30 | 30 | }; | |
| 31 | 31 | let line = if options.shout { | |
| 32 | 32 | shout(&message) | |
| 33 | + | } else if options.whisper { | |
| 34 | + | whisper(&message) | |
| 33 | 35 | } else { | |
| 34 | 36 | message | |
| 35 | 37 | }; |