Mac OS 下統(tǒng)計代碼行數(shù)

作為編程人員涩堤,項目中的代碼行數(shù)很有必要老厌。
Windows 下比較成熟的工具較多菌瘪,而Mac下工具比較少鱼响。
這里舉幾個Mac 下統(tǒng)計代碼行數(shù)的方法:

自定義腳本:

腳本1:

終端進入目錄竖瘾,輸入:

$ find . "(" -name "*.m" -or -name "*.mm" -or -name "*.cpp" -or -name "*.h" -or -name "*.rss" ")" -print | xargs wc -l
缺點:

需要自定義腳本:不同的編程語言沟突,有不同的文件后綴名,需要自行配置准浴;
不能過濾掉注釋事扭;
不能過濾掉空行

腳本2:

$ find . -name "*.m" -or -name "*.h" -or -name "*.xib" -or -name "*.c" |xargs grep -v "^$"|wc -l

改進:
去掉空行
xargs grep -v "^$"

CLOC:

大家都知道用 wc -l 命令進行代碼行數(shù)統(tǒng)計,但是它會將代碼中的注釋乐横、空行所占用的文本行都統(tǒng)計在內(nèi)求橄。如果想查看一個 tar 包或一個項目目錄中“實際”的代碼行數(shù)并且不愿意自己去寫一個腳本來做此類工作,那么可以考慮使用 cloc葡公。

cloc 是一個 perl 腳本罐农,它可以統(tǒng)計很多種編程語言的代碼文件中的空行、注釋以及實際的代 碼行數(shù)催什。

CLOC是Count Lines of Code的意思涵亏,可以計算空行數(shù)、注釋行數(shù)蒲凶、各種語言的有效行數(shù)气筋,還可以比較兩個代碼庫在各種行數(shù)之間的不同。CLOC是完全由Perl實現(xiàn)的旋圆,不依賴第三方組件宠默,移植性強。

下載安裝 cloc.

$ brew install cloc
==> Downloading https://downloads.sourceforge.net/project/cloc/cloc/v1.62/cloc-1.62.pl
######################################################################## 100.0%
??  /usr/local/Cellar/cloc/1.62: 2 files, 372K, built in 13 seconds

對當前目錄進行統(tǒng)計

$ cloc ./
     105 text files.
      89 unique files.
      27 files ignored.

http://cloc.sourceforge.net v 1.62  T=0.37 s (174.4 files/s, 13545.8 lines/s)
-------------------------------------------------------------------------------
Language                     files          blank        comment           code
-------------------------------------------------------------------------------
Python                          38            480            507           2632
Bourne Shell                    17             82            183            674
HTML                             2             21              1            124
JSON                             1              0              0             99
YAML                             3             18              7             79
Javascript                       3              0             98             43
CSS                              1              0              1              1
-------------------------------------------------------------------------------
SUM:                            65            601            797           3652
-------------------------------------------------------------------------------

優(yōu)點:
過濾了空行灵巧;
區(qū)分了注釋和代理搀矫;
自動識別不同語言;

編寫一個java的文件刻肄。

filename: test.java

/*************************************************************************
    > File Name: test.java
    > Author:
    > Mail:
    > Created Time: 日  7/23 23:06:09 2017
 ************************************************************************/

public class test


public static void main(String[] args){}
    System.out.println("come into main");

}

重新統(tǒng)計:

$ cloc ./
     106 text files.
      90 unique files.
      27 files ignored.

http://cloc.sourceforge.net v 1.62  T=0.36 s (181.4 files/s, 13940.1 lines/s)
-------------------------------------------------------------------------------
Language                     files          blank        comment           code
-------------------------------------------------------------------------------
Python                          38            486            508           2632
Bourne Shell                    17             82            183            674
HTML                             2             21              1            124
JSON                             1              0              0             99
YAML                             3             18              7             79
Javascript                       3              0             98             43
Java                             1              4              6              4
CSS                              1              0              1              1
-------------------------------------------------------------------------------
SUM:                            66            611            804           3656
-------------------------------------------------------------------------------

專題

$ cloc --help

Usage: cloc [options] <file(s)/dir(s)> | <set 1> <set 2> | <report files>

 Count, or compute differences of, physical lines of source code in the
 given files (may be archives such as compressed tarballs or zip files)
 and/or recursively below the given directories.

## 統(tǒng)計一個 tar 包中的代碼行
$ cloc small-2.0.tar.gz 
      42 text files.
      41 unique files.                              
       4 files ignored.

http://cloc.sourceforge.net v 1.50  T=1.0 s (38.0 files/s, 3451.0 lines/s)
-------------------------------------------------------------------------------
Language                     files          blank        comment           code
-------------------------------------------------------------------------------
C                               21            435            482           1695
C/C++ Header                     8            102            161            275
m4                               4             18              4            136
make                             4             18             72             29
Bourne Shell                     1              2             20              2
-------------------------------------------------------------------------------
SUM:                            38            575            739           2137
-------------------------------------------------------------------------------



