Skip to content
Myr

✨ Show help items in order of definition (#944)

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

authored by Sofie Van Landeghem · committed by GitHub · parent 0f9b8fd · Browse files · 6 files+133 added−32 removed
This commit

State

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

Diff stats

6 files+133 added−32 removed

  • tests:+77 added−32 removed
  • other:+56 added−0 removed

6 files+133 added−32 removed

modified:docs/tutorial/commands/index.md+33 added
255 unmodified lines
256```257258</div>259260261## Sorting of the commands262263Note that by design, **Typer** shows the commands in the order they've been declared.264265So, if we take our original example, with `create` and `delete` commands, and reverse the order in the Python file:266267```Python hl_lines="7 12"268{!../docs_src/commands/index/tutorial004.py!}269```270271Then we will see the `delete` command first in the help output:272273<div class="termy">274275```console276// Check the help277$ python main.py --help278279Usage: main.py [OPTIONS] COMMAND [ARGS]...280281Options:282 --install-completion Install completion for the current shell.283 --show-completion Show completion for the current shell, to copy it or customize the installation.284 --help Show this message and exit.285286Commands:287 delete288 create289```290291</div>292293## Click Group294295If you come from Click, a `typer.Typer` app with subcommands is more or less the equivalent of a <a href="https://click.palletsprojects.com/en/7.x/quickstart/#nesting-commands" class="external-link" target="_blank">Click Group</a>.296
23 unmodified lines
added:docs_src/commands/index/tutorial004.py+17 added
1import typer23app = typer.Typer()456@app.command()7def delete():8 print("Deleting user: Hiro Hamada")91011@app.command()12def create():13 print("Creating user: Hiro Hamada")141516if __name__ == "__main__":17 app()
modified:tests/assets/cli/multiapp-docs-title.md+16 added-16 removed
modified:tests/assets/cli/multiapp-docs.md+16 added-16 removed
16 unmodified lines
17The end1819**Commands**:2021* `sub`21* `top`: Top command22* `sub`2324## `multiapp sub`24## `multiapp top`2526Top command2728**Usage**:2930```console29$ multiapp sub [OPTIONS] COMMAND [ARGS]...31$ multiapp top [OPTIONS]32```3334**Options**:3536* `--help`: Show this message and exit.3736**Commands**:38## `multiapp sub`3938* `bye`: Say bye39* `hello`: Say Hello40* `hi`: Say Hi4142### `multiapp sub bye`4344Say bye4540**Usage**:4142```console49$ multiapp sub bye [OPTIONS]43$ multiapp sub [OPTIONS] COMMAND [ARGS]...44```4546**Options**:4748* `--help`: Show this message and exit.4950**Commands**:5152* `hello`: Say Hello53* `hi`: Say Hi54* `bye`: Say bye5556### `multiapp sub hello`5758Say Hello59
26 unmodified lines
86**Options**:8788* `--help`: Show this message and exit.8990## `multiapp top`90### `multiapp sub bye`9192Top command92Say bye9394**Usage**:9596```console97$ multiapp top [OPTIONS]97$ multiapp sub bye [OPTIONS]98```99100**Options**:101
1 unmodified line
added:tests/test_tutorial/test_commands/test_index/test_tutorial004.py+45 added
1import subprocess2import sys34from typer.testing import CliRunner56from docs_src.commands.index import tutorial004 as mod78app = mod.app910runner = CliRunner()111213def test_help():14 result = runner.invoke(app, ["--help"])15 assert result.exit_code == 016 assert "[OPTIONS] COMMAND [ARGS]..." in result.output17 print(result.output)18 assert "Commands" in result.output19 assert "create" in result.output20 assert "delete" in result.output21 # Test that the 'delete' command precedes the 'create' command in the help output22 create_char = result.output.index("create")23 delete_char = result.output.index("delete")24 assert delete_char < create_char252627def test_create():28 result = runner.invoke(app, ["create"])29 assert result.exit_code == 030 assert "Creating user: Hiro Hamada" in result.output313233def test_delete():34 result = runner.invoke(app, ["delete"])35 assert result.exit_code == 036 assert "Deleting user: Hiro Hamada" in result.output373839def test_script():40 result = subprocess.run(41 [sys.executable, "-m", "coverage", "run", mod.__file__, "--help"],42 capture_output=True,43 encoding="utf-8",44 )45 assert "Usage" in result.stdout
modified:typer/core.py+6 added
742 unmodified lines
743 obj=self,744 ctx=ctx,745 markup_mode=self.rich_markup_mode,746 )747748 def list_commands(self, ctx: click.Context) -> List[str]:749 """Returns a list of subcommand names.750 Note that in Click's Group class, these are sorted.751 In Typer, we wish to maintain the original order of creation (cf Issue #933)"""752 return [n for n, c in self.commands.items()]