pr_01m3xsjv8netd8ept0sbk2rms3/src/main.rs
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 | use std::env; |
| Add multi-language support with --lang option | 2 | use std::process; |
| Greet people from the command line | 3 | |
| 4 | use hello::greeting; | |
| 5 | ||
| 6 | fn main() { | |
| Add multi-language support with --lang option | 7 | let mut args = env::args().skip(1); |
| 8 | let mut lang = "en".to_string(); | |
| 9 | let mut name = None; | |
| 10 | ||
| 11 | // Parse arguments | |
| 12 | while let Some(arg) = args.next() { | |
| 13 | if arg == "--lang" { | |
| 14 | lang = match args.next() { | |
| 15 | Some(l) => l, | |
| 16 | None => { | |
| 17 | eprintln!("Error: --lang requires an argument"); | |
| 18 | process::exit(1); | |
| 19 | } | |
| 20 | }; | |
| 21 | } else { | |
| 22 | name = Some(arg); | |
| 23 | break; | |
| 24 | } | |
| 25 | } | |
| 26 | ||
| 27 | // Get the name, or fall back to USER env var, or "world" | |
| 28 | let name = name | |
| Greet people from the command line | 29 | .or_else(|| env::var("USER").ok()) |
| 30 | .unwrap_or_else(|| "world".to_owned()); | |
| Add multi-language support with --lang option | 31 | |
| 32 | // Get the greeting, or print error and exit | |
| 33 | match greeting(&name, &lang) { | |
| 34 | Some(msg) => println!("{}", msg), | |
| 35 | None => { | |
| 36 | eprintln!("Error: unknown language '{}'", lang); | |
| 37 | eprintln!("Supported languages: en, es, fr"); | |
| 38 | process::exit(1); | |
| 39 | } | |
| 40 | } | |
| Greet people from the command line | 41 | } |