Unexpected behavior of no_args_is_help with 0 or 1 commands #937
-
First Check
Example CodeExample 1I expect help when invoked with no args import typer
cli = typer.Typer(no_args_is_help=True)
if __name__ == "__main__":
cli() Actually raises an exception
Example 2I expect help when invoked with no args import typer
cli = typer.Typer(no_args_is_help=True)
@cli.command("1")
def _1():
print(1)
if __name__ == "__main__":
cli() Actually prints "1"
Example 3I expect help when invoked with no args import typer
cli = typer.Typer(no_args_is_help=True)
@cli.command("1")
def _1():
print(1)
@cli.command("2")
def _2():
print(2)
if __name__ == "__main__":
cli() Works as expected (though this breaks code relying on the behaviour of the 1-command typer app)
DescriptionWhen When 2+ commands are registered, However, there are two cases where this does not hold:
Operating SystemmacOS Typer Version0.6.1 Python Version3.9.12 |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
I solved this for myself with this gist. |
Beta Was this translation helpful? Give feedback.
-
Thanks for the report! Let's think about each case individually. 2 commandsSo, the case with 2 commands is working as expected, right? 1 commandFor the case of one command, it's currently working as intended, at least that's how it was designed from the beginning. When there's an app with a single command, that command is called directly, there's no subcommand created for it. It's documented here: https://typer.tiangolo.com/tutorial/commands/#explicit-application If you want it to be a subcommand you would need to do one of:
import typer
cli = typer.Typer(no_args_is_help=True)
@cli.command("1")
def _1():
print(1)
@cli.callback()
def callback():
pass
if __name__ == "__main__":
cli() Then: $ python demo.py
Usage: demo.py [OPTIONS] COMMAND [ARGS]...
╭─ 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 ───────────────────────────────────────────────────────────────╮
│ 1 │
╰──────────────────────────────────────────────────────────────────────────╯ no commandI would actually consider an app with no command or callback to be an error, telling me that there's no command to run, not a help message telling me that the only thing I can do is get that help message. 🤔 |
Beta Was this translation helpful? Give feedback.
Thanks for the report!
Let's think about each case individually.
2 commands
So, the case with 2 commands is working as expected, right?
1 command
For the case of one command, it's currently working as intended, at least that's how it was designed from the beginning. When there's an app with a single command, that command is called directly, there's no subcommand created for it. It's documented here: https://typer.tiangolo.com/tutorial/commands/#explicit-application
If you want it to be a subcommand you would need to do one of: