| 1 |
Command-line Usage |
| 2 |
================== |
| 3 |
|
| 4 |
Radon currently has four commands: |
| 5 |
|
| 6 |
* :command:`cc`: compute Cyclomatic Complexity |
| 7 |
* :command:`raw`: compute raw metrics |
| 8 |
* :command:`mi`: compute Maintainability Index |
| 9 |
* :command:`hal`: compute Halstead complexity metrics |
| 10 |
|
| 11 |
.. note:: |
| 12 |
On some systems, such as Windows, the default encoding is not UTF-8. If you |
| 13 |
are using Unicode characters in your Python file and want to analyze it |
| 14 |
with Radon, you'll have to set the `RADONFILESENCODING` environment |
| 15 |
variable to `UTF-8`. |
| 16 |
|
| 17 |
Radon configuration files |
| 18 |
------------------------- |
| 19 |
|
| 20 |
When using radon regularly, you may want to specify default argument values in a configuration file. |
| 21 |
For example, all of the radon commands have a ``--exclude`` and ``--ignore`` argument on the command-line. |
| 22 |
|
| 23 |
Radon will look for the following files to determine default arguments: |
| 24 |
|
| 25 |
* ``radon.cfg`` |
| 26 |
* ``pyproject.toml`` (with optional `toml` install on Python <3.11) |
| 27 |
* ``setup.cfg`` |
| 28 |
* ``~/.radon.cfg`` |
| 29 |
|
| 30 |
Any radon configuration will be given in the INI-format, under the section ``[radon]``. |
| 31 |
|
| 32 |
For example: |
| 33 |
|
| 34 |
.. code-block:: ini |
| 35 |
|
| 36 |
[radon] |
| 37 |
exclude = test_*.py |
| 38 |
cc_min = B |
| 39 |
|
| 40 |
Usage with Jupyter Notebooks |
| 41 |
---------------------------- |
| 42 |
|
| 43 |
Radon can be used with ``.ipynb`` files to inspect code metrics for Python cells. Any ``%`` macros will be ignored in the metrics. |
| 44 |
|
| 45 |
.. note:: |
| 46 |
|
| 47 |
Jupyter Notebook support requires the optional ``nbformat`` package. To install, run ``pip install nbformat``. |
| 48 |
|
| 49 |
To enable scanning of Jupyter notebooks, add the ``--include-ipynb`` flag with any of the commands. |
| 50 |
|
| 51 |
To enable reporting of individual cells, add the ``--ipynb-cells`` flag with any of the commands. |
| 52 |
|
| 53 |
The :command:`cc` command |
| 54 |
------------------------- |
| 55 |
|
| 56 |
.. program:: cc |
| 57 |
|
| 58 |
This command analyzes Python source files and compute Cyclomatic Complexity. |
| 59 |
The output can be filtered by specifying the :option:`-n` and :option:`-x` |
| 60 |
flags. By default, the complexity score is not displayed, the option |
| 61 |
:option:`-s` (show complexity) toggles this behaviour. File or directories |
| 62 |
exclusion is supported through glob patterns. Every positional argument is |
| 63 |
interpreted as a path. The program then walks through its children and analyzes |
| 64 |
Python files. |
| 65 |
Every block will be ranked from A (best complexity score) to F (worst one). |
| 66 |
Ranks corresponds to complexity scores as follows: |
| 67 |
|
| 68 |
========== ====== ========================================= |
| 69 |
CC score Rank Risk |
| 70 |
========== ====== ========================================= |
| 71 |
1 - 5 A low - simple block |
| 72 |
6 - 10 B low - well structured and stable block |
| 73 |
11 - 20 C moderate - slightly complex block |
| 74 |
21 - 30 D more than moderate - more complex block |
| 75 |
31 - 40 E high - complex block, alarming |
| 76 |
41+ F very high - error-prone, unstable block |
| 77 |
========== ====== ========================================= |
| 78 |
|
| 79 |
Blocks are also classified into three types: functions, methods and classes. |
| 80 |
They're listed by letter in the command output for convenience when scanning |
| 81 |
through a longer list of blocks: |
| 82 |
|
| 83 |
============ ======== |
| 84 |
Block type Letter |
| 85 |
============ ======== |
| 86 |
Function F |
| 87 |
Method M |
| 88 |
Class C |
| 89 |
============ ======== |
| 90 |
|
| 91 |
Options |
| 92 |
+++++++ |
| 93 |
|
| 94 |
.. option:: -x, --max |
| 95 |
|
| 96 |
Set the maximum complexity rank to display, defaults to ``F``. |
| 97 |
|
| 98 |
Value can be set in a configuration file using the ``cc_max`` property. |
| 99 |
|
| 100 |
.. option:: -n, --min |
| 101 |
|
| 102 |
Set the minimum complexity rank to display, defaults to ``A``. |
| 103 |
|
| 104 |
Value can be set in a configuration file using the ``cc_min`` property. |
| 105 |
|
| 106 |
.. option:: -a, --average |
| 107 |
|
| 108 |
If given, at the end of the analysis show the average Cyclomatic |
| 109 |
Complexity. This option is influenced by :option:`-x, --max` and |
| 110 |
:option:`-n, --min` options. |
| 111 |
|
| 112 |
Value can be set in a configuration file using the ``average`` property. |
| 113 |
|
| 114 |
.. option:: --total-average |
| 115 |
|
| 116 |
Like :option:`-a, --average`, but it is not influenced by `min` and `max`. |
| 117 |
Every analyzed block is counted, no matter whether it is displayed or not. |
| 118 |
|
| 119 |
Value can be set in a configuration file using the ``total_average`` property. |
| 120 |
|
| 121 |
.. option:: -s, --show-complexity |
| 122 |
|
| 123 |
If given, show the complexity score along with its rank. |
| 124 |
|
| 125 |
Value can be set in a configuration file using the ``show_complexity`` property. |
| 126 |
|
| 127 |
.. option:: -e, --exclude |
| 128 |
|
| 129 |
Exclude files only when their path matches one of these glob patterns. |
| 130 |
Usually needs quoting at the command line. |
| 131 |
|
| 132 |
Value can be set in a configuration file using the ``exclude`` property. |
| 133 |
|
| 134 |
.. option:: -i, --ignore |
| 135 |
|
| 136 |
Ignore directories when their name matches one of these glob patterns: radon |
| 137 |
won't even descend into them. By default, hidden directories (starting with |
| 138 |
'.') are ignored. |
| 139 |
|
| 140 |
Value can be set in a configuration file using the ``ignore`` property. |
| 141 |
|
| 142 |
.. option:: -o, --order |
| 143 |
|
| 144 |
The ordering function for the results. Can be one of: |
| 145 |
|
| 146 |
* `SCORE`: order by cyclomatic complexity (descending): |
| 147 |
* `LINES`: order by line numbers; |
| 148 |
* `ALPHA`: order by block names (alphabetically). |
| 149 |
|
| 150 |
Value can be set in a configuration file using the ``order`` property. |
| 151 |
|
| 152 |
.. option:: -j, --json |
| 153 |
|
| 154 |
If given, the results will be converted into JSON. This is useful in case |
| 155 |
you need to export the results to another application. |
| 156 |
|
| 157 |
.. option:: --xml |
| 158 |
|
| 159 |
If given, the results will be converted into XML. Note that not all the |
| 160 |
information is kept. This is specifically targeted to Jenkin's plugin CCM. |
| 161 |
|
| 162 |
.. option:: --md |
| 163 |
|
| 164 |
If given, the results will be converted into Markdown. Note that not all the |
| 165 |
information is kept. |
| 166 |
|
| 167 |
.. option:: --no-assert |
| 168 |
|
| 169 |
Does not count assert statements when computing complexity. This is because |
| 170 |
Python can be run with an optimize flag which removes assert statements. |
| 171 |
|
| 172 |
Value can be set in a configuration file using the ``no_assert`` property. |
| 173 |
|
| 174 |
.. option:: --include-ipynb |
| 175 |
|
| 176 |
Include the Python cells within IPython Notebooks in the reporting. |
| 177 |
|
| 178 |
Value can be set in a configuration file using the ``include_ipynb`` property. |
| 179 |
|
| 180 |
.. option:: --ipynb-cells |
| 181 |
|
| 182 |
Report on individual cells in any .ipynb files. |
| 183 |
|
| 184 |
Value can be set in a configuration file using the ``ipynb_cells`` property. |
| 185 |
|
| 186 |
.. option:: -O, --output-file |
| 187 |
|
| 188 |
Save output to the specified output file. |
| 189 |
|
| 190 |
Value can be set in a configuration file using the ``output_file`` property. |
| 191 |
|
| 192 |
Examples |
| 193 |
++++++++ |
| 194 |
|
| 195 |
:: |
| 196 |
|
| 197 |
$ radon cc path |
| 198 |
|
| 199 |
Radon will walk through the subdirectories of path and will analyze all |
| 200 |
child nodes (every Python file it encounters). |
| 201 |
|
| 202 |
:: |
| 203 |
|
| 204 |
$ radon cc -e "path/tests*,path/docs/*" path |
| 205 |
|
| 206 |
As in the above example, Radon will walk the directories, excluding paths |
| 207 |
matching ``path/tests/*`` and ``path/docs/*``. |
| 208 |
|
| 209 |
.. warning:: |
| 210 |
|
| 211 |
Remember to quote the patterns, otherwise your shell might expand them! |
| 212 |
|
| 213 |
Depending on the single cases, a more suitable alternative might be this:: |
| 214 |
|
| 215 |
$ radon cc -i "docs,tests" path |
| 216 |
|
| 217 |
:: |
| 218 |
|
| 219 |
$ cat path/to/file.py | radon cc - |
| 220 |
|
| 221 |
Setting the path to "-" will cause Radon to analyze code from stdin |
| 222 |
|
| 223 |
:: |
| 224 |
|
| 225 |
$ radon cc --min B --max E path |
| 226 |
|
| 227 |
Here Radon will only display blocks ranked between B and E (i.e. from ``CC=6`` |
| 228 |
to ``CC=40``). |
| 229 |
|
| 230 |
|
| 231 |
The :command:`mi` command |
| 232 |
------------------------- |
| 233 |
|
| 234 |
.. program:: mi |
| 235 |
|
| 236 |
This command analyzes Python source code files and compute the Maintainability |
| 237 |
Index score. |
| 238 |
Every positional argument is treated as a starting point from which to walk |
| 239 |
looking for Python files (as in the :command:`cc` command). Paths can be |
| 240 |
excluded with the :option:`-e` option. |
| 241 |
The Maintainability Index is always in the range 0-100. MI is ranked as |
| 242 |
follows: |
| 243 |
|
| 244 |
========== ====== ================= |
| 245 |
MI score Rank Maintainability |
| 246 |
========== ====== ================= |
| 247 |
100 - 20 A Very high |
| 248 |
19 - 10 B Medium |
| 249 |
9 - 0 C Extremely low |
| 250 |
========== ====== ================= |
| 251 |
|
| 252 |
|
| 253 |
Options |
| 254 |
+++++++ |
| 255 |
|
| 256 |
.. option:: -x, --max |
| 257 |
|
| 258 |
Set the maximum MI to display. Expects a letter between A-F. Defaults to ``C``. |
| 259 |
|
| 260 |
Value can be set in a configuration file using the ``mi_max`` property. |
| 261 |
|
| 262 |
.. option:: -n, --min |
| 263 |
|
| 264 |
Set the minimum MI to display. Expects a letter between A-F. Defaults to ``A``. |
| 265 |
|
| 266 |
Value can be set in a configuration file using the ``mi_min`` property. |
| 267 |
|
| 268 |
.. option:: -e, --exclude |
| 269 |
|
| 270 |
Exclude files only when their path matches one of these glob patterns. |
| 271 |
Usually needs quoting at the command line. |
| 272 |
|
| 273 |
Value can be set in a configuration file using the ``exclude`` property. |
| 274 |
|
| 275 |
.. option:: -i, --ignore |
| 276 |
|
| 277 |
Ignore directories when their name matches one of these glob patterns: radon |
| 278 |
won't even descend into them. By default, hidden directories (starting with |
| 279 |
'.') are ignored. |
| 280 |
|
| 281 |
Value can be set in a configuration file using the ``ignore`` property. |
| 282 |
|
| 283 |
.. option:: -m, --multi |
| 284 |
|
| 285 |
If given, Radon will not count multiline strings as comments. |
| 286 |
Most of the time this is safe since multiline strings are used as functions |
| 287 |
docstrings, but one should be aware that their use is not limited to that |
| 288 |
and sometimes it would be wrong to count them as comment lines. |
| 289 |
|
| 290 |
Value can be set in a configuration file using the ``multi`` property. |
| 291 |
|
| 292 |
.. option:: -s, --show |
| 293 |
|
| 294 |
If given, the actual MI value is shown in results, alongside the rank. |
| 295 |
|
| 296 |
Value can be set in a configuration file using the ``show_mi`` property. |
| 297 |
|
| 298 |
.. option:: -j, --json |
| 299 |
|
| 300 |
Format results in JSON. |
| 301 |
|
| 302 |
.. option:: --include-ipynb |
| 303 |
|
| 304 |
Include the Python cells within IPython Notebooks in the reporting. |
| 305 |
|
| 306 |
Value can be set in a configuration file using the ``include_ipynb`` property. |
| 307 |
|
| 308 |
.. option:: --ipynb-cells |
| 309 |
|
| 310 |
Report on individual cells in any .ipynb files. |
| 311 |
|
| 312 |
Value can be set in a configuration file using the ``ipynb_cells`` property. |
| 313 |
|
| 314 |
.. option:: -O, --output-file |
| 315 |
|
| 316 |
Save output to the specified output file. |
| 317 |
|
| 318 |
Value can be set in a configuration file using the ``output_file`` property. |
| 319 |
|
| 320 |
|
| 321 |
Examples |
| 322 |
++++++++ |
| 323 |
|
| 324 |
:: |
| 325 |
|
| 326 |
$ radon mi path1 path2 |
| 327 |
|
| 328 |
Analyze every Python file under *path1* or *path2*. It checks recursively in |
| 329 |
every subdirectory. |
| 330 |
|
| 331 |
|
| 332 |
:: |
| 333 |
|
| 334 |
$ radon mi path1 -e "path1/tests/*" |
| 335 |
|
| 336 |
Like the previous example, but excluding from the analysis every path that |
| 337 |
matches `path1/tests/*`. |
| 338 |
|
| 339 |
:: |
| 340 |
|
| 341 |
$ radon mi -m path1 |
| 342 |
|
| 343 |
Like the previous examples, but does not count multiline strings as comments. |
| 344 |
|
| 345 |
|
| 346 |
The :command:`raw` command |
| 347 |
-------------------------- |
| 348 |
|
| 349 |
.. program:: raw |
| 350 |
|
| 351 |
This command analyzes the given Python modules in order to compute raw metrics. |
| 352 |
These include: |
| 353 |
|
| 354 |
* **LOC**: the total number of lines of code |
| 355 |
* **LLOC**: the number of logical lines of code |
| 356 |
* **SLOC**: the number of source lines of code - not necessarily |
| 357 |
corresponding to the **LLOC** [Wikipedia]_ |
| 358 |
* **comments**: the number of Python comment lines (i.e. only single-line |
| 359 |
comments ``#``) |
| 360 |
* **multi**: the number of lines representing multi-line strings |
| 361 |
* **blank**: the number of blank lines (or whitespace-only ones) |
| 362 |
|
| 363 |
The equation :math:`sloc + multi + single comments + blank = loc` should always |
| 364 |
hold. |
| 365 |
|
| 366 |
.. [Wikipedia] More information on **LOC**, **SLOC**, **LLOC** here: http://en.wikipedia.org/wiki/Source_lines_of_code |
| 367 |
|
| 368 |
|
| 369 |
Options |
| 370 |
+++++++ |
| 371 |
|
| 372 |
.. option:: -e, --exclude |
| 373 |
|
| 374 |
Exclude files only when their path matches one of these glob patterns. |
| 375 |
Usually needs quoting at the command line. |
| 376 |
|
| 377 |
Value can be set in a configuration file using the ``exclude`` property. |
| 378 |
|
| 379 |
.. option:: -i, --ignore |
| 380 |
|
| 381 |
Ignore directories when their name matches one of these glob patterns: radon |
| 382 |
won't even descend into them. By default, hidden directories (starting with |
| 383 |
'.') are ignored. |
| 384 |
|
| 385 |
Value can be set in a configuration file using the ``ignore`` property. |
| 386 |
|
| 387 |
.. option:: -s, --summary |
| 388 |
|
| 389 |
If given, at the end of the analysis a summary of the gathered |
| 390 |
metrics will be shown. |
| 391 |
|
| 392 |
.. option:: -j, --json |
| 393 |
|
| 394 |
If given, the results will be converted into JSON. Note that the JSON export |
| 395 |
does not include the summary (enabled with the option `-s, --summary`). |
| 396 |
|
| 397 |
.. option:: -O, --output-file |
| 398 |
|
| 399 |
Save output to the specified output file. |
| 400 |
|
| 401 |
Value can be set in a configuration file using the ``output_file`` property. |
| 402 |
|
| 403 |
.. option:: --include-ipynb |
| 404 |
|
| 405 |
Include the Python cells within IPython Notebooks in the reporting. |
| 406 |
|
| 407 |
Value can be set in a configuration file using the ``include_ipynb`` property. |
| 408 |
|
| 409 |
.. option:: --ipynb-cells |
| 410 |
|
| 411 |
Report on individual cells in any .ipynb files. |
| 412 |
|
| 413 |
Value can be set in a configuration file using the ``ipynb_cells`` property. |
| 414 |
|
| 415 |
Examples |
| 416 |
++++++++ |
| 417 |
|
| 418 |
:: |
| 419 |
|
| 420 |
$ radon raw path1 path2 |
| 421 |
|
| 422 |
Analyze every Python file under *path1* or *path2*. It checks recursively in |
| 423 |
every subdirectory. |
| 424 |
|
| 425 |
:: |
| 426 |
|
| 427 |
$ radon raw path1 -e "path1/tests/*" |
| 428 |
|
| 429 |
Like the previous example, but excluding from the analysis every path that |
| 430 |
matches ``path1/tests/*``. |
| 431 |
|
| 432 |
|
| 433 |
The :command:`hal` command |
| 434 |
-------------------------- |
| 435 |
|
| 436 |
.. program:: hal |
| 437 |
|
| 438 |
This command analyzes Python source files and computes their Halstead |
| 439 |
complexity metrics. Files can be analyzed as wholes, or in terms of their |
| 440 |
top-level functions with the :option:`-f` flag. |
| 441 |
|
| 442 |
Excluding files or directories is supported through glob patterns with the |
| 443 |
:option:`-e` flag. Every positional argument is interpreted as a path. The |
| 444 |
program walks through its children and analyzes Python files. |
| 445 |
|
| 446 |
Options |
| 447 |
+++++++ |
| 448 |
|
| 449 |
.. option:: -f, --functions |
| 450 |
|
| 451 |
Compute the metrics on the *function* level, as opposed to the *file* level. |
| 452 |
|
| 453 |
Value can be set in a configuration file using the ``functions`` property. |
| 454 |
|
| 455 |
.. option:: -e, --exclude |
| 456 |
|
| 457 |
Exclude files when their path matches one of these glob patterns. Usually |
| 458 |
needs quoting at the command line. |
| 459 |
|
| 460 |
Value can be set in a configuration file using the ``exclude`` property. |
| 461 |
|
| 462 |
.. option:: -i, --ignore |
| 463 |
|
| 464 |
Refuse to descend into directories that match any of these glob patterns. By |
| 465 |
default, hidden directories (starting with '.') are ignored. |
| 466 |
|
| 467 |
Value can be set in a configuration file using the ``ignore`` property. |
| 468 |
|
| 469 |
.. option:: -j, --json |
| 470 |
|
| 471 |
Convert results into JSON. This is useful for exporting results to another |
| 472 |
application. |
| 473 |
|
| 474 |
.. option:: -O, --output-file |
| 475 |
|
| 476 |
Save output to the specified output file. |
| 477 |
|
| 478 |
Value can be set in a configuration file using the ``output_file`` property. |
| 479 |
|
| 480 |
.. option:: --include-ipynb |
| 481 |
|
| 482 |
Include the Python cells within IPython Notebooks in the reporting. |
| 483 |
|
| 484 |
Value can be set in a configuration file using the ``include_ipynb`` property. |
| 485 |
|
| 486 |
.. option:: --ipynb-cells |
| 487 |
|
| 488 |
Report on individual cells in any .ipynb files. |
| 489 |
|
| 490 |
Value can be set in a configuration file using the ``ipynb_cells`` property. |
| 491 |
|
| 492 |
Examples |
| 493 |
++++++++ |
| 494 |
|
| 495 |
:: |
| 496 |
|
| 497 |
$ radon hal file.py |
| 498 |
|
| 499 |
Radon will analyze the given file. |
| 500 |
|
| 501 |
|
| 502 |
:: |
| 503 |
|
| 504 |
$ radon hal path/ |
| 505 |
|
| 506 |
Radon will walk through the subdirectories of ``path/`` and analyze all child |
| 507 |
nodes (every Python file it encounters). |
| 508 |
|
| 509 |
:: |
| 510 |
|
| 511 |
$ radon hal -e 'path/tests*,path/docs/*' path/ |
| 512 |
|
| 513 |
As in the above example, Radon will walk the directories, excluding paths |
| 514 |
matching ``path/tests/*`` and ``path/docs/*``. |
| 515 |
|
| 516 |
.. warning:: |
| 517 |
|
| 518 |
Remember to quote the patterns, otherwise your shell might expand them! |
| 519 |
|
| 520 |
Depending on the single cases, a more suitable alternative might be this:: |
| 521 |
|
| 522 |
$ radon hal -i "docs,tests" path |
| 523 |
|
| 524 |
:: |
| 525 |
|
| 526 |
$ radon hal - < path/to/file.py |
| 527 |
|
| 528 |
Setting the path to "-" will cause Radon to analyze code from stdin. |