Skip to content

Commit 2bb59ac

Browse files
authored
Merge pull request #18 from lukbrew25/feat/command-line-interface
feat: add Command Line Interface
2 parents 043b7a8 + 7404071 commit 2bb59ac

2 files changed

Lines changed: 69 additions & 1 deletion

File tree

README.md

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
# Postal Regex 📨
32

43
[![PyPI version](https://img.shields.io/pypi/v/postal-regex.svg)](https://pypi.org/project/postal-regex/)
@@ -111,6 +110,58 @@ print(df_validated)
111110

112111
---
113112

113+
## Command-Line Usage
114+
115+
You can validate postal codes directly from the command line without writing any code.
116+
117+
### Validate Postal Codes
118+
119+
To validate one or more postal codes for a specific country:
120+
121+
```sh
122+
python -m postal_regex.cli validate <postal_code1> <postal_code2> ... <country>
123+
```
124+
125+
**Examples:**
126+
127+
Validate a single code for India:
128+
```
129+
python -m postal_regex.cli validate 110001 IN
130+
```
131+
Output:
132+
```
133+
110001: Valid
134+
```
135+
136+
Validate multiple codes for the United States:
137+
```
138+
python -m postal_regex.cli validate 12345 90210 US
139+
```
140+
Output:
141+
```
142+
12345: Valid
143+
90210: Valid
144+
```
145+
146+
You can also use country names:
147+
```
148+
python -m postal_regex.cli validate 110001 India
149+
```
150+
151+
### View Validation Statistics
152+
153+
To view local validation statistics:
154+
```
155+
python -m postal_regex.cli stats
156+
```
157+
158+
To reset statistics:
159+
```
160+
python -m postal_regex.cli stats --reset
161+
```
162+
163+
---
164+
114165
## Contributing
115166

116167
We welcome contributions! Please see [CONTRIBUTING.md](CONTRIBUTING.md) for guidelines.

src/postal_regex/cli.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import argparse
22
from . import analytics
3+
from .core import validate
34

45

56
def main():
@@ -15,13 +16,29 @@ def main():
1516
"--reset", action="store_true", help="Reset all recorded statistics."
1617
)
1718

19+
# The 'validate' command
20+
validate_parser = subparsers.add_parser(
21+
"validate", help="Validate postal codes for a given country."
22+
)
23+
validate_parser.add_argument(
24+
"postal_codes", nargs="+", help="Postal code(s) to validate."
25+
)
26+
validate_parser.add_argument(
27+
"country", help="Country code or name to validate against."
28+
)
29+
1830
args = parser.parse_args()
1931

2032
if args.command == "stats":
2133
if args.reset:
2234
analytics.reset_stats()
2335
else:
2436
analytics.show_stats()
37+
elif args.command == "validate":
38+
for code in args.postal_codes:
39+
is_valid = validate(args.country, code)
40+
result = "Valid" if is_valid else "Invalid"
41+
print(f"{code}: {result}")
2542
else:
2643
parser.print_help()
2744

0 commit comments

Comments
 (0)