fastapi / typer

Typer, build great CLIs. Easy to code. Based on Python type hints.
https://typer.tiangolo.com/
MIT License
15.66k stars 666 forks source link

Show help items in order of definition, not alphabetically #933

Closed tiangolo closed 2 months ago

tiangolo commented 2 months ago

Privileged issue

Issue Content

Show help items in order of definition, not alphabetically

For example:

import typer

app = typer.Typer()

@app.command()
def first(word: str | None = None):
    """
    First declared command should show up first.
    """
    print(f"first {word}")

@app.command()
def after(word: str | None = None):
    """
    Last declared command should show up last, not alphabetically.
    """
    print(f"after {word}")

This is the current output:

$ typer demo.py run --help

 Usage: typer [PATH_OR_MODULE] run [OPTIONS] COMMAND [ARGS]...              

 Run the provided Typer app.                                                

╭─ Options ────────────────────────────────────────────────────────────────╮
│ --help          Show this message and exit.                              │
╰──────────────────────────────────────────────────────────────────────────╯
╭─ Commands ───────────────────────────────────────────────────────────────╮
│ after   Last declared command should show up last, not alphabetically.   │
│ first   First declared command should show up first.                     │
╰──────────────────────────────────────────────────────────────────────────╯

This is the ideal output:

$ typer demo.py run --help

 Usage: typer [PATH_OR_MODULE] run [OPTIONS] COMMAND [ARGS]...              

 Run the provided Typer app.                                                

╭─ Options ────────────────────────────────────────────────────────────────╮
│ --help          Show this message and exit.                              │
╰──────────────────────────────────────────────────────────────────────────╯
╭─ Commands ───────────────────────────────────────────────────────────────╮
│ first   First declared command should show up first.                     │
│ after   Last declared command should show up last, not alphabetically.   │
╰──────────────────────────────────────────────────────────────────────────╯