## 統(tǒng)計某個類型的文件
命令:cloc *.c *.h
簡介:該命令會統(tǒng)計當前文件夾下所有符合.c和.h的文件瓤球。
舉例:
$ cloc *.py
       2 text files.
       2 unique files.
       0 files ignored.

http://cloc.sourceforge.net v 1.62  T=0.01 s (270.8 files/s, 9207.8 lines/s)
-------------------------------------------------------------------------------
Language                     files          blank        comment           code
-------------------------------------------------------------------------------
Python                           2             15              8             45
-------------------------------------------------------------------------------
SUM:                             2             15              8             45
-------------------------------------------------------------------------------

#NOTE:  這里只會統(tǒng)計當前目錄下的.py 文件,不會進行遞歸敏弃。

統(tǒng)計結(jié)果輸出到指定文件:
使用 --report-file 選項卦羡。
舉例:

 $ cloc ./  -report-file   a.txt
      69 text files.
      60 unique files.
      52 files ignored.
Wrote a.txt

## 統(tǒng)計每個文件的代碼數(shù):
使用 --by-file 選項
舉例:
$ cloc --by-file my_project/
       2 text files.
       2 unique files.                              
       0 files ignored.

http://cloc.sourceforge.net v 1.60  T=0.01 s (149.5 files/s, 448.6 lines/s)
--------------------------------------------------------------------------------
File                              blank        comment           code
--------------------------------------------------------------------------------
my_project/perl.pl                    1              0              2
my_project/bash.sh                    1              0              2
--------------------------------------------------------------------------------
SUM:                                  2              0              4
--------------------------------------------------------------------------------

查看支持的語言:


$ cloc --show-lang
ABAP                       (abap)
ActionScript               (as)
Ada                        (ada, adb, ads, pad)
ADSO/IDSM                  (adso)
AMPLE                      (ample, dofile, startup)
Ant                        (build.xml, build.xml)
Apex Trigger               (trigger)
Arduino Sketch             (ino, pde)
ASP                        (asa, asp)
ASP.Net                    (asax, ascx, asmx, aspx, master, sitemap, webinfo)
AspectJ                    (aj)
Assembly                   (asm, S, s)
AutoHotkey                 (ahk)
awk                        (awk)
Blade                      (blade.php)
Bourne Again Shell         (bash)
Bourne Shell               (sh)
builder                    (xml.builder)
C                          (c, ec, pgc)
...


$ cloc --show-lang | wc
    195     629    7466


忽略某幾個目錄:

 --exclude-dir=<D1>[,D2,]  Exclude the given comma separated directories
                             D1, D2, D3, et cetera, from being scanned.  For
                             example  --exclude-dir=.cache,test  will skip
                             all files and subdirectories that have /.cache/
                             or /test/ as their parent directory.
                             Directories named .bzr, .cvs, .hg, .git, and
                             .svn are always excluded.
                             This option only works with individual directory
                             names so including file path separators is not
                             allowed.  Use --fullpath and --not-match-d=<regex>
                             to supply a regex matching multiple subdirectories.


舉例:

$ cloc ./   --exclude-dir=docs,tests
 

在windows 下的使用

下載 cloc.exe 文件就可用像 mac 下的 cloc 命令一樣使用。

https://blog.csdn.net/liutengteng130/article/details/79696898

【工具篇】Windows下統(tǒng)計代碼行數(shù)cloc

簡介:

csdn 上的一個介紹...

下載路徑:
https://sourceforge.net/projects/cloc/

http://cloc.sourceforge.net/

https://sourceforge.net/p/cloc/code/HEAD/tree/trunk/cloc

簡介:

官網(wǎng)的舊地址麦到。
cloc is now being developed at https://github.com/AlDanial/cloc 

Legacy releases (1.64 and earlier) can still be found here. 

https://github.com/AlDanial/cloc
簡介:

官網(wǎng)虹茶。

不同環(huán)境下的安裝。

npm install -g cloc                    # https://www.npmjs.com/package/cloc
sudo apt-get install cloc              # Debian, Ubuntu
sudo yum install cloc                  # Red Hat, Fedora
sudo dnf install cloc                  # Fedora 22 or later
sudo pacman -S cloc                    # Arch
emerge -av dev-util/cloc               # Gentoo https://packages.gentoo.org/packages/dev-util/cloc
sudo pkg install cloc                  # FreeBSD
sudo port install cloc                 # Mac OS X with MacPorts
brew install cloc                      # Mac OS X with Homebrew
choco install cloc                     # Windows with Chocolatey


官網(wǎng)還推薦了其他代碼統(tǒng)計工具:

Sonar
Ohcount
SLOCCount
sclc
USC's CODECOUNT
loc

