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

@click.Option with 'hide_input' = True (used for password) and custom 'type' parameter doesn't show custom error message. #2809

Open
Rygor83 opened this issue Nov 20, 2024 · 0 comments

Comments

@Rygor83
Copy link

Rygor83 commented Nov 20, 2024

I use @click.Option with 'hide_input' = True and custom 'type' parameter for password prompt with raise click.BadParameter (it inheriths class UsageError). And if the password failed the code doesn't show custom error message, but standard message "Error: The value you entered was invalid"

Standard message comes from termui.py file: ~172 line -

        except UsageError as e:
            if hide_input:
                echo(_("Error: The value you entered was invalid."), err=err)
            else:
                echo(_("Error: {e.message}").format(e=e), err=err)  # noqa: B306
            continue

Because it checks hide_input parameter and if it True (and it is really True as i hide_input because of the password) it give the error "Error: The value you entered was invalid". When hide_input is False - it gives me my custom message.

If i use click.ClickException exception class it give me my error, but stops the promt, so i have to start from the beginning.
If i make hide_input = False - the password is visible on the screen and i recieve my custom message. But it is wrong for password entry.

Here is the example how to replicate my error.
Run the code and enter "1234".
The message i expect "Error: Your password is week. Use 'Minimum eight characters, at least one letter and one number"

import click
import re

class PassRequirement(click.ParamType):
    """Click check class for password"""

    name = "Password requirements"

    def convert(self, value, param, ctx):
        pw_req = "Minimum eight characters, at least one letter and one number"
        pw_strength = r"^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,}$"

        if re.match(pw_strength, value):
            return value

        self.fail(f"Your password is week. Use {pw_req!r}", param, ctx)

    def fail(self, message="", param=None, ctx=None):
        raise click.BadParameter(message, ctx, param)
        # raise click.ClickException(message) - # works well, shows my message, but stops the promt, so i have to start the command from the beginning.

PASS_REQUIREMENT = PassRequirement()

@click.command()
@click.option("-password", help="Password", prompt=True, confirmation_prompt=True, hide_input=True,
              type=PASS_REQUIREMENT)
def pw(password):
    print(f'PASSWORD: {password}')


if __name__ == '__main__':
    pw()

Environment:

  • Python version: 3.12.7
  • Click version: 8.1.7
@Rygor83 Rygor83 changed the title @click.Option with 'hide_input' = True (used for password) and custom 'type' parameter don't show custom error message. @click.Option with 'hide_input' = True (used for password) and custom 'type' parameter doesn't show custom error message. Nov 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant