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

Option that is not required and prompts if specified without an arg #736

Closed
johnksterling opened this issue Feb 16, 2017 · 4 comments · Fixed by #1618
Closed

Option that is not required and prompts if specified without an arg #736

johnksterling opened this issue Feb 16, 2017 · 4 comments · Fixed by #1618
Labels
f:prompt feature: prompt for input
Milestone

Comments

@johnksterling
Copy link

Hi -

I am working on a ticket for mycli - the gist is that the team wants the '-p' flag to behave like th e mysql client - i.e. prompt if you don't pass an arg, but it is not required so do NOT prompt if the -p is not included. Today if I use standard click->prompt then scenario 2 gives an error. Below is the desired behavior:

  1. $> ./hello
    HELLO WORLD
  2. $> ./hello -p
    Please enter your password:
  3. $> ./hello -p s3cr3t
    HELLO WORLD

It appears looking at the code there is no way to do this, but it seems desirable - before working on a PR I wanted to validate that it can't be done and get input on the usefulness of the feature.

Thanks for your guidance
John

@fdavis
Copy link
Contributor

fdavis commented May 17, 2018

Thanks for your feedback!

I don't see anyway to do that currently but it definitely seems like a good use case to me.
I think we may need a new option for intance prompt_required to help disambiguate this behavior from the required option behavior

import click

@click.command()
@click.option('--username')
@click.option('-p', '--password', prompt=True, prompt_required=False, hide_input=True)
def echoing(username, password):
    click.echo(username)
    click.echo(password)

echoing()

@reinaldorauch
Copy link

@fdavis I understand where comes from you line of thought but wouldn't be better, semantically speaking, if we create a promp_when_not_declared to evidentiate the current behavior?

Using your example:

import click

@click.command()
@click.option('--username')
# Keeps the current behavior
@click.option('-p', '--password', prompt=True, prompt_when_not_declared=True, hide_input=True)
# Prompts only if passed as argument to the command, maybe change this to be the default, I say why below
@click.option('-p', '--password', prompt=True, prompt_when_not_declared=False, hide_input=True)
def echoing(username, password):
    click.echo(username)
    click.echo(password)

echoing()

When I was reading the docs I was carried to understand that as it is an option, it would only prompt if the option was passed to the command, but it prompts always.

@amy-lei
Copy link
Contributor

amy-lei commented Jun 30, 2020

I'll be working on this issue! Just to clarify:

  1. Should the default be prompt_required=True? From the examples in the doc, it seems like this should be the case. But as reinaldorauch mentions, I would've assumed the opposite since it's an option.
  2. Should the option act as a flag regardless of whether prompt_required is True or False? As of now, running hello -p raises the error Error: -p option requires an argument rather than prompting the user for a value. Or should we keep this behavior for when prompt_required=True

@amy-lei
Copy link
Contributor

amy-lei commented Jul 1, 2020

Discussed with @juped and thought the default should be True to maintain the current behavior of prompts. Then for a similar reason, options should only act as a flag when prompt_required=False and raise an error otherwise.

@davidism davidism added this to the 8.0.0 milestone Aug 6, 2020
@davidism davidism added the f:prompt feature: prompt for input label Aug 6, 2020
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 13, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
f:prompt feature: prompt for input
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants