forked from AmirhosseinDotZip/PyLineCounter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
72 lines (63 loc) · 2.57 KB
/
main.py
File metadata and controls
72 lines (63 loc) · 2.57 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
import argparse
import os
from core.counter import count_lines
from core.utils import parse_extensions
__version__ = '0.1'
def create_parser():
parser = argparse.ArgumentParser(
description='A CLI tool to count lines in files with various filtering options',
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog='''
Examples:
Count lines in current directory:
pylinecounter
Count lines in specific directory:
pylinecounter /path/to/dir
Count lines only in Python files:
pylinecounter -x py
Count lines in Python and JavaScript files:
pylinecounter -x py,js
Exclude specific file types:
pylinecounter -e txt,log
Show tree structure with line counts:
pylinecounter -t
Verbose output with file and character counts:
pylinecounter -v
Limit directory depth:
pylinecounter -d 2
Save output to file:
pylinecounter -o output.txt
'''
)
parser.add_argument('path', nargs='?', default=os.getcwd(),
help='Path to the directory or file to analyze (defaults to current directory)')
parser.add_argument('-x', '--extension', help='File extensions to include (comma-separated)')
parser.add_argument('-t', '--tree', action='store_true', help='Display file structure as tree')
parser.add_argument('-e', '--exclude', help='File extensions to exclude (comma-separated)')
parser.add_argument('-v', '--verbose', action='store_true', help='Show detailed output including character count')
parser.add_argument('--version', action='version', version=f'%(prog)s {__version__}')
parser.add_argument('-i', '--ignore-empty', action='store_true', help='Ignore empty files')
parser.add_argument('-o', '--output', help='Output file path')
parser.add_argument('-d', '--depth', type=int, help='Maximum depth for directory traversal')
return parser
def main():
parser = create_parser()
args = parser.parse_args()
include_extensions = parse_extensions(args.extension) if args.extension else None
exclude_extensions = parse_extensions(args.exclude) if args.exclude else None
result = count_lines(
path=args.path,
include_extensions=include_extensions,
exclude_extensions=exclude_extensions,
show_tree=args.tree,
verbose=args.verbose,
ignore_empty=args.ignore_empty,
max_depth=args.depth
)
if args.output:
with open(args.output, 'w') as f:
f.write(result)
else:
print(result)
if __name__ == '__main__':
main()