-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Prioritizing full parameter validation before prompting #1369
Comments
Prompting (on groups) is also performed even tough user only requests for help to be printed: $ my-program subgroup command --help
Username:
# ..
Usage: my-program subgroup command [OPTIONS]
# .. |
I believe my issue is related to this: I have a lot of pompt=True options aswell for my main group and subcommands. When invoking Expected behaviour would be to not prompt and just show the "--help" text. |
Here's how Click's processing pipeline works:
An argument with The order of processing matters, because each parameter and command can affect the values that later processing sees. Each subcommand does its own parsing after the previous parent command, so you could still end up in a situation where the parent command doesn't have extra args, does a prompt, then the subcommand errors due to an extra arg. This behavior is the same thing that leads to the confusion over The request in the title is different that the request in the description as well. The title asks that all parameters that don't require a different source for their value be processed before all that do, specifically with all prompts happening last. That might be doable, but it would be a significant change to the processing pipeline. The usual advice here is to move prompting to the command callback instead of the parameter parsing. This allows the parsing and processing to finish (and error) first, then allows more complex prompting/processing in user code. import click
@click.command()
@click.option('--name')
def cli(name):
if name is None:
name = click.prompt("Name: ")
click.echo(name)
cli() |
I am faced with the following problem. Imagine the following dummy script:
When invoked with an unexpected argument, e.g.
test.py extra_argument
instead of failing straight away, first the prompt is invoked. When the prompt is responded to, only then does the command fail:Now for a single option, this is not a big deal. However, in our application we have a command with many options with prompt. If the user accidentally pass an argument, they first have to respond to all the prompts only for the command to error out straight after and they have to start all over again.
Is there any way to prioritize parsing of all parameters before hitting prompts of incomplete ones?
The text was updated successfully, but these errors were encountered: