Skip to content

Commit 737c076

Browse files
Frogging101torvalds
authored andcommitted
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. Link: http://lkml.kernel.org/r/efe43bdbad400f39ba691ae663044462493b0773.1496799721.git.joe@perches.com Signed-off-by: John Brooks <[email protected]> Signed-off-by: Joe Perches <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent 8d81ae0 commit 737c076

File tree

1 file changed

+29
-6
lines changed

1 file changed

+29
-6
lines changed

scripts/checkpatch.pl

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
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 @@ sub help {
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 @@ sub list_types {
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 @@ sub list_types {
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 @@ sub list_types {
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 {
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 {
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) {

0 commit comments

Comments
 (0)