diff --git a/bin/markdown_section/main.py b/bin/markdown_section/main.py index 22d02772..75c7f982 100644 --- a/bin/markdown_section/main.py +++ b/bin/markdown_section/main.py @@ -56,8 +56,7 @@ def section(text: str, fragment: str) -> str | Failure: found = headings(text) wanted = fragment.strip().lower() matching = [heading for heading in found if wanted in heading[2].lower()] - if len(matching) > 1: - matching = [heading for heading in matching if heading[2].lower() == wanted] or matching + matching = [heading for heading in matching if heading[2].lower() == wanted] or matching if not matching: return Failure(f"no heading holds '{fragment}'") lines = text.splitlines() @@ -65,5 +64,5 @@ def section(text: str, fragment: str) -> str | Failure: listed = "\n".join(f"{index + 1}: {lines[index]}" for index, _, _ in matching) return Failure(f"{len(matching)} headings hold '{fragment}', so use more of one:\n{listed}") start, level, _ = matching[0] - end = next((index for index, deeper, _ in found if index > start and deeper <= level), len(lines)) + end = next((index for index, deeper, _ in found if index > start and deeper <= level), None) return "\n".join(lines[start:end]).rstrip("\n") + "\n" diff --git a/bin/markdown_section/test_markdown_section.py b/bin/markdown_section/test_markdown_section.py index 82565201..6063a061 100644 --- a/bin/markdown_section/test_markdown_section.py +++ b/bin/markdown_section/test_markdown_section.py @@ -118,10 +118,12 @@ class Main(unittest.TestCase): with self.subTest(args): self.assertEqual(self.run_main(*args), (2, "", "usage: markdown-section \n")) - def test_help_prints_the_usage_and_exits_0(self): - code, out, err = self.run_main("-h") - self.assertEqual((code, err), (0, "")) - self.assertTrue(out.startswith("Print one section of a markdown file")) + def test_help_prints_the_usage_and_exits_0_whatever_follows_it(self): + for args in (("-h",), ("--help",), ("--help", "x")): + with self.subTest(args): + code, out, err = self.run_main(*args) + self.assertEqual((code, err), (0, "")) + self.assertTrue(out.startswith("Print one section of a markdown file")) class Launcher(unittest.TestCase):