class

Ignorelint::CLI

Inherits Reference < Object

The command-line interface class.

Design

The CLI is a regular class (not a struct) because it holds mutable state: the parsed options (@paths, @fail_on, @format, @fix, @verbose). A single instance is created per invocation and orchestrates the entire lint run.

Exit codes

  • 0: no issues at or above the --fail-on threshold
  • 1: issues found at or above the threshold
  • 2: invalid CLI arguments (unknown flags, bad values)

Crystal note: private constants

SEVERITY_WIDTH is a private constant (uppercase = constant in Crystal). It is defined on the class, not the module. Crystal's private on a constant restricts visibility to the defining type.

Constructors

new(io : IO, err : IO = STDERR)

Initialize the CLI with an output stream.

Detects TTY status; option defaults resolve later in run so that explicit flags beat environment, which beats the projectfile subtree. Crystal's responds_to?(:tty?) is a type-safe way to check if the IO supports TTY detection (not all IO types do — StringIO does not).

Class methods

color_enabled?(tty : Bool) : Bool

TTY plus empty-or-unset NO_COLOR means color.

run(args : Array(String), io : IO = STDOUT, err : IO = STDERR) : Nil

Convenience entry point: create an instance and run.

Separates construction from execution so tests can inject a custom IO. The io parameter defaults to STDOUT but can be replaced with a StringIO for testing.

Instance methods

run(args : Array(String), input : IO = STDIN) : Int32

Main execution: parse flags, discover files, lint, format.

Returns the process exit code (0 = clean) instead of exiting, so specs can assert on it; self.run performs the actual exit.

The flow is:

  1. Build and parse the option parser (consumes flags from args)
  2. Load policy (explicit --config, else cwd projectfile discovery)
  3. Apply projectfile, then environment overrides (flags always win)
  4. Build the output formatter
  5. Determine which files to lint (explicit paths or auto-discovery)
  6. Lint each file, collecting the exit code
  7. Emit formatted output
  8. Return non-zero if issues were found above the threshold