typer icon indicating copy to clipboard operation
typer copied to clipboard

Bypass all callbacks, parameter validation when '--help' is requested on any command/subcommand

Open Nitinsiwach opened this issue 2 years ago • 2 comments

First Check

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

Commit to Help

  • [X] I commit to help with one of those options 👆

Example Code

N/A

Description

Thank you for this great package. It has helped me immensely Issue: When --help is requested it does not make sense (in my opinion) to validate the parameters provided, run app callbacks, parameters callbacks on defaults etc. Let us short-circuit everything and just provide help?

Wanted Solution

--help provides only help without any side-effects

Wanted Code

N/A

Alternatives

No response

Operating System

Linux

Operating System Details

No response

Typer Version

0.4.0

Python Version

3.9.7

Additional Context

No response

Nitinsiwach avatar Dec 28 '21 22:12 Nitinsiwach

I haven't seen this when I created my issue: #373, so I duplicated this one basically 👍

shustinm avatar Mar 23 '22 10:03 shustinm

I'm very much interested in this one. I'm using a callback on the main command level to set a context value before continuing to a lower command level. The context value I'm setting uses the typer prompt. The issue is that the user always gets prompted even when specifying --help. Example:

# mycli.py
import typer

import subcommand

def main_callback(ctx: typer.Context):
    ctx.ensure_object(dict)
    ctx.obj['username'] = typer.prompt('username')

app = typer.Typer()
app.add_typer(subcommand.app, name='subcommand', callback=main_callback)

if __name__ == '__main__':
    app()
# subcommand
import typer

app = typer.Typer()

@app.command()
def get(ctx: typer.Context, my_arg: str):
    print(ctx.obj['username'])

python mycli.py subcommand get --help prompts me for my username instead of providing the help for the get subcommand

If I had a way to manually check if --help was passed, I would do my own check and then return from the main_callback early to trigger printing the help. However, I couldn't find a way to do this in the documentation. If anyone knows of a way, I would gladly implement as a work around.

nbarton915 avatar May 17 '22 23:05 nbarton915