-
First Check
Commit to Help
Example Codeimport typer
app = typer.Typer(no_args_is_help=True)
@app.command()
def foo():
...
@app.command()
def bar():
...
if __name__ == "__main__":
app() Description
Operating SystemmacOS Operating System DetailsVersion 15.5 Typer Version0.16.0 Python VersionPython 3.12.10 Additional Context![]() PR that added support (#1222) seems to have removed an assert on the exit code of a no args help here |
Beta Was this translation helpful? Give feedback.
Replies: 6 comments
-
I encountered exactly the same issue after upgrading to Typer 0.16.0. Version 0.15.4 (with Click < 8.2) was working fine. |
Beta Was this translation helpful? Give feedback.
-
This issue appears to have been introduced in version 0.16.0 with click 8.2 support. < 0.16.0❯ cat test.py
#!/usr/bin/env -S uv run --python 3.13 --script
# /// script
# dependencies = [
# "click<8.2.0",
# "typer<0.16.0"
# ]
# ///
import typer
app = typer.Typer()
@app.command(no_args_is_help=True)
def main(greet: str) -> None:
print(f"Hi, {greet}")
if __name__ == "__main__":
app()
❯ ./test.py
Uninstalled 2 packages in 5ms
Installed 2 packages in 4ms
Usage: test.py [OPTIONS] GREET
╭─ Arguments ──────────────────────────────────────────────────────────────────╮
│ * greet TEXT [default: None] [required] │
╰──────────────────────────────────────────────────────────────────────────────╯
╭─ 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. │
╰──────────────────────────────────────────────────────────────────────────────╯
❯ echo $?
0 == 0.16.0❯ cat test.py
#!/usr/bin/env -S uv run --python 3.13 --script
# /// script
# dependencies = [
# "click==8.2.0",
# "typer==0.16.0"
# ]
# ///
import typer
app = typer.Typer()
@app.command(no_args_is_help=True)
def main(greet: str) -> None:
print(f"Hi, {greet}")
if __name__ == "__main__":
app()
❯ ./test.py
Uninstalled 1 package in 3ms
Installed 1 package in 1ms
Usage: test.py [OPTIONS] GREET
╭─ Arguments ──────────────────────────────────────────────────────────────────╮
│ * greet TEXT [default: None] [required] │
╰──────────────────────────────────────────────────────────────────────────────╯
╭─ 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. │
╰──────────────────────────────────────────────────────────────────────────────╯
Usage: test.py [OPTIONS] GREET
Try 'test.py --help' for help.
╭─ Error ──────────────────────────────────────────────────────────────────────╮
│ │
╰──────────────────────────────────────────────────────────────────────────────╯
❯ echo $?
2 |
Beta Was this translation helpful? Give feedback.
-
@tiangolo can you please check for this issue? Sir |
Beta Was this translation helpful? Give feedback.
-
@tiangolo it seems to be a problem related to this commit on click: pallets/click@d8763b9 This commit introduced an exception named click == 8.1.8 def parse_args(self, ctx: Context, args: list[str]) -> list[str]:
if not args and self.no_args_is_help and not ctx.resilient_parsing:
echo(ctx.get_help(), color=ctx.color)
ctx.exit() click >= 8.2.0 def parse_args(self, ctx: Context, args: list[str]) -> list[str]:
if not args and self.no_args_is_help and not ctx.resilient_parsing:
raise NoArgsIsHelpError(ctx) I have made a pull request that fixes that issue here #1240 |
Beta Was this translation helpful? Give feedback.
-
@SeedyROM also posted an interim workaround to the PR thread at #1240 (comment): # Disable no_args_is_help, and specify the following callback logic instead
@app.callback(invoke_without_command=True)
def main(ctx: typer.Context):
"""My awesome CLI tool"""
if ctx.invoked_subcommand is None:
print(ctx.get_help())
raise typer.Exit() |
Beta Was this translation helpful? Give feedback.
-
Thanks all! This has been fixed by PR #1278 and released as Typer v.0.16.1. |
Beta Was this translation helpful? Give feedback.
Thanks all!
This has been fixed by PR #1278 and released as Typer v.0.16.1.
The issue (only) happened when using Click 8.2.0 or higher.