Skip to content
Myr

✨ Add support for `typer ./someprogram.py utils docs --title` (#782)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Sebastián Ramírez · committed by GitHub · parent ef9beaf · Browse files · 4 files+143 added−3 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Trailers

  • Co-authored-by: Jeffrey Newman <jeff@newman.me>

Diff stats

4 files+143 added−3 removed

  • tests:+132 added−0 removed
  • other:+11 added−3 removed

4 files+143 added−3 removed

modified:docs/tutorial/typer-command.md+1 added
270 unmodified lines
271**Options**:272273* `--name TEXT`: The name of the CLI program to use in docs.274* `--output FILE`: An output file to write docs to, like README.md.275* `--title TEXT`: A title to use in the docs, by default the name of the command.276277For example:278279<div class="termy">
99 unmodified lines
added:tests/assets/cli/multiapp-docs-title.md+102 added
1# Awesome CLI23Demo App45**Usage**:67```console8$ multiapp [OPTIONS] COMMAND [ARGS]...9```1011**Options**:1213* `--install-completion`: Install completion for the current shell.14* `--show-completion`: Show completion for the current shell, to copy it or customize the installation.15* `--help`: Show this message and exit.1617The end1819**Commands**:2021* `sub`22* `top`: Top command2324## `multiapp sub`2526**Usage**:2728```console29$ multiapp sub [OPTIONS] COMMAND [ARGS]...30```3132**Options**:3334* `--help`: Show this message and exit.3536**Commands**:3738* `bye`: Say bye39* `hello`: Say Hello40* `hi`: Say Hi4142### `multiapp sub bye`4344Say bye4546**Usage**:4748```console49$ multiapp sub bye [OPTIONS]50```5152**Options**:5354* `--help`: Show this message and exit.5556### `multiapp sub hello`5758Say Hello5960**Usage**:6162```console63$ multiapp sub hello [OPTIONS]64```6566**Options**:6768* `--name TEXT`: [default: World]69* `--age INTEGER`: The age of the user [default: 0]70* `--help`: Show this message and exit.7172### `multiapp sub hi`7374Say Hi7576**Usage**:7778```console79$ multiapp sub hi [OPTIONS] [USER]80```8182**Arguments**:8384* `[USER]`: The name of the user to greet [default: World]8586**Options**:8788* `--help`: Show this message and exit.8990## `multiapp top`9192Top command9394**Usage**:9596```console97$ multiapp top [OPTIONS]98```99100**Options**:101102* `--help`: Show this message and exit.
modified:tests/test_cli/test_doc.py+30 added
53 unmodified lines
54 assert docs in written_docs55 assert "Docs saved to:" in result.stdout565758def test_doc_title_output(tmp_path: Path):59 out_file: Path = tmp_path / "out.md"60 result = subprocess.run(61 [62 sys.executable,63 "-m",64 "coverage",65 "run",66 "-m",67 "typer",68 "tests.assets.cli.multi_app",69 "utils",70 "docs",71 "--name",72 "multiapp",73 "--title",74 "Awesome CLI",75 "--output",76 str(out_file),77 ],78 capture_output=True,79 encoding="utf-8",80 )81 docs_path: Path = Path(__file__).parent.parent / "assets/cli/multiapp-docs-title.md"82 docs = docs_path.read_text()83 written_docs = out_file.read_text()84 assert docs in written_docs85 assert "Docs saved to:" in result.stdout868788def test_doc_not_existing():89 result = subprocess.run(90 [91 sys.executable,
51 unmodified lines
modified:typer/cli.py+10 added-3 removed
187 unmodified lines
188 ctx: typer.Context,189 indent: int = 0,190 name: str = "",191 call_prefix: str = "",192 title: Optional[str] = None,193) -> str:194 docs = "#" * (1 + indent)195 command_name = name or obj.name196 if call_prefix:197 command_name = f"{call_prefix} {command_name}"198 if not title:197 title = f"`{command_name}`" if command_name else "CLI"199 title = f"`{command_name}`" if command_name else "CLI"200 docs += f" {title}\n\n"201 if obj.help:202 docs += f"{obj.help}\n\n"203 usage_pieces = obj.collect_usage_pieces(ctx)
61 unmodified lines
265@utils_app.command()266def docs(267 ctx: typer.Context,268 name: str = typer.Option("", help="The name of the CLI program to use in docs."),267 output: Path = typer.Option(269 output: Optional[Path] = typer.Option(270 None,271 help="An output file to write docs to, like README.md.",272 file_okay=True,273 dir_okay=False,274 ),275 title: Optional[str] = typer.Option(276 None,277 help="The title for the documentation page. If not provided, the name of "278 "the program is used.",279 ),280) -> None:281 """282 Generate Markdown docs for a Typer app.283 """
1 unmodified line
285 if not typer_obj:286 typer.echo("No Typer app found", err=True)287 raise typer.Abort()288 click_obj = typer.main.get_command(typer_obj)282 docs = get_docs_for_click(obj=click_obj, ctx=ctx, name=name)289 docs = get_docs_for_click(obj=click_obj, ctx=ctx, name=name, title=title)290 clean_docs = f"{docs.strip()}\n"291 if output:292 output.write_text(clean_docs)293 typer.echo(f"Docs saved to: {output}")
6 unmodified lines