From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751770AbdFFRH7 (ORCPT ); Tue, 6 Jun 2017 13:07:59 -0400 Received: from mail.fastquake.com ([45.33.83.177]:60166 "EHLO mail.fastquake.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751750AbdFFRH5 (ORCPT ); Tue, 6 Jun 2017 13:07:57 -0400 From: John Brooks To: Joe Perches Cc: Andy Whitcroft , linux-kernel@vger.kernel.org Subject: [PATCH v2] checkpatch: Change format of --color argument to --color[=WHEN] Date: Tue, 6 Jun 2017 13:07:12 -0400 Message-Id: <1496768832-31004-1-git-send-email-john@fastquake.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1496704230.1968.5.camel@perches.com> References: <1496704230.1968.5.camel@perches.com> Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 ls and dmesg, which allows the user to specify whether to colourize output always, never, or only when the output is a terminal ("auto"). Because the option is no longer boolean, --nocolor (or --no-color) is no longer available. Users of the old negative option should use --color=never instead. v2 (Joe Perches): - Prevent --color from consuming other args in e.g. ./scripts/checkpatch.pl --color foo.patch Signed-off-by: John Brooks --- scripts/checkpatch.pl | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl index 4b9569f..9d03ae0 100755 --- a/scripts/checkpatch.pl +++ b/scripts/checkpatch.pl @@ -56,7 +56,7 @@ my $codespell = 0; my $codespellfile = "/usr/share/codespell/dictionary.txt"; my $conststructsfile = "$D/const_structs.checkpatch"; my $typedefsfile = ""; -my $color = 1; +my $color = "auto"; my $allow_c99_comments = 1; sub help { @@ -115,7 +115,8 @@ Options: (default:/usr/share/codespell/dictionary.txt) --codespellfile Use this codespell dictionary --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 When FILE is - read standard input. @@ -181,6 +182,14 @@ if (-f $conf) { 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") { + $_ = "--color=$color"; + } +} + GetOptions( 'q|quiet+' => \$quiet, 'tree!' => \$tree, @@ -211,7 +220,7 @@ GetOptions( 'codespell!' => \$codespell, 'codespellfile=s' => \$codespellfile, 'typedefsfile=s' => \$typedefsfile, - 'color!' => \$color, + 'color:s' => \$color, 'h|help' => \$help, 'version' => \$help ) or help(1); @@ -237,6 +246,16 @@ if ($#ARGV < 0) { push(@ARGV, '-'); } +if ($color eq "always") { + $color = 1; +} elsif ($color eq "never") { + $color = 0; +} elsif ($color eq "auto") { + $color = (-t STDOUT); +} else { + die "Invalid color mode: $color\n"; +} + sub hash_save_array_words { my ($hashRef, $arrayRef) = @_; @@ -1881,7 +1900,7 @@ sub report { return 0; } my $output = ''; - if (-t STDOUT && $color) { + if ($color) { if ($level eq 'ERROR') { $output .= RED; } elsif ($level eq 'WARNING') { @@ -1892,10 +1911,10 @@ sub report { } $output .= $prefix . $level . ':'; if ($show_types) { - $output .= BLUE if (-t STDOUT && $color); + $output .= BLUE if ($color); $output .= "$type:"; } - $output .= RESET if (-t STDOUT && $color); + $output .= RESET if ($color); $output .= ' ' . $msg . "\n"; if ($showfile) { -- 2.7.4