Merge pull request #10644 from littleji/20181010

【完成翻译】20181010 Cloc - Count The Lines Of Source Code In Many Programming Languages
This commit is contained in:
Xingyu.Wang 2018-10-12 22:17:40 +08:00 committed by GitHub
commit faa5dd199a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 198 additions and 198 deletions

View File

@ -1,198 +0,0 @@
translating by littleji
Cloc Count The Lines Of Source Code In Many Programming Languages
======
![](https://www.ostechnix.com/wp-content/uploads/2018/10/cloc-720x340.png)
As a developer, you may need to share the progress and statistics of your code to your boss or colleagues. Your boss might want to analyze the code and give any additional inputs. In such cases, there are few programs, as far as I know, available to analyze the source code. One such program is [**Ohcount**][1]. Today, I came across yet another similar utility namely **“Cloc”**. Using the Cloc, you can easily count the lines of source code in several programming languages. It counts the blank lines, comment lines, and physical lines of source code and displays the result in a neat tabular-column format. Cloc is free, open source and cross-platform utility entirely written in **Perl** programming language.
### Features
Cloc ships with numerous advantages including the following:
* Easy to install/use. Requires no dependencies.
* Portable
* It can produce results in a variety of formats, such as plain text, SQL, JSON, XML, YAML, comma separated values.
* Can count your git commits.
* Count the code in directories and sub-directories.
* Count codes count code within compressed archives like tar balls, Zip files, Java .ear files etc.
* Open source and cross platform.
### Installing Cloc
The Cloc utility is available in the default repositories of most Unix-like operating systems. So, you can install it using the default package manager as shown below.
On Arch Linux and its variants:
```
$ sudo pacman -S cloc
```
On Debian, Ubuntu:
```
$ sudo apt-get install cloc
```
On CentOS, Red Hat, Scientific Linux:
```
$ sudo yum install cloc
```
On Fedora:
```
$ sudo dnf install cloc
```
On FreeBSD:
```
$ sudo pkg install cloc
```
It can also installed using third-party package manager like [**NPM**][2] as well.
```
$ npm install -g cloc
```
### Count The Lines Of Source Code In Many Programming Languages
Let us start with a simple example. I have a “hello world” program written in C in my current working directory.
```
$ cat hello.c
#include <stdio.h>
int main()
{
// printf() displays the string inside quotation
printf("Hello, World!");
return 0;
}
```
To count the lines of code in the hello.c program, simply run:
```
$ cloc hello.c
```
Sample output:
![](https://www.ostechnix.com/wp-content/uploads/2018/10/Hello-World-Program.png)
The first column specifies the **name of programming languages that the source code consists of**. As you can see in the above output, the source code of “hello world” program is written using **C** programming language.
The second column displays the **number of files in each programming languages**. So, our code contains **1 file** in total.
The third column displays the **total number of blank lines**. We have zero blank files in our code.
The fourth column displays **number of comment lines**.
And the final and fifth column displays **total physical lines of given source code**.
It is just a 6 line code program, so counting the lines in the code is not a big deal. What about the some big source code file? Have a look at the following example:
```
$ cloc file.tar.gz
```
Sample output:
![](https://www.ostechnix.com/wp-content/uploads/2018/10/cloc-1.png)
As per the above output, it is quite difficult to manually find exact count of code. But, Cloc displays the result in seconds with nice tabular-column format. You can view the gross total of each section at the end which is quite handy when it comes to analyze the source code of a program.
Cloc not only counts the individual source code files, but also files inside directories and sub-directories, archives, and even in specific git commits etc.
**Count the lines of codes in a directory:**
```
$ cloc dir/
```
![][4]
**Sub-directory:**
```
$ cloc dir/cloc/tests
```
![][5]
**Count the lines of codes in archive file:**
```
$ cloc archive.zip
```
![][6]
You can also count lines in a git repository, using a specific commit like below.
```
$ git clone https://github.com/AlDanial/cloc.git
$ cd cloc
$ cloc 157d706
```
![][7]
Cloc can recognize several programming languages. To view the complete list of recognized languages, run:
```
$ cloc --show-lang
```
For more details, refer the help section.
```
$ cloc --help
```
Cheers!
--------------------------------------------------------------------------------
via: https://www.ostechnix.com/cloc-count-the-lines-of-source-code-in-many-programming-languages/
作者:[SK][a]
选题:[lujun9972][b]
译者:[译者ID](https://github.com/译者ID)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://www.ostechnix.com/author/sk/
[b]: https://github.com/lujun9972
[1]: https://www.ostechnix.com/ohcount-the-source-code-line-counter-and-analyzer/
[2]: https://www.ostechnix.com/install-node-js-linux/
[3]: data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7
[4]: http://www.ostechnix.com/wp-content/uploads/2018/10/cloc-2-1.png
[5]: http://www.ostechnix.com/wp-content/uploads/2018/10/cloc-4.png
[6]: http://www.ostechnix.com/wp-content/uploads/2018/10/cloc-3.png
[7]: http://www.ostechnix.com/wp-content/uploads/2018/10/cloc-5.png

View File

@ -0,0 +1,198 @@
Cloc 计算不同编程语言源代码的行数
======
![](https://www.ostechnix.com/wp-content/uploads/2018/10/cloc-720x340.png)
作为一个开发人员,你可能需要不时的想你的领导或者同事分享你目前的工作与代码开发进展,抑或你的领导想对代码进行全方位的分析。在这种情况下,你就需要用到我所知的这么几个程序,其中一个是[**Ohcount**][1]。今天,我遇到了另一个程序, **"Cloc"**。通过使用 Cloc你可以很容易的计算出多种语言的源代码行数。它还可以计算空行数、代码行数、实际占用的行数并通过整齐的表格进行结果输出。Cloc 是免费的、开源的、跨平台程序,使用的 **Perl** 进行开发。
### 特点
Cloc 有很多优势:
* 容易安装和实用,不需要额外的依赖项。
* 便携式
* 支持多种的结果格式导出包括纯文本、SQL、JSON、XML、YAML、CSV
* 可以计算 git 的提交数
* 可递归计算文件夹内的代码行数
* 可计算压缩后的文件tar、zip、Java ear
* 开源跨平台部署
### 安装
Cloc 的安装包在大多数的 *nix 操作系统的默认软件库内,所以你只需要使用默认的包管理器安装如下这样。
Arch Linux:
```
$ sudo pacman -S cloc
```
Debian, Ubuntu:
```
$ sudo apt-get install cloc
```
CentOS, Red Hat, Scientific Linux:
```
$ sudo yum install cloc
```
Fedora:
```
$ sudo dnf install cloc
```
FreeBSD:
```
$ sudo pkg install cloc
```
当然你也可以使用第三方的包管理器比如[**NPM**][2]。
```
$ npm install -g cloc
```
### 使用实例
首先来几个简单的例子,比如下面在我目前工作目录中的的 C 代码。
```
$ cat hello.c
#include <stdio.h>
int main()
{
// printf() displays the string inside quotation
printf("Hello, World!");
return 0;
}
```
想要计算行数,只需要简单运行:
```
$ cloc hello.c
```
输出:
![](https://www.ostechnix.com/wp-content/uploads/2018/10/Hello-World-Program.png)
第一列是被分析文件的语言类型,上面我们可以看到该分析的文件语言类型是 **C**
第二列显示的是该种语言类型有多少文件,图中说明只有一个。
第三列显示空行的个数,图中显示无。
第四列显示注释的行数。
第五列显示该文件中的总共的行数。
这是一个有只有6行代码的源文件我们看到算的还算准确那么如果用来算一个行数较多的源文件会发生什么呢
```
$ cloc file.tar.gz
```
输出:
![](https://www.ostechnix.com/wp-content/uploads/2018/10/cloc-1.png)
根据上述输出手动查找准确的代码计数非常困难。但是Cloc以易读的表格格式显示结果。你还可以在最后查看每个部分的总计这在分析程序的源代码时非常方便。
除了源代码文件Cloc 还能递归的计算各个目录及其子目录下的文件、压缩包、甚至 git 中的 commit 数目等。
**文件夹中使用的例子:**
```
$ cloc dir/
```
![][4]
**子文件夹中使用的例子:**
```
$ cloc dir/cloc/tests
```
![][5]
**计算一个压缩包中源代码的行数:**
```
$ cloc archive.zip
```
![][6]
**你还可以计算一个 git 项目:**
```
$ git clone https://github.com/AlDanial/cloc.git
$ cd cloc
$ cloc 157d706
```
![][7]
**使用下面的命令,查看 Cloc 支持的语言类型:**
```
$ cloc --show-lang
```
当然help 能告诉你更多关于 Cloc 的使用帮助。
```
$ cloc --help
```
开始使用吧!
--------------------------------------------------------------------------------
via: https://www.ostechnix.com/cloc-count-the-lines-of-source-code-in-many-programming-languages/
作者:[SK][a]
选题:[lujun9972][b]
译者:[译者ID](https://github.com/littleji)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://www.ostechnix.com/author/sk/
[b]: https://github.com/lujun9972
[1]: https://www.ostechnix.com/ohcount-the-source-code-line-counter-and-analyzer/
[2]: https://www.ostechnix.com/install-node-js-linux/
[3]: data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7
[4]: http://www.ostechnix.com/wp-content/uploads/2018/10/cloc-2-1.png
[5]: http://www.ostechnix.com/wp-content/uploads/2018/10/cloc-4.png
[6]: http://www.ostechnix.com/wp-content/uploads/2018/10/cloc-3.png
[7]: http://www.ostechnix.com/wp-content/uploads/2018/10/cloc-5.png