typer icon indicating copy to clipboard operation
typer copied to clipboard

required=True support [typer.Option]

Open kmcminn opened this issue 3 years ago • 1 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

import typer
app = typer.Typer()

# currently required options look like this:
@app.command()
def main(
    username: str = typer.Option(..., help="A Username")
):
    <code>

Description

Please make typer.Option required arguments conform to a more readable syntax than ellipsis.

From the function param:

username: str = typer.Option(..., help="A Username")

unless you have working knowledge of this library you wouldn't be able to know if:

  • the default value for username is ellipsis
  • would be confused why the default value doesnt form to the type str
  • would probably not know without inspecing typer.Option's signature that the first argument is a default value
  • would not know that an ellipsis as a default would invoke additional option logic

Please consider moving this feature to a kwarg in typer.Option.

Wanted Solution

typer.Option supports a new kwarg: required: bool

Wanted Code

import typer
app = typer.Typer()

# This is a more expressive and readable
@app.command()
def main(
    username: str = typer.Option("", help="A Username", required=True)
):
    <code>

Alternatives

N/A

Operating System

Linux

Operating System Details

No response

Typer Version

0.4.0

Python Version

3.9+

Additional Context

No response

kmcminn avatar Feb 24 '22 17:02 kmcminn

IMO, the current interface is good. Adding the required parameter, would negate the first parameter (default value), which would be weird (the default value is ignored if required=True).

If anything, it should be made clearer in the instructions that to make an Option or Argument required, ... should be the default value.

joaonc avatar May 16 '22 21:05 joaonc

check this out https://typer.tiangolo.com/tutorial/options/required/

Alirezaaraby avatar Nov 12 '22 16:11 Alirezaaraby

I just came across the same problem. I wanted to add a required Option with Choices (Enum). And i didn't want to set a default value, because i wanted that the user gets an error displayed, if he doesn't specify the option. I didn't want default value to execute silently. Hope it makes some sense.

Great package btw ;)

teneon avatar Nov 18 '22 14:11 teneon