-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Expand file tree
/
Copy pathcli.py
More file actions
51 lines (45 loc) · 1.76 KB
/
Copy pathcli.py
File metadata and controls
51 lines (45 loc) · 1.76 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
49
50
51
import argparse
import sys
from mini_contacts import storage
def _print_table(contacts: list[dict[str, str]]) -> None:
if not contacts:
print("No contacts found.")
return
headers = {"name": "Name", "email": "Email", "phone": "Phone"}
widths = {
field: max(
len(headers[field]), max(len(row[field]) for row in contacts)
)
for field in storage.FIELDNAMES
}
header_line = " | ".join(
headers[f].ljust(widths[f]) for f in storage.FIELDNAMES
)
separator = "-+-".join("-" * widths[f] for f in storage.FIELDNAMES)
print(header_line)
print(separator)
for row in contacts:
print(" | ".join(row[f].ljust(widths[f]) for f in storage.FIELDNAMES))
def main(argv: list[str] | None = None) -> None:
parser = argparse.ArgumentParser(description="Minimal contact manager")
parser.add_argument(
"--path", default="~/.mini-contacts.csv", help="Path to the CSV file"
)
subparsers = parser.add_subparsers(dest="command")
subparsers.required = True
add_parser = subparsers.add_parser("add", help="Add a new contact")
add_parser.add_argument("--name", required=True)
add_parser.add_argument("--email", required=True)
add_parser.add_argument("--phone", required=True)
subparsers.add_parser("list", help="List all contacts")
args = parser.parse_args(argv)
try:
if args.command == "add":
storage.add_contact(args.path, args.name, args.email, args.phone)
print(f"Added contact: {args.name}")
elif args.command == "list":
contacts = storage.read_contacts(args.path)
_print_table(contacts)
except (ValueError, OSError) as e:
print(f"Error: {e}", file=sys.stderr)
sys.exit(1)