flagon-io/hello

public

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

Add a --reverse flag #85

Mergedg1tbot merged into maing1t-agenton g1t

Approved by g1t-agent

3 files+28−10/3 viewed
+1−0
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. |
1111 | `--whisper` | Lower-cases the greeting and ends it with `...` instead of `!`. With `--shout`, the last one given wins. |
12+| `--reverse` | Spells the name backwards. Applies to the default name too. |
1213 | `--version` | Prints the crate version and exits. |
1314 | `--times N` | Prints the greeting `N` times. Defaults to 1. |
1415 | `--lang LANG` | Greets in the given language. Supported values are `en`, `es` and `fr`. Defaults to `en`. |
+21−0
3030 pub shout: bool,
3131 /// Whether to lower-case the greeting and end it with `...` (`--whisper`).
3232 pub whisper: bool,
33+ /// Whether to spell the name backwards (`--reverse`).
34+ pub reverse: bool,
3335 /// Whether to print the crate version and exit (`--version`).
3436 pub version: bool,
3537 /// How many times to print the greeting (`--times N`).
5254 name: None,
5355 shout: false,
5456 whisper: false,
57+ reverse: false,
5558 version: false,
5659 times: 1,
5760 lang: Lang::En,
8487 } else if arg == "--whisper" {
8588 options.whisper = true;
8689 options.shout = false;
90+ } else if arg == "--reverse" {
91+ options.reverse = true;
8792 } else if arg == "--version" {
8893 options.version = true;
8994 } else if arg == "--quiet" {
114119 Ok(options)
115120 }
116121
122+/// `name` spelled backwards.
123+pub fn reverse(name: &str) -> String {
124+ name.chars().rev().collect()
125+}
126+
117127 /// `text` upper-cased.
118128 pub fn shout(text: &str) -> String {
119129 text.to_uppercase()
349359 }
350360
351361 #[test]
362+ fn parse_args_reverse() {
363+ assert!(parse_args(args(&["--reverse", "Ada"])).unwrap().reverse);
364+ assert!(!parse_args(args(&["Ada"])).unwrap().reverse);
365+ }
366+
367+ #[test]
368+ fn reverse_spells_backwards() {
369+ assert_eq!(hail(&reverse("Ada")), "Hi, adA!");
370+ }
371+
372+ #[test]
352373 fn greets_by_name() {
353374 assert_eq!(hail("Ada"), "Hi, Ada!");
354375 }
1+6−1
11 use std::{env, process};
22
3−use hello::{greeting_in, loud, parse_args, part, shout, wave, whisper};
3+use hello::{greeting_in, loud, parse_args, part, reverse, shout, wave, whisper};
44
55 fn main() {
66 let options = match parse_args(env::args().skip(1)) {
2222 .name
2323 .or_else(|| env::var("USER").ok())
2424 .unwrap_or_else(|| "world".to_owned());
25+ let name = if options.reverse {

g1t-agent

Because the name is reversed here, --bye also prints the reversed name. This is fine, but add a test in tests/cli.rs for --reverse (and --reverse --bye) so the behaviour is pinned down.

26+ reverse(&name)
27+ } else {
28+ name
29+ };
2530 let message = greeting_in(&name, options.lang);
2631 let message = if options.loud {
2732 loud(&message)