flagon-io/hello

public

Greets people from the command line. The sample repository for g1t.

Add a --whisper flag #83

Mergedg1tbot merged into maing1t-agenton g1t

Approved by g1t-agent

3 files+33−10/3 viewed
+1−0
88 | -------- | ----------- |
99 | `NAME` | Optional. The name to greet. Only the first argument is read. Defaults to the `USER` environment variable, then to `world`. |
1010 | `--shout` | Upper-cases the greeting. |
11+| `--whisper` | Lower-cases the greeting and ends it with `...` instead of `!`. With `--shout`, the last one given wins. |
1112 | `--version` | Prints the crate version and exits. |
1213 | `--times N` | Prints the greeting `N` times. Defaults to 1. |
1314 | `--lang LANG` | Greets in the given language. Supported values are `en`, `es` and `fr`. Defaults to `en`. |
1+29−0
2828 pub name: Option<String>,
2929 /// Whether to upper-case the greeting (`--shout`).
3030 pub shout: bool,
31+ /// Whether to lower-case the greeting and end it with `...` (`--whisper`).
32+ pub whisper: bool,
3133 /// Whether to print the crate version and exit (`--version`).
3234 pub version: bool,
3335 /// How many times to print the greeting (`--times N`).
4951 Options {
5052 name: None,
5153 shout: false,
54+ whisper: false,
5255 version: false,
5356 times: 1,
5457 lang: Lang::En,
7780 while let Some(arg) = args.next() {
7881 if arg == "--shout" {
7982 options.shout = true;
83+ options.whisper = false;
84+ } else if arg == "--whisper" {
85+ options.whisper = true;
86+ options.shout = false;
8087 } else if arg == "--version" {
8188 options.version = true;
8289 } else if arg == "--quiet" {
112119 text.to_uppercase()
113120 }
114121
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 --lang es this yields ¡hola, ada... with a dangling leading ¡. Consider stripping it, or accept it and add a test documenting the behaviour.

125+ format!("{}...", body.trim_end_matches('!').trim_end())
126+}
127+
115128 /// `text` with its final exclamation mark replaced by three.
116129 pub fn loud(text: &str) -> String {
117130 format!("{}!!!", text.strip_suffix('!').unwrap_or(text))
320333 }
321334
322335 #[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]
323352 fn greets_by_name() {
324353 assert_eq!(hail("Ada"), "Hi, Ada!");
325354 }
+3−1
11 use std::{env, process};
22
3−use hello::{greeting_in, loud, parse_args, part, shout, wave};
3+use hello::{greeting_in, loud, parse_args, part, shout, wave, whisper};
44
55 fn main() {
66 let options = match parse_args(env::args().skip(1)) {
3030 };
3131 let line = if options.shout {
3232 shout(&message)
33+ } else if options.whisper {
34+ whisper(&message)
3335 } else {
3436 message
3537 };