File: commandline.rst

package info (click to toggle)
radon 6.0.1%2Bds1-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 952 kB
  • sloc: python: 4,890; makefile: 185
file content (528 lines) | stat: -rw-r--r-- 15,234 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
Command-line Usage
==================

Radon currently has four commands:

    * :command:`cc`: compute Cyclomatic Complexity
    * :command:`raw`: compute raw metrics
    * :command:`mi`: compute Maintainability Index
    * :command:`hal`: compute Halstead complexity metrics

.. note::
    On some systems, such as Windows, the default encoding is not UTF-8. If you
    are using Unicode characters in your Python file and want to analyze it
    with Radon, you'll have to set the `RADONFILESENCODING` environment
    variable to `UTF-8`.

Radon configuration files
-------------------------

When using radon regularly, you may want to specify default argument values in a configuration file.
For example, all of the radon commands have a ``--exclude`` and ``--ignore`` argument on the command-line.

Radon will look for the following files to determine default arguments:

* ``radon.cfg``
* ``pyproject.toml`` (with optional `toml` install on Python <3.11)
* ``setup.cfg``
* ``~/.radon.cfg``

Any radon configuration will be given in the INI-format, under the section ``[radon]``.

For example:

.. code-block:: ini

   [radon]
   exclude = test_*.py
   cc_min = B

Usage with Jupyter Notebooks
----------------------------

Radon can be used with ``.ipynb`` files to inspect code metrics for Python cells. Any ``%`` macros will be ignored in the metrics.

.. note::

   Jupyter Notebook support requires the optional ``nbformat`` package. To install, run ``pip install nbformat``.

To enable scanning of Jupyter notebooks, add the ``--include-ipynb`` flag with any of the commands.

To enable reporting of individual cells, add the ``--ipynb-cells`` flag with any of the commands.

The :command:`cc` command
-------------------------

.. program:: cc

This command analyzes Python source files and compute Cyclomatic Complexity.
The output can be filtered by specifying the :option:`-n` and :option:`-x`
flags. By default, the complexity score is not displayed, the option
:option:`-s` (show complexity) toggles this behaviour. File or directories
exclusion is supported through glob patterns. Every positional argument is
interpreted as a path. The program then walks through its children and analyzes
Python files.
Every block will be ranked from A (best complexity score) to F (worst one).
Ranks corresponds to complexity scores as follows:

    ========== ====== =========================================
     CC score   Rank   Risk
    ========== ====== =========================================
     1 - 5      A      low - simple block
     6 - 10     B      low - well structured and stable block
     11 - 20    C      moderate - slightly complex block
     21 - 30    D      more than moderate - more complex block
     31 - 40    E      high - complex block, alarming
       41+      F      very high - error-prone, unstable block
    ========== ====== =========================================

Blocks are also classified into three types: functions, methods and classes.
They're listed by letter in the command output for convenience when scanning
through a longer list of blocks:

    ============ ========
     Block type   Letter
    ============ ========
     Function     F
     Method       M
     Class        C
    ============ ========

Options
+++++++

.. option:: -x, --max

   Set the maximum complexity rank to display, defaults to ``F``.

   Value can be set in a configuration file using the ``cc_max`` property.

.. option:: -n, --min

   Set the minimum complexity rank to display, defaults to ``A``.

   Value can be set in a configuration file using the ``cc_min`` property.

.. option:: -a, --average

   If given, at the end of the analysis show the average Cyclomatic
   Complexity. This option is influenced by :option:`-x, --max` and
   :option:`-n, --min` options.

   Value can be set in a configuration file using the ``average`` property.

.. option:: --total-average

   Like :option:`-a, --average`, but it is not influenced by `min` and `max`.
   Every analyzed block is counted, no matter whether it is displayed or not.

   Value can be set in a configuration file using the ``total_average`` property.

.. option:: -s, --show-complexity

   If given, show the complexity score along with its rank.

   Value can be set in a configuration file using the ``show_complexity`` property.

.. option:: -e, --exclude

   Exclude files only when their path matches one of these glob patterns.
   Usually needs quoting at the command line.

   Value can be set in a configuration file using the ``exclude`` property.

.. option:: -i, --ignore

   Ignore directories when their name matches one of these glob patterns: radon
   won't even descend into them. By default, hidden directories (starting with
   '.') are ignored.

   Value can be set in a configuration file using the ``ignore`` property.

.. option:: -o, --order

   The ordering function for the results. Can be one of:

    * `SCORE`: order by cyclomatic complexity (descending):
    * `LINES`: order by line numbers;
    * `ALPHA`: order by block names (alphabetically).

   Value can be set in a configuration file using the ``order`` property.

.. option:: -j, --json

   If given, the results will be converted into JSON. This is useful in case
   you need to export the results to another application.

.. option:: --xml

   If given, the results will be converted into XML. Note that not all the
   information is kept. This is specifically targeted to Jenkin's plugin CCM.

.. option:: --md

   If given, the results will be converted into Markdown. Note that not all the
   information is kept.

.. option:: --no-assert

   Does not count assert statements when computing complexity. This is because
   Python can be run with an optimize flag which removes assert statements.

   Value can be set in a configuration file using the ``no_assert`` property.

.. option:: --include-ipynb

   Include the Python cells within IPython Notebooks in the reporting.

   Value can be set in a configuration file using the ``include_ipynb`` property.

.. option:: --ipynb-cells

   Report on individual cells in any .ipynb files.

   Value can be set in a configuration file using the ``ipynb_cells`` property.

.. option:: -O, --output-file

   Save output to the specified output file.

   Value can be set in a configuration file using the ``output_file`` property.

Examples
++++++++

::

    $ radon cc path

Radon will walk through the subdirectories of path and will analyze all
child nodes (every Python file it encounters).

::

    $ radon cc -e "path/tests*,path/docs/*" path

