You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
The text was updated successfully, but these errors were encountered:
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
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 -
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"
Environment:
The text was updated successfully, but these errors were encountered: