Password CLI Option and Confirmation Prompt
Apart from having a prompt, you can make a CLI option have a confirmation_prompt=True
:
import typer
from typing_extensions import Annotated
def main(
name: str,
email: Annotated[str, typer.Option(prompt=True, confirmation_prompt=True)],
):
print(f"Hello {name}, your email is {email}")
if __name__ == "__main__":
typer.run(main)
Tip
Prefer to use the Annotated
version if possible.
import typer
def main(
name: str, email: str = typer.Option(..., prompt=True, confirmation_prompt=True)
):
print(f"Hello {name}, your email is {email}")
if __name__ == "__main__":
typer.run(main)
And the CLI program will ask for confirmation:
A Password promptΒΆ
When receiving a password, it is very common (in most shells) to not show anything on the screen while typing the password.
The program will still receive the password, but nothing will be shown on screen, not even ****
.
You can achieve the same using hide_input=True
.
And if you combine it with confirmation_prompt=True
you can easily receive a password with double confirmation:
import typer
from typing_extensions import Annotated
def main(
name: str,
password: Annotated[
str, typer.Option(prompt=True, confirmation_prompt=True, hide_input=True)
],
):
print(f"Hello {name}. Doing something very secure with password.")
print(f"...just kidding, here it is, very insecure: {password}")
if __name__ == "__main__":
typer.run(main)
Tip
Prefer to use the Annotated
version if possible.
import typer
def main(
name: str,
password: str = typer.Option(
..., prompt=True, confirmation_prompt=True, hide_input=True
),
):
print(f"Hello {name}. Doing something very secure with password.")
print(f"...just kidding, here it is, very insecure: {password}")
if __name__ == "__main__":
typer.run(main)
Check it: