-
Notifications
You must be signed in to change notification settings - Fork 115
Expand file tree
/
Copy path__main__.py
More file actions
48 lines (42 loc) · 1.67 KB
/
Copy path__main__.py
File metadata and controls
48 lines (42 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
"""Command-line debug helper over the 2.0 API.
python -m nameparser "Dr. Juan Q. Xavier de la Vega III"
python -m nameparser --json "Doe, John"
"""
import argparse
import json
from nameparser import parse
def main(argv: list[str] | None = None) -> int:
ap = argparse.ArgumentParser(
prog="nameparser", description="Parse a personal name.")
ap.add_argument("name", help="the name string to parse")
ap.add_argument("--json", action="store_true",
help="print the component dict as JSON")
ap.add_argument("--locale", metavar="CODE",
help="parse with a locale pack (e.g. 'ru'); see "
"nameparser.locales")
args = ap.parse_args(argv)
# `is not None`, not truthiness: --locale "" must reach get() and
# exit 2 listing the codes, not silently fall back to the default
if args.locale is not None:
from nameparser import locales, parser_for
try:
parser = parser_for(locales.get(args.locale))
except KeyError as exc:
ap.error(exc.args[0]) # exits 2, message to stderr
n = parser.parse(args.name)
else:
n = parse(args.name)
if args.json:
print(json.dumps(n.as_dict(), ensure_ascii=False))
return 0
# Label each section: the two reprs are byte-identical for input
# that is already correctly cased, so without labels there is no
# telling which is the parse and which is the capitalized view.
print("Parsed:")
print(repr(n))
print("Capitalized:")
print(repr(n.capitalized()))
print("Initials:", n.initials())
return 0
if __name__ == "__main__":
raise SystemExit(main())