How to use the command 'scc' (with examples)
The command ‘scc’ is a tool written in Go that counts lines of code in a directory. It provides useful information such as the number of lines, blanks, code, comments, and complexity for each file. The command offers various options for customization and output formats to suit different needs.
Use case 1: Print lines of code in the current directory
Code:
scc
Motivation: This use case is helpful when you want to quickly obtain the total lines of code in the current directory without specifying a target directory.
Explanation: Running the ‘scc’ command without any arguments will count the lines of code in the current directory and display the result.
Example output:
─────────────────────────────
Language Files Lines Blanks Comments Code Complexity
─────────────────────────────
C 3 124 17 2 105 12
C++ 2 59 6 6 47 7
Go 4 256 39 32 185 54
JavaScript 9 1106 149 0 957 114
Markdown 1 20 4 0 16 0
─────────────────────────────
Total 19 1565 215 40 1310 187
─────────────────────────────
Use case 2: Print lines of code in the target directory
Code:
scc path/to/directory
Motivation: This use case is useful when you want to count the lines of code in a specific directory.
Explanation: By providing the path to the target directory after the ‘scc’ command, the tool will count the lines of code in that directory and its subdirectories, if any.
Example output:
─────────────────────────────
Language Files Lines Blanks Comments Code Complexity
─────────────────────────────
Java 6 1261 168 0 1093 129
JavaScript 8 567 100 0 467 88
Python 5 245 48 24 173 50
XML 1 115 9 32 74 3
─────────────────────────────
Total 20 2202 325 56 1807 270
─────────────────────────────
Use case 3: Display output for every file
Code:
scc --by-file
Motivation: This use case is beneficial when you want to see the line count information for each file individually.
Explanation: The ‘–by-file’ option will display the output for every file separately, providing a detailed breakdown of the lines of code.
Example output:
─────────────────────────────
Filename Language Lines Blanks Comments Code Complexity
─────────────────────────────
file1.go Go 50 5 8 37 6
file2.js JavaScript 150 10 0 140 20
file3.py Python 25 4 6 15 7
...
─────────────────────────────
Total (143 files) 2202 325 56 1807 270
─────────────────────────────
Use case 4: Display output using a specific output format
Code:
scc --format csv
Motivation: This use case allows you to customize the output format to suit your needs. By specifying the ‘–format’ option, you can choose from available formats such as ’tabular’, ‘wide’, ‘json’, ‘csv’, ‘cloc-yaml’, ‘html’, or ‘html-table’.
Explanation: In this example, we have chosen the ‘csv’ format, which will generate the output in Comma-Separated Values format. This can be useful for further processing or integration with other tools.
Example output:
Language,Files,Lines,Blanks,Comments,Code,Complexity
Java,6,1261,168,0,1093,129
JavaScript,8,567,100,0,467,88
Python,5,245,48,24,173,50
XML,1,115,9,32,74,3
Use case 5: Only count files with specific file extensions
Code:
scc --include-ext go,java,js
Motivation: This use case is helpful when you only want to count lines of code for specific file types.
Explanation: By using the ‘–include-ext’ option followed by a comma-separated list of file extensions, you can instruct ‘scc’ to only count the lines of code for files with those extensions.
Example output:
─────────────────────────────
Language Files Lines Blanks Comments Code Complexity
─────────────────────────────
Go 4 256 39 32 185 54
Java 6 1261 168 0 1093 129
JavaScript 8 567 100 0 467 88
─────────────────────────────
Total 18 2084 307 32 1745 271
─────────────────────────────
Use case 6: Exclude directories from being counted
Code:
scc --exclude-dir .git,.hg
Motivation: This use case is useful when you want to exclude specific directories from being counted in the lines of code.
Explanation: By specifying the ‘–exclude-dir’ option followed by a comma-separated list of directory names, you can exclude those directories from the line count.
Example output:
─────────────────────────────
Language Files Lines Blanks Comments Code Complexity
─────────────────────────────
C 3 124 17 2 105 12
Go 4 256 39 32 185 54
JavaScript 9 1106 149 0 957 114
Markdown 1 20 4 0 16 0
─────────────────────────────
Total 17 1506 209 34 1263 180
─────────────────────────────
Use case 7: Display output and sort by column
Code:
scc --sort lines
Motivation: This use case allows you to sort the output by a specific column, providing a different perspective on the lines of code.
Explanation: By specifying the ‘–sort’ option followed by the desired column name, you can sort the output accordingly. The available sorting options are ‘files’, ’name’, ’lines’, ‘blanks’, ‘code’, ‘comments’, and ‘complexity’.
Example output:
─────────────────────────────
Language Files Lines Blanks Comments Code Complexity
─────────────────────────────
JavaScript 9 1106 149 0 957 114
Go 4 256 39 32 185 54
C 3 124 17 2 105 12
Markdown 1 20 4 0 16 0
─────────────────────────────
Total 17 1506 209 34 1263 180
─────────────────────────────
Use case 8: Print help for scc
Code:
scc -h
Motivation: This use case is helpful when you need to quickly access the help information and usage instructions for the ‘scc’ command.
Explanation: Running ‘scc -h’ will display the help menu with a brief description of the tool and an explanation of the available options.
Example output:
scc usage: scc [flags] [path...]
-b, --blank Count blank lines
-c, --code Count lines of code
--complexity Include cyclomatic complexity score
--csv Emit output in CSV format
-d, --debug Debug mode will emit full exception stack trace on panic
--duplicate Include duplicate lines in line count
-e, --exclude-dir string Do not include directories matching the provided globbing pattern(s)
--extensions string Comma separated list of extensions to include in line count
--format string Output format [tabular (default), wide, json, csv, cloc-yaml, html, html-table]
-h, --help help for scc
-i, --include-ext string Include files matching the provided globbing pattern(s)
-k, --kilo Print output using kilobytes
-l, --languages Show list of supported languages
-L, --list-languages List all supported languages
-m, --multiprocess int The number of subprocesses to spin up to process files (default number of logical CPUs) (default 4)
-n, --average Show average per file
--no-cocomo Do not include SLOC-by-COCOMO information
-o, --output string File to write metrics out to
--overhead float By default we add 35% to the final result to attempt to roughly account for the overhead of supporting multiple languages. This value can be changed with this flag. Set to 0 to disable(ie treat 100 lines as 100 lines). Note that SLOC by COCOMO is not modified by this flag. (default 0.35)
-t, --lines Show total lines of code
-u, --unicode Use unicode line count characters
--unsupported Display unsupported files
-v, --version Show version
-y, --yaml Emit output in YAML format
Conclusion:
The ‘scc’ command is a versatile tool for counting lines of code in a directory. You can use it to obtain line count information for the current directory or a specific target directory. The command provides options to customize the output format, filter files based on extensions, exclude directories from counting, sort the output by a specific column, and more. It is a valuable tool for code analysis and understanding the composition of your codebase.