利用Mac 終端命令統(tǒng)計代碼行數(shù)
http://blog.csdn.net/colawjy/article/details/46425715
簡介:

利用shell命令隅要。

Mac OS 下統(tǒng)計代碼行數(shù)的工具
http://www.cnblogs.com/wengzilin/p/4580646.html
一個轉(zhuǎn)載:
http://blog.csdn.net/forwardto9/article/details/50389420
簡介:

下載安裝了cloc.
比較詳細的介紹蝴罪。

Counting lines of code with cloc
https://linuxconfig.org/counting-lines-of-code-with-cloc
簡介:

linuxconfig.org  上的內(nèi)容。

英文文章步清,解釋還是比較全面的要门。 

有統(tǒng)計 .zip虏肾, .tar 的舉例,有 `-by-file`的示例欢搜。 

http://www.reibang.com/p/7c23265f0fed
PPRows for Mac
簡介:

一作者自己開發(fā)的一個圖形界面工具封豪。可以體驗一下炒瘟。

github地址:https://github.com/jkpang/PPRows

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末吹埠,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子疮装,更是在濱河造成了極大的恐慌缘琅,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,692評論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件廓推,死亡現(xiàn)場離奇詭異刷袍,居然都是意外死亡,警方通過查閱死者的電腦和手機樊展,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,482評論 3 392
  • 文/潘曉璐 我一進店門呻纹,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人专缠,你說我怎么就攤上這事雷酪。” “怎么了涝婉?”我有些...
    開封第一講書人閱讀 162,995評論 0 353
  • 文/不壞的土叔 我叫張陵哥力,是天一觀的道長。 經(jīng)常有香客問我嘁圈,道長,這世上最難降的妖魔是什么蟀淮? 我笑而不...
    開封第一講書人閱讀 58,223評論 1 292
  • 正文 為了忘掉前任最住,我火速辦了婚禮,結(jié)果婚禮上怠惶,老公的妹妹穿的比我還像新娘涨缚。我一直安慰自己,他們只是感情好策治,可當我...
    茶點故事閱讀 67,245評論 6 388
  • 文/花漫 我一把揭開白布脓魏。 她就那樣靜靜地躺著,像睡著了一般通惫。 火紅的嫁衣襯著肌膚如雪茂翔。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,208評論 1 299
  • 那天履腋,我揣著相機與錄音珊燎,去河邊找鬼惭嚣。 笑死,一個胖子當著我的面吹牛悔政,可吹牛的內(nèi)容都是我干的晚吞。 我是一名探鬼主播,決...
    沈念sama閱讀 40,091評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼谋国,長吁一口氣:“原來是場噩夢啊……” “哼槽地!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起芦瘾,我...
    開封第一講書人閱讀 38,929評論 0 274
  • 序言:老撾萬榮一對情侶失蹤捌蚊,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后旅急,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體逢勾,經(jīng)...
    沈念sama閱讀 45,346評論 1 311
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,570評論 2 333
  • 正文 我和宋清朗相戀三年藐吮,在試婚紗的時候發(fā)現(xiàn)自己被綠了溺拱。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,739評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡谣辞,死狀恐怖迫摔,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情泥从,我是刑警寧澤句占,帶...
    沈念sama閱讀 35,437評論 5 344
  • 正文 年R本政府宣布,位于F島的核電站躯嫉,受9級特大地震影響纱烘,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜祈餐,卻給世界環(huán)境...
    茶點故事閱讀 41,037評論 3 326
  • 文/蒙蒙 一擂啥、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧帆阳,春花似錦哺壶、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,677評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至鳍徽,卻和暖如春资锰,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背阶祭。 一陣腳步聲響...
    開封第一講書人閱讀 32,833評論 1 269
  • 我被黑心中介騙來泰國打工台妆, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留翎猛,地道東北人。 一個月前我還...
    沈念sama閱讀 47,760評論 2 369
  • 正文 我出身青樓接剩,卻偏偏與公主長得像切厘,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子懊缺,可洞房花燭夜當晚...
    茶點故事閱讀 44,647評論 2 354

推薦閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 172,081評論 25 707
  • 1889年1月30日鹃两,二十三歲的愛爾蘭詩人葉芝第一次遇見了美麗的女演員茅德.岡遗座。她佇立窗畔,身旁盛開著一大團蘋果花...
    周世恩閱讀 794評論 1 4
  • 今天媽媽休息沒上班俊扳,早上媽媽送上學途蒋,把小喇叭拿在手里,一邊走一邊聽馋记,一邊聽一邊跟著說和唱号坡。下午放學去看了場電影,哆...
    索小佳閱讀 231評論 0 0
  • Day2 幸福從醒來開始 2017年5月8日 回拉薩第一個清晨梯醒,幸福在清淺的陽光里宽堆,在隔壁部隊的號角里,安然...
    若_愚愚閱讀 235評論 0 0