History

checkpatch: change format of --color argument to --color[=WHEN]

The boolean --color argument did not offer the ability to force colourized output even if stdout is not a terminal. Change the format of the argument to the familiar --color[=WHEN] construct as seen in common Linux utilities such as git, ls and dmesg, which allows the user to specify whether to colourize output "always", "never", or "auto" when the output is a terminal. The default is "auto". The old command-line uses of --color and --no-color are unchanged.

John Brooks committed 9 years ago

Commit 737c0761 parent 8d81ae01 file · +29 −6Browse files

Showing the changes to scripts (1 of 1 file). Show all

Unified · Split

scripts/checkpatch.pl+29−6
@@ -57,7 +57,7 @@ my $codespell = 0; · 56 unmodified lines
5757 my $codespellfile = "/usr/share/codespell/dictionary.txt";
5858 my $conststructsfile = "$D/const_structs.checkpatch";
5959 my $typedefsfile = "";
60−my $color = 1;
60+my $color = "auto";
6161 my $allow_c99_comments = 1;
6262
6363 sub help {
@@ -116,7 +116,8 @@ Options: · 52 unmodified lines
116116 (default:/usr/share/codespell/dictionary.txt)
117117 --codespellfile Use this codespell dictionary
118118 --typedefsfile Read additional types from this file
119− --color Use colors when output is STDOUT (default: on)
119+ --color[=WHEN] Use colors 'always', 'never', or only when output
120+ is a terminal ('auto'). Default is 'auto'.
120121 -h, --help, --version display this help and exit
121122
122123 When FILE is - read standard input.
@@ -182,6 +183,14 @@ if (-f $conf) { · 59 unmodified lines
182183 unshift(@ARGV, @conf_args) if @conf_args;
183184 }
184185
186+# Perl's Getopt::Long allows options to take optional arguments after a space.
187+# Prevent --color by itself from consuming other arguments
188+foreach (@ARGV) {
189+ if ($_ eq "--color" || $_ eq "-color") {
190+ $_ = "--color=$color";
191+ }
192+}
193+
185194 GetOptions(
186195 'q|quiet+' => \$quiet,
187196 'tree!' => \$tree,
@@ -212,7 +221,9 @@ GetOptions( · 24 unmodified lines
212221 'codespell!' => \$codespell,
213222 'codespellfile=s' => \$codespellfile,
214223 'typedefsfile=s' => \$typedefsfile,
215− 'color!' => \$color,
224+ 'color=s' => \$color,
225+ 'no-color' => \$color, #keep old behaviors of -nocolor
226+ 'nocolor' => \$color, #keep old behaviors of -nocolor
216227 'h|help' => \$help,
217228 'version' => \$help
218229 ) or help(1);
@@ -238,6 +249,18 @@ if ($#ARGV < 0) { · 19 unmodified lines
238249 push(@ARGV, '-');
239250 }
240251
252+if ($color =~ /^[01]$/) {
253+ $color = !$color;
254+} elsif ($color =~ /^always$/i) {
255+ $color = 1;
256+} elsif ($color =~ /^never$/i) {
257+ $color = 0;
258+} elsif ($color =~ /^auto$/i) {
259+ $color = (-t STDOUT);
260+} else {
261+ die "Invalid color mode: $color\n";
262+}
263+
241264 sub hash_save_array_words {
242265 my ($hashRef, $arrayRef) = @_;
243266
@@ -1883,7 +1906,7 @@ sub report { · 1639 unmodified lines
18831906 return 0;
18841907 }
18851908 my $output = '';
1886− if (-t STDOUT && $color) {
1909+ if ($color) {
18871910 if ($level eq 'ERROR') {
18881911 $output .= RED;
18891912 } elsif ($level eq 'WARNING') {
@@ -1894,10 +1917,10 @@ sub report { · 4 unmodified lines
18941917 }
18951918 $output .= $prefix . $level . ':';
18961919 if ($show_types) {
1897− $output .= BLUE if (-t STDOUT && $color);
1920+ $output .= BLUE if ($color);
18981921 $output .= "$type:";
18991922 }
1900− $output .= RESET if (-t STDOUT && $color);
1923+ $output .= RESET if ($color);
19011924 $output .= ' ' . $msg . "\n";
19021925
19031926 if ($showfile) {