[QUESTION] How do I disable typer's exception handler #880
Replies: 6 comments 1 reply
-
to answer my own question after some digging it looks like you can do this.... if you normally have import typer
app = typer.Typer()
if __name__ == '__main__':
app() you can instead do import typer
app = typer.Typer()
if __name__ == '__main__':
command = typer.main.get_command(app)
try:
result = command(standalone_mode=False)
except:
print(f'exception was thrown')
sys.exit(result) |
Beta Was this translation helpful? Give feedback.
-
I dug into the source code and found that app = typer.Typer()
app.command()(main)
app()` # <-- this app takes the standalone_mode argurment |
Beta Was this translation helpful? Give feedback.
-
I saw this question when I was looking at disabling the typer's exception message in case of issue, as it was too verbose.
or to remove the typer exception completely:
|
Beta Was this translation helpful? Give feedback.
-
A disadvantage of setting |
Beta Was this translation helpful? Give feedback.
-
I actually preferd to add a handler for the throws, then I still benefit from more of try:
app()
except SystemExit as ex:
if ex.args[0] != 0:
raise
# else continue, all is well |
Beta Was this translation helpful? Give feedback.
-
[Maintenance note]: moving this to the discussion forum - we can continue the discussion there! 🙏 |
Beta Was this translation helpful? Give feedback.
-
I want to be able to disable typer/Click's exception handler, and handle the exceptions myself.
I see that in click you can do this by setting standalone_mode = False in the command.main() function.
Beta Was this translation helpful? Give feedback.
All reactions