|
1 | 1 | #!/usr/bin/env python
|
2 | 2 |
|
3 |
| -from __future__ import print_function |
4 | 3 |
|
| 4 | +from __future__ import print_function, unicode_literals |
| 5 | + |
| 6 | +import argparse |
5 | 7 | import sys
|
6 |
| -import textwrap |
7 |
| - |
8 |
| - |
9 |
| -def get_arguments(): |
10 |
| - import argparse |
11 |
| - |
12 |
| - parser = argparse.ArgumentParser(textwrap.dedent(""" |
13 |
| - This is a simple script that takes input from stdin and prints out any |
14 |
| - lines that are greater than 80+ characters. In such a case, it will print |
15 |
| - out the line number, the length of the line, and the line with whitespace |
16 |
| - stripped. The reason why we print out the line with whitespace stripped is |
17 |
| - that in the case where there is a lot of redundant whitespace, the output |
18 |
| - becomes hard to read and no value is provided in terms of finding the line |
19 |
| - in question. |
20 |
| -
|
21 |
| - Exits with 1 if it finds any violating lines, 0 otherwise.""")) |
22 |
| - |
23 |
| - parser.add_argument('infile', nargs='?', type=argparse.FileType('r'), |
24 |
| - default=sys.stdin, |
25 |
| - help=textwrap.dedent("""The file to read input from. If |
26 |
| - no file is provided, stdin is used.""")) |
27 |
| - parser.add_argument('-o', '--output', type=argparse.FileType('w'), |
28 |
| - default=sys.stdout, |
29 |
| - help=textwrap.dedent("""The file to print to. If no |
30 |
| - file is provided, stdout is used"""), |
31 |
| - dest='outfile') |
32 |
| - parser.add_argument('--max-length', type=int, default=80, metavar='LENGTH', |
33 |
| - help=textwrap.dedent("""Maximum line length that the |
34 |
| - script should check for""")) |
35 |
| - parser.add_argument('--count-newline', action='store_false', |
36 |
| - help=textwrap.dedent("""Should we include the newline |
37 |
| - in our line length count""")) |
| 8 | + |
| 9 | + |
| 10 | +DESCRIPTION = """ |
| 11 | +This is a simple script that takes input from stdin and prints out any lines that are |
| 12 | +greater than 80+ characters. In such a case, it will print out the line number, the |
| 13 | +length of the line, and the line with whitespace stripped. The reason why we print out |
| 14 | +the line with whitespace stripped is that in the case where there is a lot of redundant |
| 15 | +whitespace, the output becomes hard to read and no value is provided in terms of finding |
| 16 | +the line in question. Exits with 1 if it finds any violating lines, 0 otherwise. |
| 17 | +""" |
| 18 | + |
| 19 | + |
| 20 | +def parse_args(): |
| 21 | + parser = argparse.ArgumentParser(description=DESCRIPTION) |
| 22 | + |
| 23 | + parser.add_argument( |
| 24 | + "infile", |
| 25 | + nargs="?", |
| 26 | + type=argparse.FileType("r"), |
| 27 | + default=sys.stdin, |
| 28 | + help="The file to read input from. If no file is provided, stdin is used.", |
| 29 | + ) |
| 30 | + parser.add_argument( |
| 31 | + "-o", |
| 32 | + "--output", |
| 33 | + type=argparse.FileType("w"), |
| 34 | + default=sys.stdout, |
| 35 | + help="The file to print to. If no file is provided, stdout is used", |
| 36 | + dest="outfile", |
| 37 | + ) |
| 38 | + parser.add_argument( |
| 39 | + "--max-length", |
| 40 | + type=int, |
| 41 | + default=80, |
| 42 | + metavar="LENGTH", |
| 43 | + help="Maximum line length that the script should check for", |
| 44 | + ) |
| 45 | + parser.add_argument( |
| 46 | + "--count-newline", |
| 47 | + action="store_false", |
| 48 | + help="Should we include the newline in our line length count", |
| 49 | + ) |
| 50 | + |
38 | 51 | return parser.parse_args()
|
39 | 52 |
|
40 | 53 |
|
41 |
| -args = get_arguments() |
| 54 | +def main(): |
| 55 | + args = parse_args() |
| 56 | + |
| 57 | + found_violation = False |
| 58 | + |
| 59 | + for lineno, line in enumerate(args.infile, start=1): |
| 60 | + # sys.stdin.readlines() includes a newline. So we subtract 1 from our length to |
| 61 | + # get the "true" line length. |
| 62 | + length = len(line) - int(args.count_newline) |
| 63 | + if length > args.max_length: |
| 64 | + found_violation = True |
| 65 | + print( |
| 66 | + "line: {}. length: {}: {}".format(lineno, length, line.strip()), |
| 67 | + file=args.outfile, |
| 68 | + ) |
42 | 69 |
|
43 |
| -found_violation = False |
| 70 | + sys.exit(found_violation) |
44 | 71 |
|
45 |
| -for lineno, line in enumerate(args.infile, start=1): |
46 |
| - # sys.stdin.readlines() includes a newline. So we subtract 1 from our |
47 |
| - # length to get the "true" line length. |
48 |
| - length = len(line) - int(args.count_newline) |
49 |
| - if length > args.max_length: |
50 |
| - found_violation = True |
51 |
| - print("line: {}. length: {}: {}".format(lineno, length, line.strip()), |
52 |
| - file=args.outfile) |
53 | 72 |
|
54 |
| -sys.exit(found_violation) |
| 73 | +if __name__ == "__main__": |
| 74 | + sys.exit(main()) |
0 commit comments