Skip to content

Commit cd94143

Browse files
derek73claude
andcommitted
Reimplement the CLI over the new API with --json
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
1 parent 1748493 commit cd94143

3 files changed

Lines changed: 51 additions & 23 deletions

File tree

nameparser/__main__.py

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,30 @@
1-
"""Command-line debug helper: parse a name and print the result.
2-
3-
Usage:
1+
"""Command-line debug helper over the 2.0 API (migration spec §6).
42
53
python -m nameparser "Dr. Juan Q. Xavier de la Vega III"
4+
python -m nameparser --json "Doe, John"
65
"""
7-
import logging
8-
import sys
6+
import argparse
7+
import json
98

10-
from nameparser import HumanName
9+
from nameparser import parse
1110

1211

13-
def main() -> None:
14-
if len(sys.argv) <= 1:
15-
print('Usage: python -m nameparser "Name String"')
16-
raise SystemExit(1)
17-
log = logging.getLogger('HumanName')
18-
log.setLevel(logging.ERROR)
19-
log.addHandler(logging.StreamHandler())
20-
name_string = sys.argv[1]
21-
hn = HumanName(name_string)
22-
print(repr(hn))
23-
hn.capitalize()
24-
print(repr(hn))
25-
# Use comma rather than concatenation: initials() returns
26-
# empty_attribute_default (possibly None) when there are no initials.
27-
print("Initials:", hn.initials())
12+
def main(argv: list[str] | None = None) -> int:
13+
ap = argparse.ArgumentParser(
14+
prog="nameparser", description="Parse a personal name.")
15+
ap.add_argument("name", help="the name string to parse")
16+
ap.add_argument("--json", action="store_true",
17+
help="print the component dict as JSON")
18+
args = ap.parse_args(argv)
19+
n = parse(args.name)
20+
if args.json:
21+
print(json.dumps(n.as_dict(), ensure_ascii=False))
22+
return 0
23+
print(repr(n))
24+
print(repr(n.capitalized()))
25+
print("Initials:", n.initials())
26+
return 0
2827

2928

30-
if __name__ == '__main__':
31-
main()
29+
if __name__ == "__main__":
30+
raise SystemExit(main())

tests/v2/test_cli.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import json
2+
import subprocess
3+
import sys
4+
5+
6+
def _run(*args: str) -> subprocess.CompletedProcess:
7+
return subprocess.run([sys.executable, "-m", "nameparser", *args],
8+
capture_output=True, text=True)
9+
10+
11+
def test_cli_prints_repr_capitalized_and_initials() -> None:
12+
out = _run("dr. juan de la vega iii").stdout
13+
assert "juan" in out # raw repr first
14+
assert "Juan" in out # capitalized repr second
15+
assert "Initials:" in out
16+
17+
18+
def test_cli_json() -> None:
19+
proc = _run("John Smith", "--json")
20+
data = json.loads(proc.stdout)
21+
assert data["given"] == "John" and data["family"] == "Smith"
22+
23+
24+
def test_cli_no_args_usage() -> None:
25+
proc = _run()
26+
assert proc.returncode != 0

tests/v2/test_layering.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@
7878
# v1 import-path preservation: thin re-exports of the facade/shim
7979
"parser.py": ("nameparser._facade",),
8080
"config/__init__.py": ("nameparser._config_shim",),
81+
# CLI (migration spec §6): imports only the public package, same as
82+
# any other consumer -- no access to internal modules.
83+
"__main__.py": ("nameparser",),
8184
}
8285

8386

0 commit comments

Comments
 (0)