-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
52 lines (48 loc) · 2.28 KB
/
main.py
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
import argparse
import sys
import colorama
# Initialize colorama for cross-platform ANSI color support
colorama.init()
def main():
parser = argparse.ArgumentParser(description="A program similar to Linux grep.")
parser.add_argument("filename", help="Path to the input file.")
parser.add_argument("search", help="String to search for.")
parser.add_argument("-n", action="store_true", help="Display line numbers.")
parser.add_argument("-s", action="store_true", help="Strip leading and trailing spaces.")
parser.add_argument("-e", action="store_true", help="exclude the part/string provided.")
parser.add_argument("-v", action="store_true", help="prints the version of the program.")
args = parser.parse_args()
try:
with open(args.filename, "r") as file:
for line_no, line in enumerate(file, start=1):
if args.s:
line = line.strip() # Remove leading and trailing spaces
if args.e:
if args.search.lower() not in line.lower():
highlighted_line = line
if args.n and args.s:
print(f"{line_no:<4} : {highlighted_line}")
elif args.n:
print(f"{line_no:<4} : {highlighted_line}", end="")
elif args.s:
print(highlighted_line)
else:
print(highlighted_line, end="")
elif args.search.lower() in line.lower():
# Highlight matching words
highlighted_line = line.replace(args.search, f"{colorama.Fore.RED}{args.search}{colorama.Style.RESET_ALL}")
if args.n and args.s:
print(f"{line_no:<4} : {highlighted_line}")
elif args.n:
print(f"{line_no:<4} : {highlighted_line}", end="")
elif args.s:
print(highlighted_line)
else:
print(highlighted_line, end="")
except FileNotFoundError:
print(f"Error: File '{args.filename}' not found.")
if __name__ == "__main__":
if '-v' in sys.argv:
print("grep v1.0.0")
sys.exit()
main()