Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to exclude shebang from the count #827

Closed
mpsijm opened this issue Apr 13, 2024 · 5 comments
Closed

Add option to exclude shebang from the count #827

mpsijm opened this issue Apr 13, 2024 · 5 comments

Comments

@mpsijm
Copy link

mpsijm commented Apr 13, 2024

Hi! Thanks for maintaining cloc for all these years 😄

I have been manually changing cloc to exclude the shebang from the count for quite some time now, by commenting out this line:

cloc/cloc

Line 7078 in 657038f

unshift @lines, $original_lines[0]; # add the first line back

Can you explain why cloc considers a shebang as a line of code? And if you think that the shebang should be part of the count by default, can we add an option to exclude it? I can probably write the PR myself, I'm just not sure what the option should be called, because something like --exclude-shebang may also imply that the shebang is not used to detect the language, which is not what I want 🙂

@AlDanial
Copy link
Owner

The reason cloc counts the #! line is because this is a line of code. The invoked interpreter acts on arguments given to it. Here's an example:

#!/usr/bin/perl -n -i
print if /4/;

One line or two? If you only count the print statement you miss the bulk of the logic which in this case is "loop over every line in the input file and perform an in-place edit but do not print the result." Adding print if /4/ means that only lines with the character 4 will be saved.

I'll grant you that few scripts with #! add arguments after the interpeter.

Adding a --exclude-pound-bang switch? I don't know, seems a bit much when we're just talking about one line per script. However you're exactly on the right track with regards to making cloc treat the #! line as a comment. Just comment out the entire "Exception for scripting languages" block. In my copy that would be

 7071  #  chomp( $original_lines[0] );
 7072  #  if (defined $Script_Language{$language} and
 7073  #      $original_lines[0] =~ /^#!/ and
 7074  #      (!scalar(@lines) or ($lines[0] ne $original_lines[0]))) {
 7075  #      unshift @lines, $original_lines[0];  # add the first line back
 7076  #  }

The script language is already identified by this point so no harm done.

@mpsijm
Copy link
Author

mpsijm commented Apr 20, 2024

Thanks, I learned something today! 😄 I only ever used standard shebangs with things like /bin/bash and /usr/bin/python3, didn't know that you could also pass extra arguments there 🙂

In my particular use case, I use cloc to find the smallest source file in a folder. All source files solve the same problem, so it's basically a form of code golf. And when the Python files are only a few lines long, adding the shebang line makes a difference 😛

Perhaps we can count the shebang line only for languages where command-line parameters matter (like perl), or only when it actually contains arguments? But, it's also totally fair if you would rather not implement this proposal upstream, given the complicated technicalities. In that case, I'll just keep the commenting-out fix on my own machine 🙂 Thanks for verifying that I'm on the right track though 😄

@AlDanial
Copy link
Owner

AlDanial commented May 1, 2024

But it isn't just perl that uses #! arguments. My work colleagues frequently add -u to the python #! line to un-buffer stdout and stderr, ie, #!/usr/bin/env python3 -u.

In any case, regarding code golf, wouldn't the results be the same if all scripts count #! compared to all of them not counting #! ?

@mpsijm
Copy link
Author

mpsijm commented May 1, 2024

Again, learned something new, thanks! 😄

For the code golf, we also accept files in non-scripting languages, e.g. C++ and Java, which do not require the shebang.

@mpsijm mpsijm changed the title Do not count shebang, or add option to exclude it from the count Add option to exclude shebang from the count May 1, 2024
@AlDanial
Copy link
Owner

Closing as: won't implement.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants