Scripts2026年5月26日·1 分钟阅读

cloc — Count Lines of Code Across Any Project

cloc counts blank lines, comment lines, and physical lines of source code in over 250 programming languages from the command line.

Agent 就绪

Agent 可直接安装

这个资产可安装;Agent 先选择当前运行时、检查安装计划,再运行匹配命令。

Native · 98/100策略:允许
Agent 入口
任意 MCP/CLI Agent
类型
Skill
安装
Single
信任
信任等级:Established
入口
cloc Overview
直接安装命令
npx -y tokrepo@latest install 4546dbbf-5920-11f1-9bc6-00163e2b0d79 --target codex

先 dry-run 确认安装计划,再运行此命令。

Introduction

cloc (Count Lines of Code) is a portable command-line utility that tallies blank lines, comment lines, and physical source lines across more than 250 programming languages. It is widely used in code audits, migration planning, and engineering dashboards.

What cloc Does

  • Counts code, comments, and blank lines for 250+ languages
  • Diffs two directory trees to show added, removed, and modified lines per language
  • Processes files, directories, compressed archives, and git commit ranges
  • Outputs plain text, JSON, XML, YAML, CSV, or SQL insert statements
  • Filters by language, extension, directory, or custom regex

Architecture Overview

cloc is a single self-contained Perl script. It walks the file tree, identifies each file's language via extension tables and shebang heuristics, strips comments using language-specific regex rules, and accumulates per-language tallies. Diff mode pairs files between two trees and reports net changes. No daemon or database is involved.

Self-Hosting & Configuration

  • Install via package manager (apt, brew, choco), npm, or download the standalone Perl script
  • Runs as a one-shot CLI with no service or config file required
  • Use a .clocignore file to exclude paths, similar to .gitignore
  • Pass --exclude-dir, --exclude-lang, or --exclude-ext for fine-grained filtering
  • Add custom language definitions with --read-lang-def or --force-lang-def

Key Features

  • Zero-dependency single Perl file that runs on any OS with Perl installed
  • Recognizes 250+ programming and markup languages out of the box
  • Diff mode compares two codebases and reports net line changes per language
  • Multiple output formats (JSON, YAML, XML, CSV, SQL) for CI pipeline integration
  • Handles tar/zip archives and git commit ranges without manual extraction

Comparison with Similar Tools

  • Tokei — Rust-based, significantly faster on large repos but fewer output format options
  • scc — Go-based, adds COCOMO cost estimates and complexity metrics
  • SLOCCount — Early predecessor to cloc, now unmaintained
  • loc — Minimal Rust counter, fast but limited language coverage
  • wc -l — Unix built-in that counts raw lines without language awareness

FAQ

Q: How accurate is cloc for polyglot repositories? A: cloc identifies languages by file extension and shebang lines. Ambiguous cases like .h files use heuristics. Override with --force-lang.

Q: Can cloc count lines inside a git history range? A: Yes. Use cloc --git --diff v1.0 v2.0 to compare two tags, branches, or commits.

Q: Does cloc count generated or vendored code? A: By default yes. Use --exclude-dir=vendor,node_modules or a .clocignore to skip them.

Q: How does cloc handle binary files? A: cloc auto-detects and skips binary files. Use --binary-check to tune the heuristic threshold.

Sources

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产