-
-
Notifications
You must be signed in to change notification settings - Fork 672
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Prompt only if --option
's argument
isn't provided
#431
Comments
prompt
if --option
's argument
isn't providedprompt
**only** if --option
's argument
isn't provided
prompt
**only** if --option
's argument
isn't providedprompt
only if --option
's argument
isn't provided
My bad, I do not understand this behavior: ~/test via 🐍 v3.10.5 (test)
➜ python -m test_typer --password "def"
Password: abc # why prompt at all?
Username: admin | Password: def
~/test via 🐍 v3.10.5 (test)
➜ python -m test_typer
Password: def
def [default]: 123 # why? what's this?!
Username: admin | Password: 123
~/test via 🐍 v3.10.5 (test)
➜ python -m test_typer --password
Password: 123 # why not read from here?
Error: Option '--password' requires an argument Can someone please explain? |
prompt
only if --option
's argument
isn't provided--option
's argument
isn't provided
I think something like this could give you what you want to achieve, if I've understood correctly. Instead of specifying the import typer
def main(
user: str,
password: str = typer.Option("", help="Enter your password."),
):
if password:
print(f"{user} and {password}")
else:
new_password = typer.prompt("Enter your password", confirmation_prompt=True, hide_input=True)
print(f"user is: {user} and password is: {new_password}")
if __name__ == "__main__":
typer.run(main) As far as I can tell, if you add an option, then use the option when you are running the program on the command line, you have to specify some value for that option - i.e. there is no way to leave it blank. So I don't think the way you're trying to solve this is possible. |
Yup, I've done something like this.
Right, but it feels counter intuitive when used with |
import typer
cli = typer.Typer()
@cli.command()
def req(
username: str = "admin",
password: str = typer.Option("default", prompt=True),
) -> None:
print(f"Username: {username} | Password: {password}")
cli() Output: $ python /tmp/asd/potato/potato/main.py
Password [default]: hello
Username: admin | Password: hello |
Thanks @heiskane! |
First Check
Commit to Help
Example Code
Description
Assume that the above code runs directly with command
req
I would prefer if it were possible to providepassword
via prompt and not on commandlineAdditionlly, usinggetpass
&getuser
seems more favourable.When I provide a command like:
It prompts, but afterword it runs into error:
Password: Error: Option '--password,' requires an argument
Wanted Solution
Prompt for (here) password only when
--password
is passed w/o any argument.Wanted Code
# NAY
Alternatives
NA
Operating System
Linux
Operating System Details
Arch Linux
Kernel: 5.18
Typer Version
0.6.1
Python Version
3.10.5
Additional Context
No response
The text was updated successfully, but these errors were encountered: