Skip to content

Conversation

@sushilk123
Copy link

Add EMAIL parameter type for email validation

This PR adds a new EmailParamType to Click for validating email addresses in command-line applications.

Features

  • Email validation using a simple but effective regex pattern
  • Clear error messages for invalid email addresses
  • Consistent API following existing parameter type patterns
  • Comprehensive tests covering valid/invalid cases and edge cases
  • Example usage demonstrating the EMAIL type
  • Proper documentation with version info

Usage

python
import click

@click.command()
@click.option('--email', type=click.EMAIL)
def send_message(email):
click.echo(f"Sending to: {email}")

Validation Examples

Implementation Details

  • Follows the same patterns as existing types like UUID
  • Uses a simple but effective regex: ^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$
  • Includes proper error handling, whitespace trimming, and type conversion
  • Comprehensive test suite with 6 test functions covering all scenarios

Files Changed

  • src/click/types.py - Added EmailParamType class and EMAIL constant
  • src/click/__init__.py - Exported EMAIL for public API
  • tests/test_types.py - Added comprehensive test suite
  • examples/email_example.py - Added usage example

This addresses the common need for email validation in CLI applications while maintaining Click's design principles.

Sushil Khairnar added 2 commits October 14, 2025 16:22
- Add EmailParamType class with regex-based email validation
- Export EMAIL constant for easy usage
- Include comprehensive test suite covering valid/invalid cases
- Add example demonstrating EMAIL type usage
- Support standard email formats with proper error messages

The EMAIL type validates email addresses using a simple but effective
regex pattern and provides clear error messages for invalid inputs.
Follows the same patterns as existing parameter types like UUID.
- Reject emails with leading/trailing dots (.user@, user.@)
- Reject emails with consecutive dots (domain..com)
- Still allow valid formats with + symbols and proper dots
- All tests now pass
@sushilk123 sushilk123 force-pushed the add-email-param-type branch from 8fdbe71 to ade8b3c Compare October 14, 2025 21:02
@ThiefMaster
Copy link
Member

I don't think this is useful in click. If someone needs this, they can implement this in their application and use a proper email validator. I'm quite certain that the current regex excludes valid emails, e.g. anything containing iternational domain names.

@ThiefMaster
Copy link
Member

(I also have the feeling this PR is AI-generated. Don't do that.)

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 29, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants