Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow users to specify maximum line length on the command-line. #1

Merged
merged 1 commit into from Jul 1, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 12 additions & 6 deletions src/ocstyle/main.py
Expand Up @@ -15,6 +15,7 @@

"""Basic Objective C style checker."""

import argparse
import os.path
import sys

Expand All @@ -23,16 +24,16 @@
from ocstyle import rules


def check(path):
def check(path, maxLineLength):
"""Style checks the given path."""
with open(path) as f:
return checkFile(path, f)
return checkFile(path, f, maxLineLength)


def checkFile(path, f):
def checkFile(path, f, maxLineLength):
"""Style checks the given file object."""
content = f.read()
lineErrors = rules.setupLines(content)
lineErrors = rules.setupLines(content, maxLineLength)
result = parcon.Exact(rules.entireFile).parse_string(content)
if path.endswith(('.m', '.mm')):
result = [err for err in result if not isinstance(err, rules.Error) or not err.kind.endswith('InHeader')]
Expand All @@ -43,10 +44,15 @@ def checkFile(path, f):

def main():
"""Main body of the script."""
for filename in sys.argv[1:]:

parser = argparse.ArgumentParser()
parser.add_argument("--maxLineLength", action="store", type=int, default=120, help="Maximum line length")
args, filenames = parser.parse_known_args()

for filename in filenames:
if not os.path.isdir(filename):
print filename
for part in check(filename):
for part in check(filename, args.maxLineLength):
if isinstance(part, rules.Error):
print 'ERROR: %s' % part
else:
Expand Down
6 changes: 2 additions & 4 deletions src/ocstyle/rules.py
Expand Up @@ -30,14 +30,12 @@

TAB_SIZE = 4

MAX_LINE_LENGTH = 120

LINES = []

# PyLint has a very hard time with our decorator pattern. # pylint: disable=E1120


def setupLines(content):
def setupLines(content, maxLineLength):
"""Setup line position data."""
LINES[:] = []
pos = -1
Expand All @@ -51,7 +49,7 @@ def setupLines(content):
errors = []
for lineNo in range(1, len(LINES)):
lineLength = LINES[lineNo] - LINES[lineNo - 1] - 1 # Remove the \n character.
if lineLength > MAX_LINE_LENGTH:
if lineLength > maxLineLength:
errors.append(Error(
'LineTooLong', 'Line too long: %d chars over the %d limit' % (lineLength, MAX_LINE_LENGTH),
LINES[lineNo], LINES))
Expand Down