Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Add support for rich_help_panel of commands in generated docs #828

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
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
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
24 changes: 15 additions & 9 deletions tests/assets/cli/multiapp-docs-title.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,38 +35,44 @@ $ multiapp sub [OPTIONS] COMMAND [ARGS]...

**Commands**:

* `bye`: Say bye
* `hello`: Say Hello

**Farewell**:

* `bye`: Say bye

**Greet**:

* `hi`: Say Hi

### `multiapp sub bye`
### `multiapp sub hello`

Say bye
Say Hello

**Usage**:

```console
$ multiapp sub bye [OPTIONS]
$ multiapp sub hello [OPTIONS]
```

**Options**:

* `--name TEXT`: [default: World]
* `--age INTEGER`: The age of the user [default: 0]
* `--help`: Show this message and exit.

### `multiapp sub hello`
### `multiapp sub bye`

Say Hello
Say bye

**Usage**:

```console
$ multiapp sub hello [OPTIONS]
$ multiapp sub bye [OPTIONS]
```

**Options**:

* `--name TEXT`: [default: World]
* `--age INTEGER`: The age of the user [default: 0]
* `--help`: Show this message and exit.

### `multiapp sub hi`
Expand Down
24 changes: 15 additions & 9 deletions tests/assets/cli/multiapp-docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,38 +35,44 @@ $ multiapp sub [OPTIONS] COMMAND [ARGS]...

**Commands**:

* `bye`: Say bye
* `hello`: Say Hello

**Farewell**:

* `bye`: Say bye

**Greet**:

* `hi`: Say Hi

### `multiapp sub bye`
### `multiapp sub hello`

Say bye
Say Hello

**Usage**:

```console
$ multiapp sub bye [OPTIONS]
$ multiapp sub hello [OPTIONS]
```

**Options**:

* `--name TEXT`: [default: World]
* `--age INTEGER`: The age of the user [default: 0]
* `--help`: Show this message and exit.

### `multiapp sub hello`
### `multiapp sub bye`

Say Hello
Say bye

**Usage**:

```console
$ multiapp sub hello [OPTIONS]
$ multiapp sub bye [OPTIONS]
```

**Options**:

* `--name TEXT`: [default: World]
* `--age INTEGER`: The age of the user [default: 0]
* `--help`: Show this message and exit.

### `multiapp sub hi`
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 @@ -240,19 +243,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": {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": {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