flagon-io/hello

public

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

Commit

Add --loud flag that ends the greeting with three exclamation marks

g1t agentand Claude Sonnet 5.5committed Parent81ed18bBrowse files
3 files+30−10/3 viewed
+21−0
3636 pub lang: Lang,
3737 /// Whether to print nothing (`--quiet`).
3838 pub quiet: bool,
39+ /// Whether to end the greeting with three exclamation marks (`--loud`).
40+ pub loud: bool,
3941 }
4042
4143 impl Default for Options {
4749 times: 1,
4850 lang: Lang::En,
4951 quiet: false,
52+ loud: false,
5053 }
5154 }
5255 }
7275 options.version = true;
7376 } else if arg == "--quiet" {
7477 options.quiet = true;
78+ } else if arg == "--loud" {
79+ options.loud = true;
7580 } else if arg == "--times" {
7681 let value = args
7782 .next()
98103 text.to_uppercase()
99104 }
100105
106+/// `text` with its final exclamation mark replaced by three.
107+pub fn loud(text: &str) -> String {
108+ format!("{}!!!", text.strip_suffix('!').unwrap_or(text))
109+}
110+
101111 /// The greeting for `name`.
102112 pub fn greeting(name: &str) -> String {
103113 greeting_in(name, Lang::En)
136146 }
137147
138148 #[test]
149+ fn parse_args_loud() {
150+ assert!(parse_args(args(&["--loud", "Ada"])).unwrap().loud);
151+ assert!(!parse_args(args(&["Ada"])).unwrap().loud);
152+ }
153+
154+ #[test]
155+ fn loud_uses_three_exclamation_marks() {
156+ assert_eq!(loud(&greeting("Ada")), "Hi, Ada!!!");
157+ }
158+
159+ #[test]
139160 fn parse_args_no_args() {
140161 assert_eq!(parse_args(args(&[])).unwrap().name, None);
141162 }
+2−1
11 use std::{env, process};
22
3−use hello::{greeting_in, parse_args, shout};
3+use hello::{greeting_in, loud, parse_args, shout};
44
55 fn main() {
66 let options = match parse_args(env::args().skip(1)) {
2323 .or_else(|| env::var("USER").ok())
2424 .unwrap_or_else(|| "world".to_owned());
2525 let message = greeting_in(&name, options.lang);
26+ let message = if options.loud { loud(&message) } else { message };
2627 let line = if options.shout { shout(&message) } else { message };
2728 for _ in 0..options.times {
2829 println!("{line}");
+7−0
9292 assert!(output.status.success());
9393 assert!(output.stdout.is_empty());
9494 }
95+
96+#[test]
97+fn loud_ends_with_three_exclamation_marks() {
98+ let output = run(&["--loud", "Ada"]);
99+ assert!(output.status.success());
100+ assert_eq!(String::from_utf8(output.stdout).unwrap(), "Hi, Ada!!!\n");
101+}