Files
swift-mirror/utils/80+-check
Alex Chan a3483cf731 Python improvements in utils/80+-check
* The original version had an off-by-one error in the line number
  (assuming you use the GitHub model of starting lines at 1).  Fix this
  error, and move to using `enumerate()` instead of tracking the
  `count` variable ourselves.
* Add a comment about the exit codes to the help message.
* Increase clarity in variable/argument names.
2016-06-19 07:52:55 +01:00

54 lines
2.2 KiB
Python
Executable File

#!/usr/bin/env python
from __future__ import print_function
import sys
import textwrap
def get_arguments():
import argparse
parser = argparse.ArgumentParser(textwrap.dedent("""
This is a simple script that takes input from stdin and prints out any
lines that are greater than 80+ characters. In such a case, it will print
out the line number, the length of the line, and the line with whitespace
stripped. The reason why we print out the line with whitespace stripped is
that in the case where there is a lot of redundant whitespace, the output
becomes hard to read and no value is provided in terms of finding the line
in question.
Exits with 1 if it finds any violating lines, 0 otherwise."""))
parser.add_argument('infile', nargs='?', type=argparse.FileType('r'),
default=sys.stdin,
help=textwrap.dedent("""The file to read input from. If
no file is provided, stdin is used."""))
parser.add_argument('-o', '--output', type=argparse.FileType('w'),
default=sys.stdout,
help=textwrap.dedent("""The file to print to. If no
file is provided, stdout is used"""),
dest='outfile')
parser.add_argument('--max-length', type=int, default=80, metavar='LENGTH',
help=textwrap.dedent("""Maximum line length that the
script should check for"""))
parser.add_argument('--count-newline', action='store_false',
help=textwrap.dedent("""Should we include the newline
in our line length count"""))
return parser.parse_args()
args = get_arguments()
found_violation = False
for lineno, line in enumerate(args.infile, start=1):
# sys.stdin.readlines() includes a newline. So we subtract 1 from our
# length to get the "true" line length.
length = len(line) - int(args.count_newline)
if length > args.max_length:
found_violation = True
print("line: {}. length: {}: {}".format(lineno, length, line.strip()),
file=args.outfile)
sys.exit(found_violation)