当前位置:网站首页>wc 统计已过时,cloc 每一行代码都有效
wc 统计已过时,cloc 每一行代码都有效
2022-06-23 11:32:00 【李三十一】
上下文
我们通常用代码量来评判一个程序员技术的高低或一个项目的大小,阅读新项目源码时,也需要了解其代码量,心里有个预期。
那如何统计代码量呢?
传统做法
通过find+wc命令完成
$ find . -name "*.go" | xargs wc -l
60 ./etcd/tutorial/distributed_lock.go
...
3491 total
如上,统计命令显示了所有文件,一共有 3491 行代码。但是此处统计太过粗暴,wc 统计过程将注释、空白行等内容都被算作代码统计其中。
我们应该尝试更有效的统计方式。
有效统计 cloc
cloc(Count Lines of Code[0]) 是一个 Perl 语言开发的开源代码统计工具。支持多平台、多语言、分类别的统计目标文件或文件夹中源代码的文件数、空白行数、注释行数和代码行数。
安装
支持多种安装方式,几乎包含常用的所有安装方式。
npm install -g cloc # https://www.npmjs.com/package/cloc
sudo apt install cloc # Debian, Ubuntu
sudo yum install cloc # Red Hat, Fedora
sudo dnf install cloc # Fedora 22 or later
sudo pacman -S cloc # Arch
sudo emerge -av dev-util/cloc # Gentoo https://packages.gentoo.org/packages/dev-util/cloc
sudo apk add cloc # Alpine Linux
doas pkg_add cloc # OpenBSD
sudo pkg install cloc # FreeBSD
sudo port install cloc # macOS with MacPorts
brew install cloc # macOS with Homebrew
choco install cloc # Windows with Chocolatey
scoop install cloc # Windows with Scoop
cloc 优势?
cloc 具有易于使用、可扩展和可移植的特性:
安装方便,只需下载文件包运行即可。 支持分类别统计多种语言:Java、Go、Python、C 等。 支持分类别统计多种格式的结果:纯文本、SQL、JSON、XML、YAML、逗号分隔值等。 支持分类别统计文件数、空白行数、注释行数和代码行数。
基本使用
$ cloc ./ # 可统计目录
$ cloc perl-5.22.0.tar.gz # 也可直接统计压缩包
对于前端同学,统计代码往往需要忽略最大的 node_modules 以及打包生成的 dist 文件,支持过滤逻辑,使用非常方便。
$ cloc $(git ls-files)
# 或
$ cloc --vcs git
# 或过滤 svn
$ cloc --vcs svn
高级用法
使用方法也很简单,统计结果会按照语言分类统计源代码的空白行、注释行和物理行。具体如下:
统计单个文件
#$ cloc docker-compose-devcontainer.yml
1 text file.
1 unique file.
0 files ignored.
github.com/AlDanial/cloc v 1.92 T=0.01 s (140.6 files/s, 4919.9 lines/s)
-------------------------------------------------------------------------------
Language files blank comment code
-------------------------------------------------------------------------------
YAML 1 3 1 31
-------------------------------------------------------------------------------
统计文件夹
$ cloc .
54 text files.
42 unique files.
14 files ignored.
github.com/AlDanial/cloc v 1.92 T=0.14 s (290.8 files/s, 22637.6 lines/s)
-------------------------------------------------------------------------------
Language files blank comment code
-------------------------------------------------------------------------------
Go 29 381 216 2468
Markdown 6 28 0 55
...
-------------------------------------------------------------------------------
SUM: 42 431 220 2618
-------------------------------------------------------------------------------
也同时支持通过--exclude-dir参数过滤掉不计入统计的路径
$ cloc . --exclude-dir=build
52 text files.
40 unique files.
14 files ignored.
github.com/AlDanial/cloc v 1.92 T=0.07 s (605.9 files/s, 48999.6 lines/s)
-------------------------------------------------------------------------------
Language files blank comment code
-------------------------------------------------------------------------------
Go 29 381 216 2468
Markdown 5 27 0 54
...
-------------------------------------------------------------------------------
SUM: 40 423 217 2595
-------------------------------------------------------------------------------
也支持按--by-file参数进行统计结果的筛选。
$ cloc . --by-file | head -n 10
54 text files.
42 unique files.
14 files ignored.
github.com/AlDanial/cloc v 1.92 T=0.03 s (1380.3 files/s, 107434.2 lines/s)
-----------------------------------------------------------------------------------------
File blank comment code
-----------------------------------------------------------------------------------------
./etcd/tutorial/demo.go 53 0 291
./etcd/simple_raft/raft_log.go 29 93 263
统计文件夹下多个子目录
$ for d in ./*/ ; do (cd "$d" && echo "$d" && cloc .); done
./go-bootcamp/
1474 text files.
1226 unique files.
540 files ignored.
github.com/AlDanial/cloc v 1.92 T=7.16 s (171.2 files/s, 118482.4 lines/s)
-------------------------------------------------------------------------------
Language files blank comment code
-------------------------------------------------------------------------------
JavaScript 421 10606 84539 584311
...
-------------------------------------------------------------------------------
SUM: 1226 18182 100323 729998
-------------------------------------------------------------------------------
./html-bootcamp/
18 text files.
15 unique files.
5 files ignored.
github.com/AlDanial/cloc v 1.92 T=0.02 s (635.5 files/s, 9744.9 lines/s)
-------------------------------------------------------------------------------
Language files blank comment code
-------------------------------------------------------------------------------
HTML 4 28 8 89
...
-------------------------------------------------------------------------------
SUM: 15 51 15 164
-------------------------------------------------------------------------------
./java-bootcamp/
3 text files.
1 unique file.
3 files ignored.
github.com/AlDanial/cloc v 1.92 T=0.01 s (104.8 files/s, 524.2 lines/s)
-------------------------------------------------------------------------------
Language files blank comment code
-------------------------------------------------------------------------------
Markdown 1 2 0 3
-------------------------------------------------------------------------------
更多命令
$ cloc --help
支持的语言
$ cloc --show-lang
通过 docker 运行
$ docker run --rm -v $PWD:/tmp aldanial/cloc ./
以上,就是今天的全部内容,包含了 cloc 安装和使用。当然,重要的内容往往篇幅较小,很推荐 docker 运行方式。
️️️读者每一份热爱都是笔者前进的动力! 我是三十一[1],感谢各位朋友:求点赞、求评论、求转发,大家下期见!
References
[0] Count Lines of Code: https://github.com/AlDanial/cloc [1] 三十一: http://www.lee31.cn/assets/image/ThirtyOneLee.jpeg
往期文章
入门篇:从 etcd 名字的由来,到安装部署和使用姿势 如何看待 Python(PyScript) 可以做浏览器前端开发语言? 深度总结,带你玩转 NVIDIA GPU 还活在上个时代,Etcd 3.0 实现分布式锁竟如此简单 从 0 到 1,如何徒手撸一个 Python 插件系统? 一站式机器学习开业平台 MLflow 怎么样?
边栏推荐
- 某问答社区App x-zse-96签名分析
- Comment Huawei Cloud réalise l'architecture mondiale de réseau audio - vidéo en temps réel à faible latence
- 4E1 PDH光端机19英寸机架式单纤传输20km E1接口光纤网络光端机
- 使用单调栈解题
- 如何用 Redis 实现一个分布式锁
- Rancher 2.6 全新 Monitoring 快速入门
- 全国进入主汛期,交通运输部:不具备安全运行条件的线路坚决停运!
- 语音数据标注工具与平台
- Signature analysis of app x-zse-96 in a Q & a community
- 手机证券开户交易?现在网上开户安全么?
猜你喜欢

"Internet +" contest topic hot docking | I figure to understand 38 propositions of Baidu

汉源高科新一代绿色节能以太网接入工业交换机高效节能型千兆工业以太网交换机

“芯”有灵“蜥”,万人在线!龙蜥社区走进 Intel MeetUp 精彩回顾

Tamidog | analysis of investor types and enterprise investment types

从0到1,IDE如何提升端侧研发效率?| DX研发模式

单向链表实现--计数

汉源高科USB2.0光端机USB2.0光纤延长器USB2.0光纤传输器USB2.0接口转光纤

A child process is created in the program, and then the parent and child processes run independently. The parent process reads lowercase letters on the standard input device and writes them to the pip

2光2电级联型光纤收发器千兆2光2电光纤收发器迷你嵌入式工业矿用本安型光纤收发器

程序中创建一个子进程,然后父子进程各自独自运行,父进程在标准输入设备上读入小写字母,写入管道。子进程从管道读取字符并转化为大写字母。读到x结束
随机推荐
5 个关于 NFT 的技术漏洞
Gradienttape of tensorflow2
The simplest DIY serial port Bluetooth hardware implementation scheme
Introduction and use of list
Introduction to Huawei cloud maintenance and sharing exchange platform
全国进入主汛期,交通运输部:不具备安全运行条件的线路坚决停运!
一般的理财产品期限是几天啊?
4路电话+1路千兆以太网4路PCM电话光端机
PHP regular expression
php 手写一个完美的守护进程
Numbers that only appear once < difficulty coefficient > & Yang Hui triangle < difficulty coefficient >
Creating neural networks using tensorflow2
你真的理解LDO的輸出電容嗎!?
Groovy之范围
1路百兆光纤收发器1百兆光1百兆电桌面式以太网光纤收发器内置电源
浅谈标注平台架构
4E1 PDH光端机19英寸机架式单纤传输20km E1接口光纤网络光端机
电容参数哪里找!?
Where to find capacitance parameters!?
使用tensorflow2创建神经网络