Commit 737c0767 authored by John Brooks's avatar John Brooks Committed by Linus Torvalds

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.comSigned-off-by: default avatarJohn Brooks <john@fastquake.com>
Signed-off-by: default avatarJoe Perches <joe@perches.com>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
parent 8d81ae05
...@@ -57,7 +57,7 @@ my $codespell = 0; ...@@ -57,7 +57,7 @@ my $codespell = 0;
my $codespellfile = "/usr/share/codespell/dictionary.txt"; my $codespellfile = "/usr/share/codespell/dictionary.txt";
my $conststructsfile = "$D/const_structs.checkpatch"; my $conststructsfile = "$D/const_structs.checkpatch";
my $typedefsfile = ""; my $typedefsfile = "";
my $color = 1; my $color = "auto";
my $allow_c99_comments = 1; my $allow_c99_comments = 1;
sub help { sub help {
...@@ -116,7 +116,8 @@ Options: ...@@ -116,7 +116,8 @@ Options:
(default:/usr/share/codespell/dictionary.txt) (default:/usr/share/codespell/dictionary.txt)
--codespellfile Use this codespell dictionary --codespellfile Use this codespell dictionary
--typedefsfile Read additional types from this file --typedefsfile Read additional types from this file
--color Use colors when output is STDOUT (default: on) --color[=WHEN] Use colors 'always', 'never', or only when output
is a terminal ('auto'). Default is 'auto'.
-h, --help, --version display this help and exit -h, --help, --version display this help and exit
When FILE is - read standard input. When FILE is - read standard input.
...@@ -182,6 +183,14 @@ if (-f $conf) { ...@@ -182,6 +183,14 @@ if (-f $conf) {
unshift(@ARGV, @conf_args) if @conf_args; unshift(@ARGV, @conf_args) if @conf_args;
} }
# Perl's Getopt::Long allows options to take optional arguments after a space.
# Prevent --color by itself from consuming other arguments
foreach (@ARGV) {
if ($_ eq "--color" || $_ eq "-color") {
$_ = "--color=$color";
}
}
GetOptions( GetOptions(
'q|quiet+' => \$quiet, 'q|quiet+' => \$quiet,
'tree!' => \$tree, 'tree!' => \$tree,
...@@ -212,7 +221,9 @@ GetOptions( ...@@ -212,7 +221,9 @@ GetOptions(
'codespell!' => \$codespell, 'codespell!' => \$codespell,
'codespellfile=s' => \$codespellfile, 'codespellfile=s' => \$codespellfile,
'typedefsfile=s' => \$typedefsfile, 'typedefsfile=s' => \$typedefsfile,
'color!' => \$color, 'color=s' => \$color,
'no-color' => \$color, #keep old behaviors of -nocolor
'nocolor' => \$color, #keep old behaviors of -nocolor
'h|help' => \$help, 'h|help' => \$help,
'version' => \$help 'version' => \$help
) or help(1); ) or help(1);
...@@ -238,6 +249,18 @@ if ($#ARGV < 0) { ...@@ -238,6 +249,18 @@ if ($#ARGV < 0) {
push(@ARGV, '-'); push(@ARGV, '-');
} }
if ($color =~ /^[01]$/) {
$color = !$color;
} elsif ($color =~ /^always$/i) {
$color = 1;
} elsif ($color =~ /^never$/i) {
$color = 0;
} elsif ($color =~ /^auto$/i) {
$color = (-t STDOUT);
} else {
die "Invalid color mode: $color\n";
}
sub hash_save_array_words { sub hash_save_array_words {
my ($hashRef, $arrayRef) = @_; my ($hashRef, $arrayRef) = @_;
...@@ -1883,7 +1906,7 @@ sub report { ...@@ -1883,7 +1906,7 @@ sub report {
return 0; return 0;
} }
my $output = ''; my $output = '';
if (-t STDOUT && $color) { if ($color) {
if ($level eq 'ERROR') { if ($level eq 'ERROR') {
$output .= RED; $output .= RED;
} elsif ($level eq 'WARNING') { } elsif ($level eq 'WARNING') {
...@@ -1894,10 +1917,10 @@ sub report { ...@@ -1894,10 +1917,10 @@ sub report {
} }
$output .= $prefix . $level . ':'; $output .= $prefix . $level . ':';
if ($show_types) { if ($show_types) {
$output .= BLUE if (-t STDOUT && $color); $output .= BLUE if ($color);
$output .= "$type:"; $output .= "$type:";
} }
$output .= RESET if (-t STDOUT && $color); $output .= RESET if ($color);
$output .= ' ' . $msg . "\n"; $output .= ' ' . $msg . "\n";
if ($showfile) { if ($showfile) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment