Skip to content
Open
Show file tree
Hide file tree
Changes from 11 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
6 changes: 3 additions & 3 deletions tests/assets/cli/multi_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ def hello(name: str = "World", age: int = typer.Option(0, help="The age of the u
typer.echo(f"Hello {name}")


@sub_app.command()
@sub_app.command(rich_help_panel="Greet")
def hi(user: str = typer.Argument("World", help="The name of the user to greet")):
"""
Say Hi
"""


@sub_app.command()
@sub_app.command(rich_help_panel="Farewell")
def bye():
"""
Say bye
Expand All @@ -32,7 +32,7 @@ def bye():
app.add_typer(sub_app, name="sub")


@app.command()
@app.command(rich_help_panel="")
def top():
"""
Top command
Expand Down
6 changes: 6 additions & 0 deletions tests/assets/cli/multiapp-docs-title.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,13 @@ $ multiapp sub [OPTIONS] COMMAND [ARGS]...
**Commands**:

* `hello`: Say Hello

**Greet**:

* `hi`: Say Hi

**Farewell**:

* `bye`: Say bye

### `multiapp sub hello`
Expand Down
6 changes: 6 additions & 0 deletions tests/assets/cli/multiapp-docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,13 @@ $ multiapp sub [OPTIONS] COMMAND [ARGS]...
**Commands**:

* `hello`: Say Hello

**Greet**:

* `hi`: Say Hi

**Farewell**:

* `bye`: Say bye

### `multiapp sub hello`
Expand Down
37 changes: 27 additions & 10 deletions typer/cli.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import importlib.util
import re
import sys
from collections import defaultdict
from itertools import chain
from pathlib import Path
from typing import Any, List, Optional
from typing import Any, Dict, List, Optional

import click
import typer
import typer.core
from click import Command, Group, Option
from typer.rich_utils import _RICH_HELP_PANEL_NAME, COMMANDS_PANEL_TITLE

from . import __version__

Expand Down Expand Up @@ -250,19 +253,33 @@ def get_docs_for_click(
group = obj
commands = group.list_commands(ctx)
if commands:
docs += "**Commands**:\n\n"
panel_to_commands: Dict[str, List[click.Command]] = defaultdict(list)
for command in commands:
command_obj = group.get_command(ctx, command)
assert command_obj
docs += f"* `{command_obj.name}`"
command_help = command_obj.get_short_help_str()
if command_help:
docs += f": {_parse_html(command_help)}"
panel_name = (
getattr(command_obj, _RICH_HELP_PANEL_NAME, None)
or COMMANDS_PANEL_TITLE
)
panel_to_commands[panel_name].append(command_obj)
default_command_objs = panel_to_commands.pop(COMMANDS_PANEL_TITLE, [])
if default_command_objs:
panel_to_commands = {
COMMANDS_PANEL_TITLE: default_command_objs,
**panel_to_commands,
}
for panel_name, command_objs in panel_to_commands.items():
docs += f"**{panel_name}**:\n\n"
for command_obj in command_objs:
docs += f"* `{command_obj.name}`"
command_help = command_obj.get_short_help_str()
if command_help:
docs += f": {_parse_html(command_help)}"
docs += "\n"
docs += "\n"
docs += "\n"
for command in commands:
command_obj = group.get_command(ctx, command)
assert command_obj
for command_obj in chain.from_iterable(
command_objs for command_objs in panel_to_commands.values()
):
use_prefix = ""
if command_name:
use_prefix += f"{command_name}"
Expand Down
Loading