Skip to content
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

Closed
7 tasks done
yozachar opened this issue Jul 19, 2022 · 5 comments
Closed
7 tasks done

Prompt only if --option's argument isn't provided #431

yozachar opened this issue Jul 19, 2022 · 5 comments
Labels
feature New feature, enhancement or request

Comments

@yozachar
Copy link

yozachar commented Jul 19, 2022

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

After submitting this, I commit to one of:

  • Read open issues with questions until I find 2 issues where I can help someone and add a comment to help there.
  • I already hit the "watch" button in this repository to receive notifications and I commit to help at least 2 people that ask questions in the future.
  • Implement a Pull Request for a confirmed bug.
  • I commit to help with one of those options 👆

Example Code

import typer


cli = typer.Typer()


@cli.command()
def req(
    username: str = 'admin',
    password: str = typer.Option(
        'default',
        prompt=typer.prompt('Password'), # True,
        # prompt_required=True,
    )
) -> None:
    print(f'Username: {username} | Password: {password}')

cli()

Description

  • Assume that the above code runs directly with command req

  • I would prefer if it were possible to provide password via prompt and not on commandline

  • Additionlly, using getpass & getuser seems more favourable.

  • When I provide a command like:

    $ req --username myuser --password

    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

@yozachar yozachar added the feature New feature, enhancement or request label Jul 19, 2022
@yozachar yozachar changed the title Flag to prompt if --option's argument isn't provided Flag to prompt **only** if --option's argument isn't provided Jul 19, 2022
@yozachar yozachar changed the title Flag to prompt **only** if --option's argument isn't provided Flag to prompt only if --option's argument isn't provided Jul 19, 2022
@yozachar
Copy link
Author

yozachar commented Jul 19, 2022

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?

@yozachar yozachar changed the title Flag to prompt only if --option's argument isn't provided Prompt only if --option's argument isn't provided Jul 19, 2022
@osintalex
Copy link

I think something like this could give you what you want to achieve, if I've understood correctly. Instead of specifying the --password option and leaving it blank, you just don't use that option at all.

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.

@yozachar
Copy link
Author

yozachar commented Aug 4, 2022

Yup, I've done something like this.

... 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 ...

Right, but it feels counter intuitive when used with prompt

@heiskane
Copy link

prompt actually takes in a boolean so i believe that what you actually want it:

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

@yozachar
Copy link
Author

Thanks @heiskane!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature, enhancement or request
Projects
None yet
Development

No branches or pull requests

3 participants