typer icon indicating copy to clipboard operation
typer copied to clipboard

Allow `--help` even if callback has required options / arguments

Open shustinm opened this issue 2 years ago • 2 comments

First Check

  • [X] I added a very descriptive title to this issue.
  • [ ] 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

import typer

app = typer.Typer()


@app.command('cmd')
def cmd():
    pass


@app.callback()
def main(var: str = typer.Option(...)):
    pass


if __name__ == '__main__':
    app()

Description

If the callback has a required option, running a command under this callback with the --help option will give a "missing option" error.

code.py --help  # works
code.py cmd --help  # missing option '--var'

Here's why I think this behavior is unintended: On typer 0.3.2, regular arguments defined in the callback are not required when passing --help to commands under the callback. Let's change the main function to this:

@app.callback()
def main(var: str):
    pass

Now both invocations provide the help prompt:

code.py --help  # works
code.py cmd --help  # works

Wanted Solution

I would like for typer to print out the usage when passing --help when required options are not passed, just like it prints the usage when required arguments are not passed

Wanted Code

import typer

app = typer.Typer()


@app.command('cmd')
def cmd():
    pass


@app.callback()
def main(var: str = typer.Option(...)):
    pass


if __name__ == '__main__':
    app()

Alternatives

No response

Operating System

macOS

Operating System Details

No response

Typer Version

0.3.2

Python Version

3.8.9

Additional Context

After filling out the version, I checked the behavior with typer 0.4.0 and both argument and option don't print out the usage when running the subcommand with --help

shustinm avatar Mar 23 '22 10:03 shustinm

I guess this is intended behavior, but I'm not sure. At least what I can tell is this: If you change the three dots to Null to make the option optional, you'll have the desired output:

(...)
@app.callback()
def main(var: str = typer.Option(None)):
    pass
(...)

Will generate this output

foo@bar issue373 % python3 main.py cmd --help
Usage: main.py cmd [OPTIONS]

Options:
  --help  Show this message and exit.

BUT this also indicates another issue: Now users are not aware of the --var option 🤔

tibeer avatar Jul 08 '22 11:07 tibeer

Any news on this very "problem" ?

NikosAlexandris avatar Oct 18 '23 12:10 NikosAlexandris