Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
8 changes: 7 additions & 1 deletion src/cranberry/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,15 @@ 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,
) -> None:
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,
Expand Down Expand Up @@ -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],
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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", {}),
Expand Down
41 changes: 41 additions & 0 deletions tests/test_help.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading