Add a --shout flag that upper-cases the greeting #25
Approved by g1t-agent
Note
Running on Claude Sonnet 5.5.
Prompt
You are a coding agent working in the git repository checked out in the current directory.
Issue #20: Add a --shout flag that upper-cases the greeting
Add
pub shout: booltoOptionsinsrc/lib.rs, set by--shoutinparse_args. Insrc/main.rs, when set, print the greeting upper-cased (HELLO, ADA!). Add unit tests insrc/lib.rsfor parsing the flag (in any position relative to the name) and for the upper-casing behaviour; put the upper-casing in a small pub function so it is testable.These commands must pass when you are done. Run them if the tools are installed:
- cargo test
- cargo run -- --shout Ada
Make the change and keep it focused on the issue. Commit your work with a clear message. Do not push; that is done for you. Finish with a short summary of what you changed and why. It becomes the description of your pull request, so write it for a reviewer: plain sentences, no headings, no emoji, no checklists, and nothing about whether anything was committed or pushed. Say what you did not verify.
Read/work/repo/src/lib.rs
/work/repo/src/lib.rsRead/work/repo/src/main.rs
/work/repo/src/main.rsresult→ 1 use std::env;
1 use std::env; 2 3 use hello::{greeting, parse_args}; 4 5 fn main() { 6 // The name given, or whoever is signed in. 7 let name = parse_args(env::args().skip(1)) 8 .name 9 .or_else(|| env::var("USER").ok()) 10 .unwrap_or_else(|| "world".to_owned()); 11 println!("{}", greeting(&name)); 12 } 13result→ 1 //! Greetings.
1 //! Greetings. 2 3 /// Parsed command-line options. 4 #[derive(Debug, Default, PartialEq, Eq)] 5 pub struct Options { 6 /// The name to greet, if one was given. 7 pub name: Option<String>, 8 } 9 10 /// Parses command-line arguments (excluding the program name). 11 /// 12 /// Arguments starting with `--` are treated as flags and skipped; the name is 13 /// the first remaining argument. 14 pub fn parse_args<I: IntoIterator<Item = String>>(args: I) -> Options { 15 let name = args.into_iter().find(|arg| !arg.starts_with("--")); 16 Options { name } 17 } 18 19 /// The greeting for `name`. 20 pub fn greeting(name: &str) -> String { 21 let trimmed = name.trim(); 22 let display_name = if trimmed.is_empty() { "world" } else { trimmed }; 23 format!("Hello, {display_name}!") 24 } 25 26 /// The farewell for `name`. 27 pub fn farewell(name: &str) -> String { 28 let trimmed = name.trim(); 29 let display_name = if trimmed.is_empty() { "world" } else { trimmed }; 30 format!("Goodbye, {display_name}!") 31 } 32 33 #[cfg(test)] 34 mod tests { 35 use super::*; 36 37 fn args(list: &[&str]) -> Vec<String> { 38 list.iter().map(|s| s.to_string()).collect() 39 } 40 41 #[test] 42 fn parse_args_no_args() { 43 assert_eq!(parse_args(args(&[])).name, None); 44 } 45 46 #[test] 47 fn parse_args_name() { 48 assert_eq!(parse_args(args(&["Ada"])).name.as_deref(), Some("Ada")); 49 } 50 51 #[test] 52 fn parse_args_skips_leading_flag() { 53 assert_eq!( 54 parse_args(args(&["--unknown", "Ada"])).name.as_deref(), 55 Some("Ada") 56 ); 57 } 58 59 #[test] 60 fn greets_by_name() { 61 assert_eq!(greeting("Ada"), "Hello, Ada!"); 62 } 63 64 #[test] 65 fn greets_world_with_empty_string() { 66 assert_eq!(greeting(""), "Hello, world!"); 67 } 68 69 #[test] 70 fn greets_world_with_whitespace_only() { 71 assert_eq!(greeting(" "), "Hello, world!"); 72 } 73 74 #[test] 75 fn trims_whitespace_from_name() { 76 assert_eq!(greeting(" Ada "), "Hello, Ada!"); 77 } 78 79 #[test] 80 fn says_goodbye_by_name() { 81 assert_eq!(farewell("Ada"), "Goodbye, Ada!"); 82 } 83 84 #[test] 85 fn says_goodbye_to_world_when_blank() { 86 assert_eq!(farewell(" "), "Goodbye, world!"); 87 } 88 89 #[test] 90 fn farewell_trims_whitespace() { 91 assert_eq!(farewell(" Ada "), "Goodbye, Ada!"); 92 } 93 } 94Edit/work/repo/src/lib.rs
/work/repo/src/lib.rsresult→ The file /work/repo/src/lib.rs has been updated successfully. (file state is current in your context — no need to Read it back)
The file /work/repo/src/lib.rs has been updated successfully. (file state is current in your context — no need to Read it back)Edit/work/repo/src/lib.rs
/work/repo/src/lib.rsresult→ The file /work/repo/src/lib.rs has been updated successfully. (file state is current in your context — no need to Read it back)
The file /work/repo/src/lib.rs has been updated successfully. (file state is current in your context — no need to Read it back)Edit/work/repo/src/lib.rs
/work/repo/src/lib.rsresult→ The file /work/repo/src/lib.rs has been updated successfully. (file state is current in your context — no need to Read it back)
The file /work/repo/src/lib.rs has been updated successfully. (file state is current in your context — no need to Read it back)Edit/work/repo/src/main.rs
/work/repo/src/main.rsresult→ The file /work/repo/src/main.rs has been updated successfully. (file state is current in your context — no need to Read it back)
The file /work/repo/src/main.rs has been updated successfully. (file state is current in your context — no need to Read it back)Bashcargo test 2>&1 | tail -8 && cargo run -q -- --shout Ada
cargo test 2>&1 | tail -8 && cargo run -q -- --shout Adaresult→ test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s Doc-tests hello running 0 tests test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s HELLO, ADA!Bashcargo test 2>&1 | grep "test result" | head -1; git commit -qam "Add --shout flag that upper-cases the greeting" && git log --oneline | head -1
cargo test 2>&1 | grep "test result" | head -1; git commit -qam "Add --shout flag that upper-cases the greeting" && git log --oneline | head -1result→ test result: ok. 15 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
test result: ok. 15 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s d48f3a7 Add --shout flag that upper-cases the greetingg1t-agent
I added a
--shoutflag that upper-cases the greeting.cargo testpasses (15 tests), andcargo run -- --shout AdaprintsHELLO, ADA!.Optionsinsrc/lib.rsnow haspub shout: bool.parse_argssets it when it sees--shout, whether the flag comes before or after the name. It still skips other--flags and takes the first non-flag argument as the name. I rewroteparse_argsas a loop over the arguments to handle both, instead of the singlefindit used before.A new
pub fn shout(text: &str) -> Stringdoes the upper-casing, so it can be tested directly.src/main.rscalls it on the greeting when the flag is set.The new unit tests cover:
shoutdefaulting to false.--shoutbefore the name, after the name, and with no name.- The upper-casing itself.
I didn't test
--shoutwith no name, where the name falls back to$USER, or the printed output of the non-shout path beyond the existing tests.Note
This run cost $0.0694 over 9 turns.
Note
Pushed d48f3a772944.