Add a --bye flag #64
Mergedg1t merged into maing1t-agenton g1t
Approved by g1t-agent
13 files+798−00/13 viewed
| 1 | + | root = true | |
| 2 | + | ||
| 3 | + | [*] | |
| 4 | + | charset = utf-8 | |
| 5 | + | end_of_line = lf | |
| 6 | + | insert_final_newline = true | |
| 7 | + | trim_trailing_whitespace = true | |
| 8 | + | ||
| 9 | + | [*.rs] | |
| 10 | + | indent_style = space | |
| 11 | + | indent_size = 4 | |
| 12 | + | ||
| 13 | + | [*.md] | |
| 14 | + | indent_style = space | |
| 15 | + | trim_trailing_whitespace = true | |
| 16 | + | ||
| 17 | + | [*.toml] | |
| 18 | + | indent_style = space | |
| 19 | + | indent_size = 4 |
| 1 | + | /target |
| 1 | + | # Changelog | |
| 2 | + | ||
| 3 | + | All notable changes to this project will be documented in this file. | |
| 4 | + | ||
| 5 | + | The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), | |
| 6 | + | and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). | |
| 7 | + | ||
| 8 | + | ## [Unreleased] | |
| 9 | + | ||
| 10 | + | ### Added | |
| 11 | + | ||
| 12 | + | - `--shout` flag that upper-cases the greeting | |
| 13 | + | - `--version` flag that prints the crate version and exits | |
| 14 | + | - `--times N` flag that prints the greeting N times (default 1) | |
| 15 | + | - `--lang` option that selects the greeting language (`en` default, `es`, `fr`) |
| 1 | + | # Contributor Covenant Code of Conduct | |
| 2 | + | ||
| 3 | + | ## Our Pledge | |
| 4 | + | ||
| 5 | + | We as members, contributors, and leaders pledge to make participation in our | |
| 6 | + | community a harassment-free experience for everyone, regardless of age, body | |
| 7 | + | size, visible or invisible disability, ethnicity, sex characteristics, gender | |
| 8 | + | identity and expression, level of experience, education, socio-economic status, | |
| 9 | + | nationality, personal appearance, race, caste, color, religion, or sexual | |
| 10 | + | identity and orientation. | |
| 11 | + | ||
| 12 | + | We pledge to act and interact in ways that contribute to an open, welcoming, | |
| 13 | + | diverse, inclusive, and healthy community. | |
| 14 | + | ||
| 15 | + | ## Our Standards | |
| 16 | + | ||
| 17 | + | Examples of behavior that contributes to a positive environment for our | |
| 18 | + | community include: | |
| 19 | + | ||
| 20 | + | * Demonstrating empathy and kindness toward other people | |
| 21 | + | * Being respectful of differing opinions, viewpoints, and experiences | |
| 22 | + | * Giving and gracefully accepting constructive feedback | |
| 23 | + | * Accepting responsibility and apologizing to those affected by our mistakes, | |
| 24 | + | and learning from the experience | |
| 25 | + | * Focusing on what is best not just for us as individuals, but for the overall | |
| 26 | + | community | |
| 27 | + | ||
| 28 | + | Examples of unacceptable behavior include: | |
| 29 | + | ||
| 30 | + | * The use of sexualized language or imagery, and sexual attention or advances of | |
| 31 | + | any kind | |
| 32 | + | * Trolling, insulting or derogatory comments, and personal or political attacks | |
| 33 | + | * Public or private harassment | |
| 34 | + | * Publishing others' private information, such as a physical or email address, | |
| 35 | + | without their explicit permission | |
| 36 | + | * Other conduct which could reasonably be considered inappropriate in a | |
| 37 | + | professional setting | |
| 38 | + | ||
| 39 | + | ## Enforcement Responsibilities | |
| 40 | + | ||
| 41 | + | Community leaders are responsible for clarifying and enforcing our standards of | |
| 42 | + | acceptable behavior and will take appropriate and fair corrective action in | |
| 43 | + | response to any behavior that they deem inappropriate, threatening, offensive, | |
| 44 | + | or harmful. | |
| 45 | + | ||
| 46 | + | Community leaders have the right and responsibility to remove, edit, or reject | |
| 47 | + | comments, commits, code, wiki edits, issues, and other contributions that are | |
| 48 | + | not aligned to this Code of Conduct, and will communicate reasons for moderation | |
| 49 | + | decisions when appropriate. | |
| 50 | + | ||
| 51 | + | ## Scope | |
| 52 | + | ||
| 53 | + | This Code of Conduct applies within all community spaces, and also applies when | |
| 54 | + | an individual is officially representing the community in public spaces. | |
| 55 | + | Examples of representing our community include using an official e-mail address, | |
| 56 | + | posting via an official social media account, or acting as an appointed | |
| 57 | + | representative at an online or offline event. | |
| 58 | + | ||
| 59 | + | ## Enforcement | |
| 60 | + | ||
| 61 | + | Instances of abusive, harassing, or otherwise unacceptable behavior may be | |
| 62 | + | reported to the community leaders responsible for enforcement at | |
| 63 | + | syntaqx@gmail.com. All complaints will be reviewed and investigated promptly and | |
| 64 | + | fairly. | |
| 65 | + | ||
| 66 | + | All community leaders are obligated to respect the privacy and security of the | |
| 67 | + | reporter of any incident. | |
| 68 | + | ||
| 69 | + | ## Enforcement Guidelines | |
| 70 | + | ||
| 71 | + | Community leaders will follow these Community Impact Guidelines in determining | |
| 72 | + | the consequences for any action they deem in violation of this Code of Conduct: | |
| 73 | + | ||
| 74 | + | ### 1. Correction | |
| 75 | + | ||
| 76 | + | **Community Impact**: Use of inappropriate language or other behavior deemed | |
| 77 | + | unprofessional or unwelcome in the community. | |
| 78 | + | ||
| 79 | + | **Consequence**: A private, written warning from community leaders, providing | |
| 80 | + | clarity around the nature of the violation and an explanation of why the | |
| 81 | + | behavior was inappropriate. A public apology may be requested. | |
| 82 | + | ||
| 83 | + | ### 2. Warning | |
| 84 | + | ||
| 85 | + | **Community Impact**: A violation through a single incident or series of | |
| 86 | + | actions. | |
| 87 | + | ||
| 88 | + | **Consequence**: A warning with consequences for continued behavior. No | |
| 89 | + | interaction with the people involved, including unsolicited interaction with | |
| 90 | + | those enforcing the Code of Conduct, for a specified period of time. This | |
| 91 | + | includes avoiding interactions in community spaces as well as external channels | |
| 92 | + | like social media. Violating these terms may lead to a temporary or permanent | |
| 93 | + | ban. | |
| 94 | + | ||
| 95 | + | ### 3. Temporary Ban | |
| 96 | + | ||
| 97 | + | **Community Impact**: A serious violation of community standards, including | |
| 98 | + | sustained inappropriate behavior. | |
| 99 | + | ||
| 100 | + | **Consequence**: A temporary ban from any sort of interaction or public | |
| 101 | + | communication with the community for a specified period of time. No public or | |
| 102 | + | private interaction with the people involved, including unsolicited interaction | |
| 103 | + | with those enforcing the Code of Conduct, is allowed during this period. | |
| 104 | + | Violating these terms may lead to a permanent ban. | |
| 105 | + | ||
| 106 | + | ### 4. Permanent Ban | |
| 107 | + | ||
| 108 | + | **Community Impact**: Demonstrating a pattern of violation of community | |
| 109 | + | standards, including sustained inappropriate behavior, harassment of an | |
| 110 | + | individual, or aggression toward or disparagement of classes of individuals. | |
| 111 | + | ||
| 112 | + | **Consequence**: A permanent ban from any sort of public interaction within the | |
| 113 | + | community. | |
| 114 | + | ||
| 115 | + | ## Attribution | |
| 116 | + | ||
| 117 | + | This Code of Conduct is adapted from the [Contributor Covenant][homepage], | |
| 118 | + | version 2.1, available at | |
| 119 | + | [https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1]. | |
| 120 | + | ||
| 121 | + | Community Impact Guidelines were inspired by | |
| 122 | + | [Mozilla's code of conduct enforcement ladder][Mozilla CoC]. | |
| 123 | + | ||
| 124 | + | For answers to common questions about this code of conduct, see the FAQ at | |
| 125 | + | [https://www.contributor-covenant.org/faq][FAQ]. Translations are available at | |
| 126 | + | [https://www.contributor-covenant.org/translations][translations]. | |
| 127 | + | ||
| 128 | + | [homepage]: https://www.contributor-covenant.org | |
| 129 | + | [v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html | |
| 130 | + | [Mozilla CoC]: https://github.com/mozilla/diversity | |
| 131 | + | [FAQ]: https://www.contributor-covenant.org/faq | |
| 132 | + | [translations]: https://www.contributor-covenant.org/translations |
| 1 | + | # Contributing | |
| 2 | + | ||
| 3 | + | Thanks for helping improve `hello`. This guide explains how to build the project, run its tests and propose a change. | |
| 4 | + | ||
| 5 | + | ## Build | |
| 6 | + | ||
| 7 | + | The crate is written in Rust (2021 edition). The logic lives in `src/lib.rs` (`parse_args`), the entry point is `src/main.rs`, and integration tests are in `tests/cli.rs`. | |
| 8 | + | ||
| 9 | + | ```sh | |
| 10 | + | cargo build | |
| 11 | + | ``` | |
| 12 | + | ||
| 13 | + | To try your changes, run the binary through Cargo: | |
| 14 | + | ||
| 15 | + | ```sh | |
| 16 | + | cargo run -- --shout Ada | |
| 17 | + | ``` | |
| 18 | + | ||
| 19 | + | ## Test | |
| 20 | + | ||
| 21 | + | ```sh | |
| 22 | + | cargo test | |
| 23 | + | ``` | |
| 24 | + | ||
| 25 | + | ## Propose a change | |
| 26 | + | ||
| 27 | + | 1. Fork the repository and clone your fork. | |
| 28 | + | 2. Create a branch for your change. | |
| 29 | + | 3. Make a focused change that does one thing, and add or update tests for it. | |
| 30 | + | 4. Run `cargo test` and make sure it passes. | |
| 31 | + | 5. If you add or change a flag, update the usage section in `README.md`. | |
| 32 | + | 6. Add an entry under Unreleased in `CHANGELOG.md`. | |
| 33 | + | 7. Open a pull request that describes what you changed and why. |
| 1 | + | # This file is automatically @generated by Cargo. | |
| 2 | + | # It is not intended for manual editing. | |
| 3 | + | version = 4 | |
| 4 | + | ||
| 5 | + | [[package]] | |
| 6 | + | name = "hello" | |
| 7 | + | version = "0.1.0" |
| 1 | + | [package] | |
| 2 | + | name = "hello" | |
| 3 | + | version = "0.1.0" | |
| 4 | + | edition = "2021" | |
| 5 | + | description = "Greets people from the command line." | |
| 6 | + | license = "MIT" |
| 1 | + | MIT License | |
| 2 | + | ||
| 3 | + | Copyright (c) 2026 hello contributors | |
| 4 | + | ||
| 5 | + | Permission is hereby granted, free of charge, to any person obtaining a copy | |
| 6 | + | of this software and associated documentation files (the "Software"), to deal | |
| 7 | + | in the Software without restriction, including without limitation the rights | |
| 8 | + | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
| 9 | + | copies of the Software, and to permit persons to whom the Software is | |
| 10 | + | furnished to do so, subject to the following conditions: | |
| 11 | + | ||
| 12 | + | The above copyright notice and this permission notice shall be included in all | |
| 13 | + | copies or substantial portions of the Software. | |
| 14 | + | ||
| 15 | + | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
| 16 | + | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
| 17 | + | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
| 18 | + | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
| 19 | + | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
| 20 | + | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
| 21 | + | SOFTWARE. |
| 1 | + | .PHONY: build test run | |
| 2 | + | ||
| 3 | + | .DEFAULT_GOAL := build | |
| 4 | + | ||
| 5 | + | build: | |
| 6 | + | cargo build | |
| 7 | + | ||
| 8 | + | test: | |
| 9 | + | cargo test | |
| 10 | + | ||
| 11 | + | run: | |
| 12 | + | cargo run -- $(ARGS) |
| 1 | + | # hello | |
| 2 | + | ||
| 3 | + | Greets people from the command line. | |
| 4 | + | ||
| 5 | + | ## Usage | |
| 6 | + | ||
| 7 | + | | Argument | Description | | |
| 8 | + | | -------- | ----------- | | |
| 9 | + | | `NAME` | Optional. The name to greet. Only the first argument is read. Defaults to the `USER` environment variable, then to `world`. | | |
| 10 | + | | `--shout` | Upper-cases the greeting. | | |
| 11 | + | | `--version` | Prints the crate version and exits. | | |
| 12 | + | | `--times N` | Prints the greeting `N` times. Defaults to 1. | | |
| 13 | + | | `--lang LANG` | Greets in the given language. Supported values are `en`, `es` and `fr`. Defaults to `en`. | | |
| 14 | + | ||
| 15 | + | ```sh | |
| 16 | + | $ cargo run -- Ada | |
| 17 | + | Hi, Ada! | |
| 18 | + | ``` | |
| 19 | + | ||
| 20 | + | ```sh | |
| 21 | + | $ cargo run -- --shout Ada | |
| 22 | + | HI, ADA! | |
| 23 | + | ``` | |
| 24 | + | ||
| 25 | + | ```sh | |
| 26 | + | $ cargo run -- --times 2 Ada | |
| 27 | + | Hi, Ada! | |
| 28 | + | Hi, Ada! | |
| 29 | + | ``` | |
| 30 | + | ||
| 31 | + | ```sh | |
| 32 | + | $ cargo run -- --lang es Ana | |
| 33 | + | ¡Hola, Ana! | |
| 34 | + | ``` | |
| 35 | + | ||
| 36 | + | ```sh | |
| 37 | + | $ cargo run -- --lang fr Luc | |
| 38 | + | Bonjour, Luc ! | |
| 39 | + | ``` | |
| 40 | + | ||
| 41 | + | ```sh | |
| 42 | + | $ cargo run -- --version | |
| 43 | + | 0.1.0 | |
| 44 | + | ``` | |
| 45 | + | ||
| 46 | + | With no name it greets whoever is signed in. | |
| 47 | + | ||
| 48 | + | ## Output | |
| 49 | + | ||
| 50 | + | ```sh | |
| 51 | + | $ cargo run -- Grace | |
| 52 | + | Hi, Grace! | |
| 53 | + | ``` | |
| 54 | + | ||
| 55 | + | ## Develop | |
| 56 | + | ||
| 57 | + | This project uses the Rust 2021 edition. | |
| 58 | + | ||
| 59 | + | ```sh | |
| 60 | + | cargo test | |
| 61 | + | ``` | |
| 62 | + | ||
| 63 | + | ## Troubleshooting | |
| 64 | + | ||
| 65 | + | **`error: --times value must be a number, got "abc"`** | |
| 66 | + | ||
| 67 | + | `--times` needs a whole number of zero or more. If the value is anything else, such as `abc`, `-1` or `2.5`, the program prints this error to stderr and exits with status 2 without greeting anyone. Pass a plain number, for example `--times 3`. A `--times` with no value after it also fails, with `--times requires a value`. | |
| 68 | + | ||
| 69 | + | **The greeting says `Hi, world!` when I gave a name** | |
| 70 | + | ||
| 71 | + | An empty or whitespace-only name, such as `cargo run -- ""`, is treated as no name and the greeting uses `world`. It does not fall back to the `USER` environment variable; that only happens when no name argument is given at all. Check that your shell is not expanding the name to an empty string, for example from an unset variable like `"$NAME"`. | |
| 72 | + | ||
| 73 | + | **`error: --lang value must be one of en, es, fr, got "de"`** | |
| 74 | + | ||
| 75 | + | `--lang` only accepts `en`, `es` or `fr`. For any other value the program prints this error to stderr and exits with status 2 without greeting anyone. Pass one of the supported languages, for example `--lang es`. | |
| 76 | + | ||
| 77 | + | ## License | |
| 78 | + | ||
| 79 | + | This project is licensed under the MIT License. | |
| 80 | + | The full license text is available at https://opensource.org/license/mit. |
| 1 | + | //! Greetings. | |
| 2 | + | ||
| 3 | + | /// A supported greeting language. | |
| 4 | + | #[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] | |
| 5 | + | pub enum Lang { | |
| 6 | + | #[default] | |
| 7 | + | En, | |
| 8 | + | Es, | |
| 9 | + | Fr, | |
| 10 | + | } | |
| 11 | + | ||
| 12 | + | impl Lang { | |
| 13 | + | /// Parses a language code (`en`, `es` or `fr`). | |
| 14 | + | pub fn parse(code: &str) -> Option<Lang> { | |
| 15 | + | match code { | |
| 16 | + | "en" => Some(Lang::En), | |
| 17 | + | "es" => Some(Lang::Es), | |
| 18 | + | "fr" => Some(Lang::Fr), | |
| 19 | + | _ => None, | |
| 20 | + | } | |
| 21 | + | } | |
| 22 | + | } | |
| 23 | + | ||
| 24 | + | /// Parsed command-line options. | |
| 25 | + | #[derive(Debug, PartialEq, Eq)] | |
| 26 | + | pub struct Options { | |
| 27 | + | /// The name to greet, if one was given. | |
| 28 | + | pub name: Option<String>, | |
| 29 | + | /// Whether to upper-case the greeting (`--shout`). | |
| 30 | + | pub shout: bool, | |
| 31 | + | /// Whether to print the crate version and exit (`--version`). | |
| 32 | + | pub version: bool, | |
| 33 | + | /// How many times to print the greeting (`--times N`). | |
| 34 | + | pub times: usize, | |
| 35 | + | /// The greeting language (`--lang CODE`). | |
| 36 | + | pub lang: Lang, | |
| 37 | + | /// Whether to print nothing (`--quiet`). | |
| 38 | + | pub quiet: bool, | |
| 39 | + | /// Whether to end the greeting with three exclamation marks (`--loud`). | |
| 40 | + | pub loud: bool, | |
| 41 | + | /// Whether to print a goodbye after the greeting (`--bye`). | |
| 42 | + | pub bye: bool, | |
| 43 | + | } | |
| 44 | + | ||
| 45 | + | impl Default for Options { | |
| 46 | + | fn default() -> Self { | |
| 47 | + | Options { | |
| 48 | + | name: None, | |
| 49 | + | shout: false, | |
| 50 | + | version: false, | |
| 51 | + | times: 1, | |
| 52 | + | lang: Lang::En, | |
| 53 | + | quiet: false, | |
| 54 | + | loud: false, | |
| 55 | + | bye: false, | |
| 56 | + | } | |
| 57 | + | } | |
| 58 | + | } | |
| 59 | + | ||
| 60 | + | /// Parses command-line arguments (excluding the program name). | |
| 61 | + | /// | |
| 62 | + | /// `--shout` sets [`Options::shout`], `--version` sets | |
| 63 | + | /// [`Options::version`], and `--times N` sets [`Options::times`] (the | |
| 64 | + | /// next argument is consumed as the value), and `--lang CODE` sets | |
| 65 | + | /// [`Options::lang`] (likewise consuming the next argument); other | |
| 66 | + | /// arguments starting with `--` are treated as flags and skipped. The | |
| 67 | + | /// name is the first remaining argument. | |
| 68 | + | /// | |
| 69 | + | /// Returns an error message if `--times` has a missing or non-numeric | |
| 70 | + | /// value, or if `--lang` has a missing or unsupported value. | |
| 71 | + | pub fn parse_args<I: IntoIterator<Item = String>>(args: I) -> Result<Options, String> { | |
| 72 | + | let mut options = Options::default(); | |
| 73 | + | let mut args = args.into_iter(); | |
| 74 | + | while let Some(arg) = args.next() { | |
| 75 | + | if arg == "--shout" { | |
| 76 | + | options.shout = true; | |
| 77 | + | } else if arg == "--version" { | |
| 78 | + | options.version = true; | |
| 79 | + | } else if arg == "--quiet" { | |
| 80 | + | options.quiet = true; | |
| 81 | + | } else if arg == "--loud" || arg == "-L" { | |
| 82 | + | options.loud = true; | |
| 83 | + | } else if arg == "--bye" { | |
| 84 | + | options.bye = true; | |
| 85 | + | } else if arg == "--times" { | |
| 86 | + | let value = args | |
| 87 | + | .next() | |
| 88 | + | .ok_or_else(|| "--times requires a value".to_owned())?; | |
| 89 | + | options.times = value | |
| 90 | + | .parse() | |
| 91 | + | .map_err(|_| format!("--times value must be a number, got {value:?}"))?; | |
| 92 | + | } else if arg == "--lang" { | |
| 93 | + | let value = args | |
| 94 | + | .next() | |
| 95 | + | .ok_or_else(|| "--lang requires a value".to_owned())?; | |
| 96 | + | options.lang = Lang::parse(&value).ok_or_else(|| { | |
| 97 | + | format!("--lang value must be one of en, es, fr, got {value:?}") | |
| 98 | + | })?; | |
| 99 | + | } else if !arg.starts_with("--") && options.name.is_none() { | |
| 100 | + | options.name = Some(arg); | |
| 101 | + | } | |
| 102 | + | } | |
| 103 | + | Ok(options) | |
| 104 | + | } | |
| 105 | + | ||
| 106 | + | /// `text` upper-cased. | |
| 107 | + | pub fn shout(text: &str) -> String { | |
| 108 | + | text.to_uppercase() | |
| 109 | + | } | |
| 110 | + | ||
| 111 | + | /// `text` with its final exclamation mark replaced by three. | |
| 112 | + | pub fn loud(text: &str) -> String { | |
| 113 | + | format!("{}!!!", text.strip_suffix('!').unwrap_or(text)) | |
| 114 | + | } | |
| 115 | + | ||
| 116 | + | /// The greeting for `name`. | |
| 117 | + | pub fn greeting(name: &str) -> String { | |
| 118 | + | greeting_in(name, Lang::En) | |
| 119 | + | } | |
| 120 | + | ||
| 121 | + | /// The greeting for `name` in `lang`. | |
| 122 | + | pub fn greeting_in(name: &str, lang: Lang) -> String { | |
| 123 | + | let trimmed = name.trim(); | |
| 124 | + | let display_name = if trimmed.is_empty() { "world" } else { trimmed }; | |
| 125 | + | match lang { | |
| 126 | + | Lang::En => format!("Hi, {display_name}!"), | |
| 127 | + | Lang::Es => format!("¡Hola, {display_name}!"), | |
| 128 | + | Lang::Fr => format!("Bonjour, {display_name} !"), | |
| 129 | + | } | |
| 130 | + | } | |
| 131 | + | ||
| 132 | + | /// The farewell for `name`. | |
| 133 | + | pub fn farewell(name: &str) -> String { | |
| 134 | + | let trimmed = name.trim(); | |
| 135 | + | let display_name = if trimmed.is_empty() { "world" } else { trimmed }; | |
| 136 | + | format!("Goodbye, {display_name}!") | |
| 137 | + | } | |
| 138 | + | ||
| 139 | + | #[cfg(test)] | |
| 140 | + | mod tests { | |
| 141 | + | use super::*; | |
| 142 | + | ||
| 143 | + | fn args(list: &[&str]) -> Vec<String> { | |
| 144 | + | list.iter().map(|s| s.to_string()).collect() | |
| 145 | + | } | |
| 146 | + | ||
| 147 | + | #[test] | |
| 148 | + | fn parse_args_quiet() { | |
| 149 | + | assert!(parse_args(args(&["--quiet", "Ada"])).unwrap().quiet); | |
| 150 | + | assert!(!parse_args(args(&["Ada"])).unwrap().quiet); | |
| 151 | + | } | |
| 152 | + | ||
| 153 | + | #[test] | |
| 154 | + | fn parse_args_bye() { | |
| 155 | + | assert!(parse_args(args(&["--bye", "Ada"])).unwrap().bye); | |
| 156 | + | assert!(!parse_args(args(&["Ada"])).unwrap().bye); | |
| 157 | + | } | |
| 158 | + | ||
| 159 | + | #[test] | |
| 160 | + | fn parse_args_loud() { | |
| 161 | + | assert!(parse_args(args(&["--loud", "Ada"])).unwrap().loud); | |
| 162 | + | assert!(parse_args(args(&["-L", "Ada"])).unwrap().loud); | |
| 163 | + | assert!(!parse_args(args(&["Ada"])).unwrap().loud); | |
| 164 | + | } | |
| 165 | + | ||
| 166 | + | #[test] | |
| 167 | + | fn loud_uses_three_exclamation_marks() { | |
| 168 | + | assert_eq!(loud(&greeting("Ada")), "Hi, Ada!!!"); | |
| 169 | + | } | |
| 170 | + | ||
| 171 | + | #[test] | |
| 172 | + | fn parse_args_no_args() { | |
| 173 | + | assert_eq!(parse_args(args(&[])).unwrap().name, None); | |
| 174 | + | } | |
| 175 | + | ||
| 176 | + | #[test] | |
| 177 | + | fn parse_args_name() { | |
| 178 | + | assert_eq!(parse_args(args(&["Ada"])).unwrap().name.as_deref(), Some("Ada")); | |
| 179 | + | } | |
| 180 | + | ||
| 181 | + | #[test] | |
| 182 | + | fn parse_args_skips_leading_flag() { | |
| 183 | + | assert_eq!( | |
| 184 | + | parse_args(args(&["--unknown", "Ada"])).unwrap().name.as_deref(), | |
| 185 | + | Some("Ada") | |
| 186 | + | ); | |
| 187 | + | } | |
| 188 | + | ||
| 189 | + | #[test] | |
| 190 | + | fn parse_args_shout_defaults_off() { | |
| 191 | + | assert!(!parse_args(args(&["Ada"])).unwrap().shout); | |
| 192 | + | } | |
| 193 | + | ||
| 194 | + | #[test] | |
| 195 | + | fn parse_args_shout_before_name() { | |
| 196 | + | let options = parse_args(args(&["--shout", "Ada"])).unwrap(); | |
| 197 | + | assert!(options.shout); | |
| 198 | + | assert_eq!(options.name.as_deref(), Some("Ada")); | |
| 199 | + | } | |
| 200 | + | ||
| 201 | + | #[test] | |
| 202 | + | fn parse_args_shout_after_name() { | |
| 203 | + | let options = parse_args(args(&["Ada", "--shout"])).unwrap(); | |
| 204 | + | assert!(options.shout); | |
| 205 | + | assert_eq!(options.name.as_deref(), Some("Ada")); | |
| 206 | + | } | |
| 207 | + | ||
| 208 | + | #[test] | |
| 209 | + | fn parse_args_shout_alone() { | |
| 210 | + | let options = parse_args(args(&["--shout"])).unwrap(); | |
| 211 | + | assert!(options.shout); | |
| 212 | + | assert_eq!(options.name, None); | |
| 213 | + | } | |
| 214 | + | ||
| 215 | + | #[test] | |
| 216 | + | fn parse_args_version_defaults_off() { | |
| 217 | + | assert!(!parse_args(args(&["Ada"])).unwrap().version); | |
| 218 | + | } | |
| 219 | + | ||
| 220 | + | #[test] | |
| 221 | + | fn parse_args_version() { | |
| 222 | + | let options = parse_args(args(&["--version"])).unwrap(); | |
| 223 | + | assert!(options.version); | |
| 224 | + | assert_eq!(options.name, None); | |
| 225 | + | } | |
| 226 | + | ||
| 227 | + | #[test] | |
| 228 | + | fn parse_args_times_defaults_to_one() { | |
| 229 | + | assert_eq!(parse_args(args(&["Ada"])).unwrap().times, 1); | |
| 230 | + | } | |
| 231 | + | ||
| 232 | + | #[test] | |
| 233 | + | fn parse_args_times_valid() { | |
| 234 | + | let options = parse_args(args(&["--times", "3", "Ada"])).unwrap(); | |
| 235 | + | assert_eq!(options.times, 3); | |
| 236 | + | assert_eq!(options.name.as_deref(), Some("Ada")); | |
| 237 | + | } | |
| 238 | + | ||
| 239 | + | #[test] | |
| 240 | + | fn parse_args_times_missing_value() { | |
| 241 | + | assert!(parse_args(args(&["--times"])).is_err()); | |
| 242 | + | } | |
| 243 | + | ||
| 244 | + | #[test] | |
| 245 | + | fn parse_args_times_non_number() { | |
| 246 | + | assert!(parse_args(args(&["--times", "abc", "Ada"])).is_err()); | |
| 247 | + | } | |
| 248 | + | ||
| 249 | + | #[test] | |
| 250 | + | fn parse_args_lang_defaults_to_en() { | |
| 251 | + | assert_eq!(parse_args(args(&["Ada"])).unwrap().lang, Lang::En); | |
| 252 | + | } | |
| 253 | + | ||
| 254 | + | #[test] | |
| 255 | + | fn parse_args_lang_valid() { | |
| 256 | + | let options = parse_args(args(&["--lang", "es", "Ana"])).unwrap(); | |
| 257 | + | assert_eq!(options.lang, Lang::Es); | |
| 258 | + | assert_eq!(options.name.as_deref(), Some("Ana")); | |
| 259 | + | assert_eq!(parse_args(args(&["--lang", "fr"])).unwrap().lang, Lang::Fr); | |
| 260 | + | } | |
| 261 | + | ||
| 262 | + | #[test] | |
| 263 | + | fn parse_args_lang_missing_value() { | |
| 264 | + | assert_eq!( | |
| 265 | + | parse_args(args(&["--lang"])).unwrap_err(), | |
| 266 | + | "--lang requires a value" | |
| 267 | + | ); | |
| 268 | + | } | |
| 269 | + | ||
| 270 | + | #[test] | |
| 271 | + | fn parse_args_lang_unknown() { | |
| 272 | + | assert_eq!( | |
| 273 | + | parse_args(args(&["--lang", "de"])).unwrap_err(), | |
| 274 | + | "--lang value must be one of en, es, fr, got \"de\"" | |
| 275 | + | ); | |
| 276 | + | } | |
| 277 | + | ||
| 278 | + | #[test] | |
| 279 | + | fn greeting_in_each_language() { | |
| 280 | + | assert_eq!(greeting_in("Ada", Lang::En), "Hi, Ada!"); | |
| 281 | + | assert_eq!(greeting_in("Ana", Lang::Es), "¡Hola, Ana!"); | |
| 282 | + | assert_eq!(greeting_in("Luc", Lang::Fr), "Bonjour, Luc !"); | |
| 283 | + | } | |
| 284 | + | ||
| 285 | + | #[test] | |
| 286 | + | fn greeting_in_trims_and_falls_back_to_world() { | |
| 287 | + | assert_eq!(greeting_in(" Ana ", Lang::Es), "¡Hola, Ana!"); | |
| 288 | + | assert_eq!(greeting_in(" ", Lang::Fr), "Bonjour, world !"); | |
| 289 | + | } | |
| 290 | + | ||
| 291 | + | #[test] | |
| 292 | + | fn shout_upper_cases_greeting() { | |
| 293 | + | assert_eq!(shout(&greeting("Ada")), "HI, ADA!"); | |
| 294 | + | } | |
| 295 | + | ||
| 296 | + | #[test] | |
| 297 | + | fn greets_by_name() { | |
| 298 | + | assert_eq!(greeting("Ada"), "Hi, Ada!"); | |
| 299 | + | } | |
| 300 | + | ||
| 301 | + | #[test] | |
| 302 | + | fn greets_world_with_empty_string() { | |
| 303 | + | assert_eq!(greeting(""), "Hi, world!"); | |
| 304 | + | } | |
| 305 | + | ||
| 306 | + | #[test] | |
| 307 | + | fn greets_world_with_whitespace_only() { | |
| 308 | + | assert_eq!(greeting(" "), "Hi, world!"); | |
| 309 | + | } | |
| 310 | + | ||
| 311 | + | #[test] | |
| 312 | + | fn trims_whitespace_from_name() { | |
| 313 | + | assert_eq!(greeting(" Ada "), "Hi, Ada!"); | |
| 314 | + | } | |
| 315 | + | ||
| 316 | + | #[test] | |
| 317 | + | fn says_goodbye_by_name() { | |
| 318 | + | assert_eq!(farewell("Ada"), "Goodbye, Ada!"); | |
| 319 | + | } | |
| 320 | + | ||
| 321 | + | #[test] | |
| 322 | + | fn says_goodbye_to_world_when_blank() { | |
| 323 | + | assert_eq!(farewell(" "), "Goodbye, world!"); | |
| 324 | + | } | |
| 325 | + | ||
| 326 | + | #[test] | |
| 327 | + | fn farewell_trims_whitespace() { | |
| 328 | + | assert_eq!(farewell(" Ada "), "Goodbye, Ada!"); | |
| 329 | + | } | |
| 330 | + | } |
| 1 | + | use std::{env, process}; | |
| 2 | + | ||
| 3 | + | use hello::{farewell, greeting_in, loud, parse_args, shout}; | |
| 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 message = greeting_in(&name, options.lang); | |
| 26 | + | let message = if options.loud { loud(&message) } else { message }; | |
| 27 | + | let line = if options.shout { shout(&message) } else { message }; | |
| 28 | + | for _ in 0..options.times { | |
| 29 | + | println!("{line}"); | |
| 30 | + | } | |
| 31 | + | if options.bye { | |
| 32 | + | println!("{}", farewell(&name)); | |
| 33 | + | } | |
| 34 | + | } |
| 1 | + | use std::process::Command; | |
| 2 | + | ||
| 3 | + | #[test] | |
| 4 | + | fn version_flag_prints_crate_version() { | |
| 5 | + | let output = Command::new(env!("CARGO_BIN_EXE_hello")) | |
| 6 | + | .arg("--version") | |
| 7 | + | .output() | |
| 8 | + | .unwrap(); | |
| 9 | + | assert!(output.status.success()); | |
| 10 | + | assert_eq!( | |
| 11 | + | String::from_utf8(output.stdout).unwrap(), | |
| 12 | + | format!("{}\n", env!("CARGO_PKG_VERSION")) | |
| 13 | + | ); | |
| 14 | + | } | |
| 15 | + | ||
| 16 | + | fn run(args: &[&str]) -> std::process::Output { | |
| 17 | + | Command::new(env!("CARGO_BIN_EXE_hello")) | |
| 18 | + | .args(args) | |
| 19 | + | .output() | |
| 20 | + | .unwrap() | |
| 21 | + | } | |
| 22 | + | ||
| 23 | + | #[test] | |
| 24 | + | fn times_prints_greeting_repeatedly() { | |
| 25 | + | let output = run(&["--times", "3", "Ada"]); | |
| 26 | + | assert!(output.status.success()); | |
| 27 | + | assert_eq!( | |
| 28 | + | String::from_utf8(output.stdout).unwrap(), | |
| 29 | + | "Hi, Ada!\n".repeat(3) | |
| 30 | + | ); | |
| 31 | + | } | |
| 32 | + | ||
| 33 | + | #[test] | |
| 34 | + | fn times_combines_with_shout() { | |
| 35 | + | let output = run(&["--times", "2", "--shout", "Ada"]); | |
| 36 | + | assert!(output.status.success()); | |
| 37 | + | assert_eq!( | |
| 38 | + | String::from_utf8(output.stdout).unwrap(), | |
| 39 | + | "HI, ADA!\n".repeat(2) | |
| 40 | + | ); | |
| 41 | + | } | |
| 42 | + | ||
| 43 | + | #[test] | |
| 44 | + | fn times_with_bad_value_fails() { | |
| 45 | + | let output = run(&["--times", "abc"]); | |
| 46 | + | assert!(!output.status.success()); | |
| 47 | + | assert!(!output.stderr.is_empty()); | |
| 48 | + | } | |
| 49 | + | ||
| 50 | + | #[test] | |
| 51 | + | fn lang_es_greets_in_spanish() { | |
| 52 | + | let output = run(&["--lang", "es", "Ana"]); | |
| 53 | + | assert!(output.status.success()); | |
| 54 | + | assert_eq!(String::from_utf8(output.stdout).unwrap(), "¡Hola, Ana!\n"); | |
| 55 | + | } | |
| 56 | + | ||
| 57 | + | #[test] | |
| 58 | + | fn lang_fr_greets_in_french() { | |
| 59 | + | let output = run(&["--lang", "fr", "Luc"]); | |
| 60 | + | assert!(output.status.success()); | |
| 61 | + | assert_eq!(String::from_utf8(output.stdout).unwrap(), "Bonjour, Luc !\n"); | |
| 62 | + | } | |
| 63 | + | ||
| 64 | + | #[test] | |
| 65 | + | fn lang_en_is_the_default() { | |
| 66 | + | let output = run(&["--lang", "en", "Ada"]); | |
| 67 | + | assert!(output.status.success()); | |
| 68 | + | assert_eq!(String::from_utf8(output.stdout).unwrap(), "Hi, Ada!\n"); | |
| 69 | + | let output = run(&["Ada"]); | |
| 70 | + | assert_eq!(String::from_utf8(output.stdout).unwrap(), "Hi, Ada!\n"); | |
| 71 | + | } | |
| 72 | + | ||
| 73 | + | #[test] | |
| 74 | + | fn lang_unknown_fails_naming_supported_languages() { | |
| 75 | + | let output = run(&["--lang", "de", "Ada"]); | |
| 76 | + | assert_eq!(output.status.code(), Some(2)); | |
| 77 | + | let stderr = String::from_utf8(output.stderr).unwrap(); | |
| 78 | + | assert!(stderr.contains("en, es, fr"), "stderr was {stderr:?}"); | |
| 79 | + | } | |
| 80 | + | ||
| 81 | + | #[test] | |
| 82 | + | fn lang_missing_value_fails() { | |
| 83 | + | let output = run(&["--lang"]); | |
| 84 | + | assert_eq!(output.status.code(), Some(2)); | |
| 85 | + | let stderr = String::from_utf8(output.stderr).unwrap(); | |
| 86 | + | assert!(stderr.contains("--lang requires a value"), "stderr was {stderr:?}"); | |
| 87 | + | } | |
| 88 | + | ||
| 89 | + | #[test] | |
| 90 | + | fn quiet_prints_nothing() { | |
| 91 | + | let output = run(&["--quiet", "Ada"]); | |
| 92 | + | assert!(output.status.success()); | |
| 93 | + | assert!(output.stdout.is_empty()); | |
| 94 | + | } | |
| 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 | + | } | |
| 102 | + | ||
| 103 | + | #[test] | |
| 104 | + | fn short_l_is_an_alias_for_loud() { | |
| 105 | + | let output = run(&["-L", "Ada"]); | |
| 106 | + | assert!(output.status.success()); | |
| 107 | + | assert_eq!(String::from_utf8(output.stdout).unwrap(), "Hi, Ada!!!\n"); | |
| 108 | + | } |