-
Notifications
You must be signed in to change notification settings - Fork 205
Expand file tree
/
Copy pathCat_command.py
More file actions
82 lines (51 loc) · 1.25 KB
/
Cat_command.py
File metadata and controls
82 lines (51 loc) · 1.25 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/python
import argparse
from pathlib import Path
from sys import stderr, stdout
import os
class CatError(Exception):
pass
class Logger:
def __init__(self, verbosity=False):
self.verbose = verbosity
def error(self, message):
print(f"ERROR: {message}")
logger = Logger()
"""
Read the selected text file
Example:
your/path/file.txt
"""
def readFile(src: Path):
"""
if the given path is a directory
ERROR the path is a directory
"""
if src.is_dir():
logger.error(f"The path {src}: is a directory")
else:
with open(src, "r") as f:
for lines in f:
print(lines, end="")
def cli() -> argparse.Namespace:
parser = argparse.ArgumentParser(
prog="cat",
description="cat command implementation in python",
epilog="Example: your/path/file.txt",
)
parser.add_argument("source", type=Path, help="Source file")
return parser.parse_args()
def main():
args = cli()
try:
readFile(args.source)
except CatError as e:
logger.error(e)
exit(1)
except KeyboardInterrupt:
logger.error("\nInterrupt")
"""
Start the program
"""
if __name__ == "__main__":
main()