mirror of
https://github.com/pre-commit/pre-commit-hooks.git
synced 2026-04-06 20:16:53 +00:00
Add hook to check the number of allowed lines
This commit is contained in:
parent
1ca4ceba96
commit
9d0b99f39f
4 changed files with 72 additions and 0 deletions
35
pre_commit_hooks/check_number_of_lines_count.py
Executable file
35
pre_commit_hooks/check_number_of_lines_count.py
Executable file
|
|
@ -0,0 +1,35 @@
|
|||
#!/usr/bin/env python3
|
||||
import argparse
|
||||
from typing import Optional
|
||||
from typing import Sequence
|
||||
|
||||
|
||||
def main(argv: Optional[Sequence[str]] = None) -> int:
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument('filenames', nargs='*')
|
||||
parser.add_argument(
|
||||
'--max-lines',
|
||||
default=30,
|
||||
type=int,
|
||||
action='store',
|
||||
help='Maximum allowable number of lines (default: 30)',
|
||||
)
|
||||
args = parser.parse_args(argv)
|
||||
|
||||
retcode = 0
|
||||
for filename in args.filenames:
|
||||
with open(filename) as file:
|
||||
file_content = file.readlines()
|
||||
number_of_lines = len(file_content)
|
||||
if number_of_lines > args.max_lines:
|
||||
print(
|
||||
f'{filename} ({number_of_lines} lines) exceeds '
|
||||
f'{args.max_lines} lines.',
|
||||
)
|
||||
retcode = 1
|
||||
|
||||
return retcode
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
exit(main())
|
||||
Loading…
Add table
Add a link
Reference in a new issue