| 1 | use std::{env, process}; |
| 2 | |
| 3 | use hello::{farewell, greeting_in, loud, parse_args, reverse, shout, wave, whisper}; |
| 4 | |
| 5 | fn main() { |
| 6 | let options = match parse_args(env::args().skip(1)) { |
| 7 | Ok(options) => options, |
| 8 | Err(message) => { |
| 9 | eprintln!("error: {message}"); |
| 10 | process::exit(2); |
| 11 | } |
| 12 | }; |
| 13 | if options.version { |
| 14 | println!("{}", env!("CARGO_PKG_VERSION")); |
| 15 | return; |
| 16 | } |
| 17 | if options.quiet { |
| 18 | return; |
| 19 | } |
| 20 | // The name given, or whoever is signed in. |
| 21 | let name = options |
| 22 | .name |
| 23 | .or_else(|| env::var("USER").ok()) |
| 24 | .unwrap_or_else(|| "world".to_owned()); |
| 25 | let name = if options.reverse { |
| 26 | reverse(&name) |
| 27 | } else { |
| 28 | name |
| 29 | }; |
| 30 | let message = greeting_in(&name, options.lang); |
| 31 | let message = if options.loud { |
| 32 | loud(&message) |
| 33 | } else { |
| 34 | message |
| 35 | }; |
| 36 | let line = if options.shout { |
| 37 | shout(&message) |
| 38 | } else if options.whisper { |
| 39 | whisper(&message) |
| 40 | } else { |
| 41 | message |
| 42 | }; |
| 43 | let line = if options.both { |
| 44 | format!("{line} {}", farewell(&name)) |
| 45 | } else { |
| 46 | line |
| 47 | }; |
| 48 | for _ in 0..options.times { |
| 49 | println!("{line}"); |
| 50 | } |
| 51 | if options.wave { |
| 52 | println!("{}", wave()); |
| 53 | } |
| 54 | if options.bye { |
| 55 | println!("{}", farewell(&name)); |
| 56 | } |
| 57 | } |