-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathH
66 lines (56 loc) · 1.95 KB
/
H
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
import os
import re
import argparse
def search_files(base_dir, pattern, case_insensitive=False, search_dirs=False):
"""
Search for files or directories matching the given pattern.
Args:
base_dir (str): The base directory to start searching from.
pattern (str): The regex pattern to match file or directory names.
case_insensitive (bool): Whether the search should be case-insensitive.
search_dirs (bool): Whether to include directories in the search.
Returns:
list: A list of matching file or directory paths.
"""
flags = re.IGNORECASE if case_insensitive else 0
regex = re.compile(pattern, flags)
matches = []
for root, dirs, files in os.walk(base_dir):
if search_dirs:
for dir_name in dirs:
if regex.search(dir_name):
matches.append(os.path.join(root, dir_name))
for file_name in files:
if regex.search(file_name):
matches.append(os.path.join(root, file_name))
return matches
def main():
parser = argparse.ArgumentParser(description="A simple file search utility.")
parser.add_argument("pattern", help="The regex pattern to search for.")
parser.add_argument(
"directory",
nargs="?",
default=".",
help="The base directory to start searching from (default: current directory)."
)
parser.add_argument(
"-i", "--ignore-case",
action="store_true",
help="Perform a case-insensitive search."
)
parser.add_argument(
"-d", "--directories",
action="store_true",
help="Search for directories instead of files."
)
args = parser.parse_args()
matches = search_files(
base_dir=args.directory,
pattern=args.pattern,
case_insensitive=args.ignore_case,
search_dirs=args.directories
)
for match in matches:
print(match)
if __name__ == "__main__":
main()