Skip to content

Main app with no commands no longer shows callback docstring in v0.4.0 #328

Description

@jayqi

First Check

  • I added a very descriptive title to this issue.
  • I used the GitHub search to find a similar issue and didn't find it.
  • I searched the Typer documentation, with the integrated search.
  • I already searched in Google "How to X in Typer" and didn't find any information.
  • I already read and followed all the tutorial in the docs and didn't find an answer.
  • I already checked if it is not related to Typer but to Click.

Commit to Help

  • I commit to help with one of those options 👆

Example Code

import typer

app = typer.Typer()

@app.callback()
def main():
    """Main program help text."""
    pass

@app.command()
def foo():
    """foo command help text."""
    typer.echo("Executed foo.")

if __name__ == "__main__":
    app()

Description

Not sure if this should be considered a bug or enhancement, since this was an undocumented change that could potentially be considered a regression.

  • In typer<=0.3.2, when the main application is executed without any commands, it would previously print the help text from the callback and exit with status code 0.
  • In typer 0.4.0, this same behavior prints only Try 'app.py --help' for help. and Error: Missing command. and exits with status code 2.

I'm fine with the status code change—it does seem to me like an improvement that it has an error code. Thank you for this change. (Though it would be nice for this to be documented in the change log.)

However, I think it would be useful to also directly print the help text without requiring users to make another invocation with --help, as was the previous behavior. The git program, for example, will exit with status 1 but also print the help text when used without any commands.

This appears to be only a typer change, and not a click change, as typer 0.4.0 behaves the same with way both click 8.0.1 and click 7.1.2.

typer 0.4.0; click 8.0.1

❯ python -c "import typer; print('typer', typer.__version__); import click; print('click', click.__version__)"
typer 0.4.0
click 8.0.1

❯ python app.py
Usage: app.py [OPTIONS] COMMAND [ARGS]...
Try 'app.py --help' for help.

Error: Missing command.

❯ echo $?
2

typer 0.4.0; click 7.1.2

❯ python -c "import typer; print('typer', typer.__version__); import click; print('click', click.__version__)"
typer 0.4.0
click 7.1.2

 ❯ python app.py
Usage: app.py [OPTIONS] COMMAND [ARGS]...
Try 'app.py --help' for help.

Error: Missing command.

❯ echo $?
2

typer 0.3.2; click 7.1.2

❯ python -c "import typer; print('typer', typer.__version__); import click; print('click', click.__version__)"
typer 0.3.2
click 7.1.2

❯ python app.py
Usage: app.py [OPTIONS] COMMAND [ARGS]...

  Main program help text.

Options:
  --install-completion  Install completion for the current shell.
  --show-completion     Show completion for the current shell, to copy it or
                        customize the installation.

  --help                Show this message and exit.

Commands:
  foo  foo command help text.

❯ echo $?
0

Operating System

macOS

Operating System Details

No response

Typer Version

0.4.0

Python Version

3.8.10

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionQuestion or problem

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions