Pick any line to see why it is the way it is: the commit, the pull request and issue it came from, and what the agent was thinking.
| Greet people from the command line | 1 | //! Greetings. |
| 2 | ||
| Extract argument parsing into parse_args in lib.rs | 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>, | |
| Add --shout flag that upper-cases the greeting | 8 | /// Whether to upper-case the greeting (`--shout`). |
| 9 | pub shout: bool, | |
| Add --version flag that prints the crate version and exits | 10 | /// Whether to print the crate version and exit (`--version`). |
| 11 | pub version: bool, | |
| Extract argument parsing into parse_args in lib.rs | 12 | } |
| 13 | ||
| 14 | /// Parses command-line arguments (excluding the program name). | |
| 15 | /// | |
| Add --version flag that prints the crate version and exits | 16 | /// `--shout` sets [`Options::shout`] and `--version` sets |
| 17 | /// [`Options::version`]; other arguments starting with `--` are | |
| Add --shout flag that upper-cases the greeting | 18 | /// treated as flags and skipped. The name is the first remaining argument. |
| Extract argument parsing into parse_args in lib.rs | 19 | pub fn parse_args<I: IntoIterator<Item = String>>(args: I) -> Options { |
| Add --shout flag that upper-cases the greeting | 20 | let mut options = Options::default(); |
| 21 | for arg in args { | |
| 22 | if arg == "--shout" { | |
| 23 | options.shout = true; | |
| Add --version flag that prints the crate version and exits | 24 | } else if arg == "--version" { |
| 25 | options.version = true; | |
| Add --shout flag that upper-cases the greeting | 26 | } else if !arg.starts_with("--") && options.name.is_none() { |
| 27 | options.name = Some(arg); | |
| 28 | } | |
| 29 | } | |
| 30 | options | |
| Extract argument parsing into parse_args in lib.rs | 31 | } |
| 32 | ||
| Add --shout flag that upper-cases the greeting | 33 | /// `text` upper-cased. |
| 34 | pub fn shout(text: &str) -> String { | |
| 35 | text.to_uppercase() | |
| 36 | } | |
| 37 | ||
| Greet people from the command line | 38 | /// The greeting for `name`. |
| 39 | pub fn greeting(name: &str) -> String { | |
| Handle empty and whitespace-only names, trim whitespace from input | 40 | let trimmed = name.trim(); |
| 41 | let display_name = if trimmed.is_empty() { "world" } else { trimmed }; | |
| 42 | format!("Hello, {display_name}!") | |
| Greet people from the command line | 43 | } |
| 44 | ||
| Add farewell function alongside greeting | 45 | /// The farewell for `name`. |
| 46 | pub fn farewell(name: &str) -> String { | |
| 47 | let trimmed = name.trim(); | |
| 48 | let display_name = if trimmed.is_empty() { "world" } else { trimmed }; | |
| 49 | format!("Goodbye, {display_name}!") | |
| 50 | } | |
| 51 | ||
| Greet people from the command line | 52 | #[cfg(test)] |
| 53 | mod tests { | |
| 54 | use super::*; | |
| 55 | ||
| Extract argument parsing into parse_args in lib.rs | 56 | fn args(list: &[&str]) -> Vec<String> { |
| 57 | list.iter().map(|s| s.to_string()).collect() | |
| 58 | } | |
| 59 | ||
| 60 | #[test] | |
| 61 | fn parse_args_no_args() { | |
| 62 | assert_eq!(parse_args(args(&[])).name, None); | |
| 63 | } | |
| 64 | ||
| 65 | #[test] | |
| 66 | fn parse_args_name() { | |
| 67 | assert_eq!(parse_args(args(&["Ada"])).name.as_deref(), Some("Ada")); | |
| 68 | } | |
| 69 | ||
| 70 | #[test] | |
| 71 | fn parse_args_skips_leading_flag() { | |
| 72 | assert_eq!( | |
| 73 | parse_args(args(&["--unknown", "Ada"])).name.as_deref(), | |
| 74 | Some("Ada") | |
| 75 | ); | |
| 76 | } | |
| 77 | ||
| Greet people from the command line | 78 | #[test] |
| Add --shout flag that upper-cases the greeting | 79 | fn parse_args_shout_defaults_off() { |
| 80 | assert!(!parse_args(args(&["Ada"])).shout); | |
| 81 | } | |
| 82 | ||
| 83 | #[test] | |
| 84 | fn parse_args_shout_before_name() { | |
| 85 | let options = parse_args(args(&["--shout", "Ada"])); | |
| 86 | assert!(options.shout); | |
| 87 | assert_eq!(options.name.as_deref(), Some("Ada")); | |
| 88 | } | |
| 89 | ||
| 90 | #[test] | |
| 91 | fn parse_args_shout_after_name() { | |
| 92 | let options = parse_args(args(&["Ada", "--shout"])); | |
| 93 | assert!(options.shout); | |
| 94 | assert_eq!(options.name.as_deref(), Some("Ada")); | |
| 95 | } | |
| 96 | ||
| 97 | #[test] | |
| 98 | fn parse_args_shout_alone() { | |
| 99 | let options = parse_args(args(&["--shout"])); | |
| 100 | assert!(options.shout); | |
| 101 | assert_eq!(options.name, None); | |
| 102 | } | |
| 103 | ||
| 104 | #[test] | |
| Add --version flag that prints the crate version and exits | 105 | fn parse_args_version_defaults_off() { |
| 106 | assert!(!parse_args(args(&["Ada"])).version); | |
| 107 | } | |
| 108 | ||
| 109 | #[test] | |
| 110 | fn parse_args_version() { | |
| 111 | let options = parse_args(args(&["--version"])); | |
| 112 | assert!(options.version); | |
| 113 | assert_eq!(options.name, None); | |
| 114 | } | |
| 115 | ||
| 116 | #[test] | |
| Add --shout flag that upper-cases the greeting | 117 | fn shout_upper_cases_greeting() { |
| 118 | assert_eq!(shout(&greeting("Ada")), "HELLO, ADA!"); | |
| 119 | } | |
| 120 | ||
| 121 | #[test] | |
| Greet people from the command line | 122 | fn greets_by_name() { |
| 123 | assert_eq!(greeting("Ada"), "Hello, Ada!"); | |
| 124 | } | |
| Handle empty and whitespace-only names, trim whitespace from input | 125 | |
| 126 | #[test] | |
| 127 | fn greets_world_with_empty_string() { | |
| 128 | assert_eq!(greeting(""), "Hello, world!"); | |
| 129 | } | |
| 130 | ||
| 131 | #[test] | |
| 132 | fn greets_world_with_whitespace_only() { | |
| 133 | assert_eq!(greeting(" "), "Hello, world!"); | |
| 134 | } | |
| 135 | ||
| 136 | #[test] | |
| 137 | fn trims_whitespace_from_name() { | |
| 138 | assert_eq!(greeting(" Ada "), "Hello, Ada!"); | |
| 139 | } | |
| Add farewell function alongside greeting | 140 | |
| 141 | #[test] | |
| 142 | fn says_goodbye_by_name() { | |
| 143 | assert_eq!(farewell("Ada"), "Goodbye, Ada!"); | |
| 144 | } | |
| 145 | ||
| 146 | #[test] | |
| 147 | fn says_goodbye_to_world_when_blank() { | |
| 148 | assert_eq!(farewell(" "), "Goodbye, world!"); | |
| 149 | } | |
| 150 | ||
| 151 | #[test] | |
| 152 | fn farewell_trims_whitespace() { | |
| 153 | assert_eq!(farewell(" Ada "), "Goodbye, Ada!"); | |
| 154 | } | |
| Greet people from the command line | 155 | } |