AlDanial / cloc

cloc counts blank lines, comment lines, and physical lines of source code in many programming languages.
GNU General Public License v2.0
19.75k stars 1.02k forks source link

Defining a file filter in config file #852

Closed aladur closed 2 months ago

aladur commented 2 months ago

Describe the bug I couldn't manage do define a file filter (--match-f) within a configuration file.

cloc; OS; OS version

To Reproduce Step1 (file fiter on command line)

$ ~/Downloads/cloc-2.02.pl --match-f='^[a-z][a-z0-9]*\.[a-z]+$' -v src

Output:

Using temp file list [/tmp/URKCGVfz7V]
     277 text files.
     268 unique files.                                          
      27 files ignored.

github.com/AlDanial/cloc v 2.02  T=0.12 s (2326.4 files/s, 571363.4 lines/s)
-----------------------------------------------------------------------------------
Language                         files          blank        comment           code
-----------------------------------------------------------------------------------
C++                                102           6520           3631          30726
C/C++ Header                       130           2658           2793           8567
Text                                10            234              0           3891
Qt                                   8              0              0           3359
XML                                  8              1              0           2071
C                                    7            119             95            838
SVG                                  1              0              0            239
Windows Resource File                2              6              0             72
-----------------------------------------------------------------------------------
SUM:                               268           9538           6519          49763
-----------------------------------------------------------------------------------

OK => file filter correctly applied (verified with --counted). OBSERVATION => No configuration file used.

To Reproduce Step2 (file filter in configuration file)

$ cat $HOME/.config/cloc/options.txt
match-f '^[a-z][a-z0-9]*\.[a-z]+$'
$ ~/Downloads/cloc-2.02.pl -v src

Output:

Reading options from /home/user/.config/cloc/options.txt.
Using temp file list [/tmp/LqQVUOZsKo]
       0 text files.
       0 unique files.                              
       0 files ignored.

OK => Configuration file from default location used FAIL => file filter does not work as expected. Expected would be the same result as in Step1.

Additional context The general question comes up how to define switches in a configuration file? I tried all combinations --match-f='pattern', match-f='pattern', --match-f 'pattern' and match-f 'pattern' as mentioned in https://github.com/AlDanial/cloc/issues/505.

Thanks for this great tool!

AlDanial commented 2 months ago

Your config file is correct; the config file reader was inadvertently including the quotes as part of the regex. Try a97216f

aladur commented 2 months ago

First I did the install step:

$ git clone https://github.com/AlDanial/cloc.git
$ checkout a97216f
$ cd cloc/Unix
$ make prefix=$HOME/temp install
...

Repeat Step2

$ cat $HOME/.config/cloc/options.txt
match-f ^[a-z][a-z0-9]*\.[a-z]+$
$ ~/temp/bin/cloc -v src

Output:

     277 text files.
     268 unique files.                                          
      27 files ignored.

github.com/AlDanial/cloc v 2.02  T=0.12 s (2304.0 files/s, 565867.2 lines/s)
-----------------------------------------------------------------------------------
Language                         files          blank        comment           code
-----------------------------------------------------------------------------------
C++                                102           6520           3631          30726
C/C++ Header                       130           2658           2793           8567
Text                                10            234              0           3891
Qt                                   8              0              0           3359
XML                                  8              1              0           2071
C                                    7            119             95            838
SVG                                  1              0              0            239
Windows Resource File                2              6              0             72
-----------------------------------------------------------------------------------
SUM:                               268           9538           6519          49763
-----------------------------------------------------------------------------------

OK => This is the expected output.

This only works if the file filter in the configuration file has no single/double quotes at all. Is this the expected behavior?

The documentation does not state how to handle quotes in the configuration file. On the command line the example in the command help uses single quotes (-match-f='^[Ww]idget'), which is, as I would assume the best choice to avoid shell interference.

AlDanial commented 2 months ago

The intent of the config file is that its entries match verbatim what you'd put on the command line. Commit a97216f updated the code to ignore these bounding quotes from the regex expressed in the config file. I believe the failure here is due to the regex anchor ref #851, I'll have to tune that still.

aladur commented 2 months ago

Tested again with 8297197:

The initially described Step1 and Step2 now work as expected. For Step2 the file filter regex in file options.txt can be specified with single or double quotes or even without quotes. Thanks for the very quick response and fix! Ready to go for release 2.03 :-)