✨ 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
Provenance
- Commit
1f684d4- Tree
27ff61d- Author
Sofie Van Landeghem <svlandeg@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
0f9b8fd
6 files changed+133 added−32 removed
modified:docs/tutorial/commands/index.md+33 added
255 unmodified lines256 ```257 258 </div>259 260+261+## Sorting of the commands262+263+Note that by design, **Typer** shows the commands in the order they've been declared.264+265+So, if we take our original example, with `create` and `delete` commands, and reverse the order in the Python file:266+267+```Python hl_lines="7 12"268+{!../docs_src/commands/index/tutorial004.py!}269+```270+271+Then we will see the `delete` command first in the help output:272+273+<div class="termy">274+275+```console276+// Check the help277+$ python main.py --help278+279+Usage: main.py [OPTIONS] COMMAND [ARGS]...280+281+Options: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.285+286+Commands:287+ delete288+ create289+```290+291+</div>292+293 ## Click Group294 295 If 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 linesadded:docs_src/commands/index/tutorial004.py+17 added
1+import typer2+3+app = typer.Typer()4+5+6+@app.command()7+def delete():8+ print("Deleting user: Hiro Hamada")9+10+11+@app.command()12+def create():13+ print("Creating user: Hiro Hamada")14+15+16+if __name__ == "__main__":17+ app()modified:tests/assets/cli/multiapp-docs-title.md+16 added-16 removed
16 unmodified lines171819202121222324242526272829302931323334353637363839383940414243444540414249434445464748495051525354555657585926 unmodified lines868788899090919292939495969797989910010110216 unmodified linesThe end
**Commands**:
* `sub`* `top`: Top command* `sub`
## `multiapp sub`## `multiapp top`
Top command
**Usage**:
```console$ multiapp sub [OPTIONS] COMMAND [ARGS]...$ multiapp top [OPTIONS]```
**Options**:
* `--help`: Show this message and exit.
**Commands**:## `multiapp sub`
* `bye`: Say bye* `hello`: Say Hello* `hi`: Say Hi
### `multiapp sub bye`
Say bye
**Usage**:
```console$ multiapp sub bye [OPTIONS]$ multiapp sub [OPTIONS] COMMAND [ARGS]...```
**Options**:
* `--help`: Show this message and exit.
**Commands**:
* `hello`: Say Hello* `hi`: Say Hi* `bye`: Say bye
### `multiapp sub hello`
Say Hello
26 unmodified lines**Options**:
* `--help`: Show this message and exit.
## `multiapp top`### `multiapp sub bye`
Top commandSay bye
**Usage**:
```console$ multiapp top [OPTIONS]$ multiapp sub bye [OPTIONS]```
**Options**:
* `--help`: Show this message and exit.modified:tests/assets/cli/multiapp-docs.md+16 added-16 removed
16 unmodified lines17 The end18 19 **Commands**:20 21-* `sub`21 * `top`: Top command22+* `sub`23 24-## `multiapp sub`24+## `multiapp top`25 26+Top command27+28 **Usage**:29 30 ```console29-$ multiapp sub [OPTIONS] COMMAND [ARGS]...31+$ multiapp top [OPTIONS]32 ```33 34 **Options**:35 36 * `--help`: Show this message and exit.37 36-**Commands**:38+## `multiapp sub`39 38-* `bye`: Say bye39-* `hello`: Say Hello40-* `hi`: Say Hi41-42-### `multiapp sub bye`43-44-Say bye45-40 **Usage**:41 42 ```console49-$ multiapp sub bye [OPTIONS]43+$ multiapp sub [OPTIONS] COMMAND [ARGS]...44 ```45 46 **Options**:47 48 * `--help`: Show this message and exit.49 50+**Commands**:51+52+* `hello`: Say Hello53+* `hi`: Say Hi54+* `bye`: Say bye55+56 ### `multiapp sub hello`57 58 Say Hello59 26 unmodified lines86 **Options**:87 88 * `--help`: Show this message and exit.89 90-## `multiapp top`90+### `multiapp sub bye`91 92-Top command92+Say bye93 94 **Usage**:95 96 ```console97-$ multiapp top [OPTIONS]97+$ multiapp sub bye [OPTIONS]98 ```99 100 **Options**:101 1 unmodified lineadded:tests/test_tutorial/test_commands/test_index/test_tutorial004.py+45 added
1+import subprocess2+import sys3+4+from typer.testing import CliRunner5+6+from docs_src.commands.index import tutorial004 as mod7+8+app = mod.app9+10+runner = CliRunner()11+12+13+def 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_char25+26+27+def test_create():28+ result = runner.invoke(app, ["create"])29+ assert result.exit_code == 030+ assert "Creating user: Hiro Hamada" in result.output31+32+33+def test_delete():34+ result = runner.invoke(app, ["delete"])35+ assert result.exit_code == 036+ assert "Deleting user: Hiro Hamada" in result.output37+38+39+def 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.stdoutmodified:typer/core.py+6 added
742 unmodified lines743 obj=self,744 ctx=ctx,745 markup_mode=self.rich_markup_mode,746 )747+748+ 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()]