diff --git a/pyproject.toml b/pyproject.toml index f6ba68b..b018706 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ version = "0.0.1" description = "Add your description here" readme = "README.md" authors = [{ name = "Chris O" }] -requires-python = ">=3.12" +requires-python = ">=3.11" dependencies = [] [build-system] diff --git a/src/cranberry/parser.py b/src/cranberry/parser.py index d9c0a95..0e4893a 100644 --- a/src/cranberry/parser.py +++ b/src/cranberry/parser.py @@ -83,6 +83,7 @@ def _print_help_and_exit( subcommands: list[type], help_cfg: dict[str, Any], version_cfg: dict[str, Any], + description: str | None = None, footer_msg: str | None, style: Style, parent_command: str | None, @@ -90,7 +91,7 @@ def _print_help_and_exit( text = render_help( app_name=app_name, command_name=command_name, - description=None, + description=description, global_fields=global_fields, command_fields=command_fields, subcommands=subcommands, @@ -140,6 +141,7 @@ def _parse_leaf_command( argv: list[str], fields: dict[str, FieldSpec], *, + leaf_cls: type, command_name: str, help_cfg: dict[str, Any], version_cfg: dict[str, Any], @@ -186,6 +188,8 @@ def _parse_leaf_command( subcommands=[], help_cfg=help_cfg, version_cfg=version_cfg, + # Get the description from the subcommand command if available, otherwise use none. + description=getattr(leaf_cls, "__cb_meta__", {}).get("description"), footer_msg=footer_msg, style=style, parent_command=parent_command, @@ -320,6 +324,7 @@ def parse_args(argv: list[str] | None = None) -> ParseContext: subcommands=subcommands, help_cfg=meta.get("help", {}), version_cfg=meta.get("version", {}), + description=meta.get("description"), footer_msg=footer_msg, style=style, parent_command=None, @@ -352,6 +357,7 @@ def parse_args(argv: list[str] | None = None) -> ParseContext: values = _parse_leaf_command( leaf_argv, fields, + leaf_cls=leaf_cls, command_name=_cmd_name(leaf_cls), help_cfg=meta.get("help", {}), version_cfg=meta.get("version", {}), diff --git a/tests/test_help.py b/tests/test_help.py index 97feb1c..bc4fef7 100644 --- a/tests/test_help.py +++ b/tests/test_help.py @@ -130,6 +130,47 @@ def test_version_flag_listed(self): result = make_help(has_version_flag=True) assert "--version" in result + def test_description_appears_before_sections(self): + result = make_help(description="My description") + + # Description should appear in output + assert "My description" in result + + # It should appear near the top (before Flags/Commands if present) + desc_index = result.index("My description") + + if "Flags:" in result: + assert desc_index < result.index("Flags:") + + if "Commands:" in result: + assert desc_index < result.index("Commands:") + + def test_description_is_not_modified(self): + result = make_help(description="Simple description text") + assert "Simple description text" in result + + def test_description_with_full_help_layout(self): + @cb.command("sub") + class Sub: + pass + + gf = {"flag": cb.flag("-g", "--global")} + + result = make_help( + description="CLI tool description", + global_fields=gf, + subcommands=[Sub], + footer_msg="Footer text", + ) + + # All major sections exist + assert "CLI tool description" in result + assert "Global Options:" in result + assert "Commands:" in result + assert "Footer text" in result + + # Description should appear early in the output + assert result.index("CLI tool description") < result.index("Commands:") # --------------------------------------------------------------------------- # _strip_ansi