Skip to main content

style50

This is style50, a tool with which code can be checked against the CS50 style guide.

Installation

pip install style50

In order to style check C, C++, or Java code, a recent version (>=14.0.0) of clang-format must be installed. clang-format may be downloaded here.

Windows

Along with most of CS50's command line tools, style50 supports being run on Windows but only via the Linux Subsystem in Windows 10. After launching it, style50 can be installed using the pip command above.

Usage

usage: style50 [-h] [-o MODE] [-y] [-i] [-v] [-V] [-E] [--ignore PATTERN]
               [--clang-format-style STYLE]
               FILE [FILE ...]

positional arguments:
FILE                  file or directory to lint

options:
-h, --help            show this help message and exit
-o, --output MODE     output mode, which can be character (default), split,
                      unified, score, json, html, or format
-y, --side-by-side    show side-by-side diff (equivalent to -o split)
-i, --in-place        rewrite files in place with style50 formatting
-v, --verbose         print full tracebacks of errors
-V, --version         show program's version number and exit
-E, --extensions      print supported file extensions (as JSON list) and
                      exit
--ignore PATTERN      paths/patterns to be ignored
--clang-format-style STYLE
                      clang-format style string or file:// URI (overrides
                      default CS50 config)

STYLE50_IGNORE is also supported as a comma-separated environment variable fallback for ignore patterns.

character, split, and unified modes output character-based, side-by-side, and unified (respectively) diffs between the inputted file and the correctly styled version. score outputs the raw percentage of correct (unchanged) lines, json outputs a JSON object containing structured results, html outputs browser-readable results, and format outputs only the formatted code.

Common examples

# Diff-only mode (default)
style50 foo.c
style50 foo.c bar.py
style50 *.c *.py

# In-place rewrite
style50 -i foo.c
style50 -i foo.c bar.py
style50 -i *.c *.py

# Side-by-side diff
style50 -y foo.c bar.py

# Ignore files/patterns
style50 --ignore "*/.*" foo.c bar.py

# Emit only formatted code (single file only)
style50 -o format foo.c

--output modes

--output supports:

  • character (default)
  • split
  • unified
  • score
  • json
  • html
  • format

-i and --output

-i/--in-place rewrites files and therefore cannot be combined with --output or --side-by-side.

Language Support

style50 currently supports the following languages:

  • C++
  • C
  • Python
  • Javascript
  • Java
  • HTML
  • CSS
  • SQL

Adding a new language

Adding a new language is very simple. Language checks are encoded as classes which inherit from the StyleCheck base class (see style50/languages.py for more real-world examples). The following is a template for style checks which allows style50 to check the imaginary FooBar language for style.

import re

from style50 import StyleCheck, Style50


class FooBar(StyleCheck):

    # REQUIRED: this property informs style50 what file extensions this
    # check should be run on (in this case, all .fb and .foobar files)
    extensions = ["fb", "foobar"]

    # REQUIRED: should return a correctly styled version of `code`
    def style(self, code):
        # All FooBar code is perfectly styled
        return code

    # OPTIONAL: should return the number of comments in `code`.
    # If this function is not defined, `style50` will not warn the student about
    # too few comments
    def count_comments(self, code):
        # A real-world, check would need to worry about not counting '#' in string-literals
        return len(re.findall(r"#.*", code))

All classes which inherit from StyleCheck are automatically registered with style50's Style50 class, making style50 easily extensible. Adding the following to the above code creates a script which checks the code that style50 already does as well as FooBar programs.

    # Style check the current directory, printing a unified diff
    Style50("unified").run(["."])

Metadata

Release files for style50 3.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for style50 3.0.0
File Size Uploaded
style50-3.0.0.tar.gz 28.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for style50 3.0.0
File Interpreter ABI Platform
style50-3.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 55.6 kB

Release files / style50-3.0.0.tar.gz

Download URL style50-3.0.0.tar.gz
Size 28.9 kB
Tags Source
SHA-256 checksum
How to use checksums
225ab8fa0deabd4d157d64442cbbcc6d6d8ded538a4c9af8402db0f233d5a99f
BLAKE2b-256 checksum
How to use checksums
acdd171b685bfb0dcd8d3993d9636f961d72c6a2e637108379523dd0f0987e35
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.7

Release files / style50-3.0.0-py3-none-any.whl

Download URL style50-3.0.0-py3-none-any.whl
Size 26.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4f9768ff3326bbb1916c26935b375a020d4cbcc960d6c5dd0e782e7175610421
BLAKE2b-256 checksum
How to use checksums
2644b49a0900b88415acceacd773c6495a669638e6baee3d04735b6af9565690
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.7

Release history Release notifications | RSS feed

This release

3.0.0 This release

2 release files

2.11.0

2 release files

2.9.1

2 release files

2.9.0

2 release files

2.8.2

2 release files

2.8.1

2 release files

2.8.0

2 release files

2.7.7

2 release files

2.7.6

2 release files

2.7.5

2 release files

2.7.4

1 release file

2.7.3

1 release file

2.7.2

1 release file

2.7.1

1 release file

2.7.0

1 release file

2.6.7

1 release file

2.6.6

1 release file

2.6.4

1 release file

2.6.2

1 release file

2.6.1

1 release file

2.6.0

1 release file

2.5.0

1 release file

2.4.2

1 release file

2.4.1

1 release file

2.4.0

1 release file

2.3.3

1 release file

2.3.2

1 release file

2.3.1

1 release file

2.3.0

1 release file

2.2.0

1 release file

2.1.6

1 release file

2.1.5

1 release file

2.1.4

1 release file

2.1.3

1 release file

2.1.2

1 release file

2.1.1

1 release file

2.1.0

1 release file

2.0.0

1 release file

0.0.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page