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.
Showing the changes to scripts/checkpatch.pl (1 of 1 file). Show all
Unified · Split
| @@ -57,7 +57,7 @@ my $codespell = 0; · 56 unmodified lines | ||
| 57 | 57 | my $codespellfile = "/usr/share/codespell/dictionary.txt"; |
| 58 | 58 | my $conststructsfile = "$D/const_structs.checkpatch"; |
| 59 | 59 | my $typedefsfile = ""; |
| 60 | −my $color = 1; | |
| 60 | +my $color = "auto"; | |
| 61 | 61 | my $allow_c99_comments = 1; |
| 62 | 62 | |
| 63 | 63 | sub help { |
| @@ -116,7 +116,8 @@ Options: · 52 unmodified lines | ||
| 116 | 116 | (default:/usr/share/codespell/dictionary.txt) |
| 117 | 117 | --codespellfile Use this codespell dictionary |
| 118 | 118 | --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'. | |
| 120 | 121 | -h, --help, --version display this help and exit |
| 121 | 122 | |
| 122 | 123 | When FILE is - read standard input. |
| @@ -182,6 +183,14 @@ if (-f $conf) { · 59 unmodified lines | ||
| 182 | 183 | unshift(@ARGV, @conf_args) if @conf_args; |
| 183 | 184 | } |
| 184 | 185 | |
| 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 | + | |
| 185 | 194 | GetOptions( |
| 186 | 195 | 'q|quiet+' => \$quiet, |
| 187 | 196 | 'tree!' => \$tree, |
| @@ -212,7 +221,9 @@ GetOptions( · 24 unmodified lines | ||
| 212 | 221 | 'codespell!' => \$codespell, |
| 213 | 222 | 'codespellfile=s' => \$codespellfile, |
| 214 | 223 | '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 | |
| 216 | 227 | 'h|help' => \$help, |
| 217 | 228 | 'version' => \$help |
| 218 | 229 | ) or help(1); |
| @@ -238,6 +249,18 @@ if ($#ARGV < 0) { · 19 unmodified lines | ||
| 238 | 249 | push(@ARGV, '-'); |
| 239 | 250 | } |
| 240 | 251 | |
| 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 | + | |
| 241 | 264 | sub hash_save_array_words { |
| 242 | 265 | my ($hashRef, $arrayRef) = @_; |
| 243 | 266 | |
| @@ -1883,7 +1906,7 @@ sub report { · 1639 unmodified lines | ||
| 1883 | 1906 | return 0; |
| 1884 | 1907 | } |
| 1885 | 1908 | my $output = ''; |
| 1886 | − if (-t STDOUT && $color) { | |
| 1909 | + if ($color) { | |
| 1887 | 1910 | if ($level eq 'ERROR') { |
| 1888 | 1911 | $output .= RED; |
| 1889 | 1912 | } elsif ($level eq 'WARNING') { |
| @@ -1894,10 +1917,10 @@ sub report { · 4 unmodified lines | ||
| 1894 | 1917 | } |
| 1895 | 1918 | $output .= $prefix . $level . ':'; |
| 1896 | 1919 | if ($show_types) { |
| 1897 | − $output .= BLUE if (-t STDOUT && $color); | |
| 1920 | + $output .= BLUE if ($color); | |
| 1898 | 1921 | $output .= "$type:"; |
| 1899 | 1922 | } |
| 1900 | − $output .= RESET if (-t STDOUT && $color); | |
| 1923 | + $output .= RESET if ($color); | |
| 1901 | 1924 | $output .= ' ' . $msg . "\n"; |
| 1902 | 1925 | |
| 1903 | 1926 | if ($showfile) { |