Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP


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

merged 1 commit into from

2 participants


We use an 80 character line length at Maginatics, and I'd like ocstyle to enforce that. This pull request adds support for specifying the maximum line length on the command-line. I'm open to any suggestions and improvements you may have.

(Thanks for open-sourcing ocstyle! I'm a big believer in automating these sort of processes.)

@robbywalker robbywalker merged commit b3e5fa5 into from

Looks great, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jul 1, 2013
  1. @vibhav
This page is out of date. Refresh to see the latest.
Showing with 14 additions and 10 deletions.
  1. +12 −6 src/ocstyle/
  2. +2 −4 src/ocstyle/
18 src/ocstyle/
@@ -15,6 +15,7 @@
"""Basic Objective C style checker."""
+import argparse
import os.path
import sys
@@ -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 =
- 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')]
@@ -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
6 src/ocstyle/
@@ -30,14 +30,12 @@
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
@@ -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:
'LineTooLong', 'Line too long: %d chars over the %d limit' % (lineLength, MAX_LINE_LENGTH),
LINES[lineNo], LINES))
Something went wrong with that request. Please try again.