Skip to content

Allow --help even if callback has required options / arguments #373

@shustinm

Description

@shustinm

First Check

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

Commit to Help

  • 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    featureNew feature, enhancement or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions