✨ 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
Provenance
- Commit
bdaa5c6- Tree
48511ff- Author
Sebastián Ramírez <tiangolo@gmail.com>- Committer
GitHub <noreply@github.com>- Parent
ef9beaf
4 files changed+143 added−3 removed
modified:docs/tutorial/typer-command.md+1 added
270 unmodified lines271 **Options**:272 273 * `--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.276 277 For example:278 279 <div class="termy">99 unmodified linesadded:tests/assets/cli/multiapp-docs-title.md+102 added
1+# Awesome CLI2+3+Demo App4+5+**Usage**:6+7+```console8+$ multiapp [OPTIONS] COMMAND [ARGS]...9+```10+11+**Options**:12+13+* `--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.16+17+The end18+19+**Commands**:20+21+* `sub`22+* `top`: Top command23+24+## `multiapp sub`25+26+**Usage**:27+28+```console29+$ multiapp sub [OPTIONS] COMMAND [ARGS]...30+```31+32+**Options**:33+34+* `--help`: Show this message and exit.35+36+**Commands**:37+38+* `bye`: Say bye39+* `hello`: Say Hello40+* `hi`: Say Hi41+42+### `multiapp sub bye`43+44+Say bye45+46+**Usage**:47+48+```console49+$ multiapp sub bye [OPTIONS]50+```51+52+**Options**:53+54+* `--help`: Show this message and exit.55+56+### `multiapp sub hello`57+58+Say Hello59+60+**Usage**:61+62+```console63+$ multiapp sub hello [OPTIONS]64+```65+66+**Options**:67+68+* `--name TEXT`: [default: World]69+* `--age INTEGER`: The age of the user [default: 0]70+* `--help`: Show this message and exit.71+72+### `multiapp sub hi`73+74+Say Hi75+76+**Usage**:77+78+```console79+$ multiapp sub hi [OPTIONS] [USER]80+```81+82+**Arguments**:83+84+* `[USER]`: The name of the user to greet [default: World]85+86+**Options**:87+88+* `--help`: Show this message and exit.89+90+## `multiapp top`91+92+Top command93+94+**Usage**:95+96+```console97+$ multiapp top [OPTIONS]98+```99+100+**Options**:101+102+* `--help`: Show this message and exit.modified:tests/test_cli/test_doc.py+30 added
53 unmodified lines54 assert docs in written_docs55 assert "Docs saved to:" in result.stdout56 57 58+def 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.stdout86+87+88 def test_doc_not_existing():89 result = subprocess.run(90 [91 sys.executable,51 unmodified linesmodified:typer/cli.py+10 added-3 removed
187 unmodified lines188 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 lines265 @utils_app.command()266 def 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 line285 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