From 9625423548ea198632914da4967098aa06d119af Mon Sep 17 00:00:00 2001 From: Felix Brandt Date: Wed, 3 Jun 2026 09:40:00 +0200 Subject: [PATCH] Add --count to repeat the greeting Implements: https://todo.hut.md/~reviewer/hello-hut/2 --- src/hello_hut/cli.py | 8 +++++++- tests/test_cli.py | 24 ++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 tests/test_cli.py diff --git a/src/hello_hut/cli.py b/src/hello_hut/cli.py index 857d4982fed467760cc744901a5147e082c7b149..d6a52e94ef8317b434d31ec13b23237fda6aa621 100644 --- a/src/hello_hut/cli.py +++ b/src/hello_hut/cli.py @@ -18,6 +18,8 @@ def build_parser(): help="print the greeting in capitals") parser.add_argument("--time-of-day", action="store_true", help="say good morning, afternoon or evening") + parser.add_argument("--count", type=int, default=1, metavar="N", + help="repeat the greeting N times (default: 1)") parser.add_argument("--version", action="version", version=f"%(prog)s {__version__}") return parser @@ -28,8 +30,12 @@ def main(argv=None): config = load_config() name = args.name or config.get("name", DEFAULT_NAME) shout = args.shout or bool(config.get("shout", False)) + if args.count < 1: + print("hello-hut: --count must be at least 1", file=sys.stderr) + return 2 salutation = time_of_day_greeting() if args.time_of_day else None message = greet(name, shout=shout, salutation=salutation) - print(message) + for _ in range(args.count): + print(message) return 0 diff --git a/tests/test_cli.py b/tests/test_cli.py new file mode 100644 index 0000000000000000000000000000000000000000..4f6e0ab3d7543742432f253cbfba9438fa6f1120 --- /dev/null +++ b/tests/test_cli.py @@ -0,0 +1,24 @@ +import io +import unittest +from contextlib import redirect_stdout + +from hello_hut.cli import main + + +def run(*argv): + out = io.StringIO() + with redirect_stdout(out): + code = main(list(argv)) + return code, out.getvalue() + + +class CliTest(unittest.TestCase): + def test_count(self): + code, out = run("Ada", "--count", "3") + self.assertEqual(code, 0) + self.assertEqual(out.splitlines(), ["Hello, Ada!"] * 3) + + def test_count_must_be_positive(self): + code, out = run("Ada", "--count", "0") + self.assertEqual(code, 2) + self.assertEqual(out, "")