As in the above example, Radon will walk the directories, excluding paths
matching ``path/tests/*`` and ``path/docs/*``.

.. warning::

   Remember to quote the patterns, otherwise your shell might expand them!

Depending on the single cases, a more suitable alternative might be this::

    $ radon cc -i "docs,tests" path

::

    $ cat path/to/file.py | radon cc -

Setting the path to "-" will cause Radon to analyze code from stdin

::

    $ radon cc --min B --max E path

Here Radon will only display blocks ranked between B and E (i.e. from ``CC=6``
to ``CC=40``).


The :command:`mi` command
-------------------------

.. program:: mi

This command analyzes Python source code files and compute the Maintainability
Index score.
Every positional argument is treated as a starting point from which to walk
looking for Python files (as in the :command:`cc` command). Paths can be
excluded with the :option:`-e` option.
The Maintainability Index is always in the range 0-100. MI is ranked as
follows:

    ========== ====== =================
     MI score   Rank   Maintainability
    ========== ====== =================
     100 - 20    A     Very high
      19 - 10    B     Medium
       9 - 0     C     Extremely low
    ========== ====== =================


Options
+++++++

.. option:: -x, --max

   Set the maximum MI to display. Expects a letter between A-F. Defaults to ``C``.

   Value can be set in a configuration file using the ``mi_max`` property.

.. option:: -n, --min

   Set the minimum MI to display. Expects a letter between A-F. Defaults to ``A``.

   Value can be set in a configuration file using the ``mi_min`` property.

.. option:: -e, --exclude

   Exclude files only when their path matches one of these glob patterns.
   Usually needs quoting at the command line.

   Value can be set in a configuration file using the ``exclude`` property.

.. option:: -i, --ignore

   Ignore directories when their name matches one of these glob patterns: radon
   won't even descend into them. By default, hidden directories (starting with
   '.') are ignored.

   Value can be set in a configuration file using the ``ignore`` property.

.. option:: -m, --multi

   If given, Radon will not count multiline strings as comments.
   Most of the time this is safe since multiline strings are used as functions
   docstrings, but one should be aware that their use is not limited to that
   and sometimes it would be wrong to count them as comment lines.

   Value can be set in a configuration file using the ``multi`` property.

.. option:: -s, --show

   If given, the actual MI value is shown in results, alongside the rank.

   Value can be set in a configuration file using the ``show_mi`` property.

.. option:: -j, --json

   Format results in JSON.

.. option:: --include-ipynb

   Include the Python cells within IPython Notebooks in the reporting.

   Value can be set in a configuration file using the ``include_ipynb`` property.

.. option:: --ipynb-cells

   Report on individual cells in any .ipynb files.

   Value can be set in a configuration file using the ``ipynb_cells`` property.

.. option:: -O, --output-file

   Save output to the specified output file.

   Value can be set in a configuration file using the ``output_file`` property.


Examples
++++++++

::

    $ radon mi path1 path2

Analyze every Python file under *path1* or *path2*. It checks recursively in
every subdirectory.


::

    $ radon mi path1 -e "path1/tests/*"

Like the previous example, but excluding from the analysis every path that
matches `path1/tests/*`.

::

    $ radon mi -m path1

Like the previous examples, but does not count multiline strings as comments.


The :command:`raw` command
--------------------------

.. program:: raw

This command analyzes the given Python modules in order to compute raw metrics.
These include:

    * **LOC**: the total number of lines of code
    * **LLOC**: the number of logical lines of code
    * **SLOC**: the number of source lines of code - not necessarily
      corresponding to the **LLOC** [Wikipedia]_
    * **comments**: the number of Python comment lines (i.e. only single-line
      comments ``#``)
    * **multi**: the number of lines representing multi-line strings
    * **blank**: the number of blank lines (or whitespace-only ones)

The equation :math:`sloc + multi + single comments + blank = loc` should always
hold.

.. [Wikipedia] More information on **LOC**, **SLOC**, **LLOC** here: http://en.wikipedia.org/wiki/Source_lines_of_code


Options
+++++++

.. option:: -e, --exclude

   Exclude files only when their path matches one of these glob patterns.
   Usually needs quoting at the command line.

   Value can be set in a configuration file using the ``exclude`` property.

.. option:: -i, --ignore

   Ignore directories when their name matches one of these glob patterns: radon
   won't even descend into them. By default, hidden directories (starting with
   '.') are ignored.

   Value can be set in a configuration file using the ``ignore`` property.

.. option:: -s, --summary

   If given, at the end of the analysis a summary of the gathered
   metrics will be shown.

.. option:: -j, --json

   If given, the results will be converted into JSON. Note that the JSON export
   does not include the summary (enabled with the option `-s, --summary`).

.. option:: -O, --output-file

   Save output to the specified output file.

   Value can be set in a configuration file using the ``output_file`` property.

.. option:: --include-ipynb

   Include the Python cells within IPython Notebooks in the reporting.

   Value can be set in a configuration file using the ``include_ipynb`` property.

.. option:: --ipynb-cells

   Report on individual cells in any .ipynb files.

   Value can be set in a configuration file using the ``ipynb_cells`` property.

Examples
++++++++

::

    $ radon raw path1 path2

Analyze every Python file under *path1* or *path2*. It checks recursively in
every subdirectory.

::

    $ radon raw path1 -e "path1/tests/*"

Like the previous example, but excluding from the analysis every path that
matches ``path1/tests/*``.


The :command:`hal` command
--------------------------

.. program:: hal

This command analyzes Python source files and computes their Halstead
complexity metrics. Files can be analyzed as wholes, or in terms of their
top-level functions with the :option:`-f` flag.

Excluding files or directories is supported through glob patterns with the
:option:`-e` flag. Every positional argument is interpreted as a path. The
program walks through its children and analyzes Python files.

Options
+++++++

.. option:: -f, --functions

   Compute the metrics on the *function* level, as opposed to the *file* level.

   Value can be set in a configuration file using the ``functions`` property.

.. option:: -e, --exclude

   Exclude files when their path matches one of these glob patterns. Usually
   needs quoting at the command line.

   Value can be set in a configuration file using the ``exclude`` property.

.. option:: -i, --ignore

   Refuse to descend into directories that match any of these glob patterns. By
   default, hidden directories (starting with '.') are ignored.

   Value can be set in a configuration file using the ``ignore`` property.

.. option:: -j, --json

   Convert results into JSON. This is useful for exporting results to another
   application.

.. option:: -O, --output-file

   Save output to the specified output file.

   Value can be set in a configuration file using the ``output_file`` property.

.. option:: --include-ipynb

   Include the Python cells within IPython Notebooks in the reporting.

   Value can be set in a configuration file using the ``include_ipynb`` property.

.. option:: --ipynb-cells

   Report on individual cells in any .ipynb files.

   Value can be set in a configuration file using the ``ipynb_cells`` property.

Examples
++++++++

::

    $ radon hal file.py

Radon will analyze the given file.


::

    $ radon hal path/

Radon will walk through the subdirectories of ``path/`` and analyze all child
nodes (every Python file it encounters).

::

    $ radon hal -e 'path/tests*,path/docs/*' path/

As in the above example, Radon will walk the directories, excluding paths
matching ``path/tests/*`` and ``path/docs/*``.

.. warning::

   Remember to quote the patterns, otherwise your shell might expand them!

Depending on the single cases, a more suitable alternative might be this::

    $ radon hal -i "docs,tests" path

::

    $ radon hal - < path/to/file.py

Setting the path to "-" will cause Radon to analyze code from stdin.