From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751531AbdFFTYD (ORCPT ); Tue, 6 Jun 2017 15:24:03 -0400 Received: from smtprelay0054.hostedemail.com ([216.40.44.54]:34200 "EHLO smtprelay.hostedemail.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1751426AbdFFTXd (ORCPT ); Tue, 6 Jun 2017 15:23:33 -0400 X-Session-Marker: 6A6F6540706572636865732E636F6D X-Spam-Summary: 2,0,0,,d41d8cd98f00b204,joe@perches.com,:::::::,RULES_HIT:41:355:379:541:599:960:973:982:983:988:989:1208:1260:1277:1311:1313:1314:1345:1359:1373:1437:1515:1516:1518:1534:1543:1593:1594:1605:1711:1730:1747:1777:1792:1801:2198:2199:2393:2559:2562:2828:3138:3139:3140:3141:3142:3622:3653:3865:3866:3867:3868:3870:3871:3872:4250:4321:4605:5007:6119:6299:7875:7903:8957:9040:9121:10004:10400:10848:11026:11232:11233:11658:11914:12043:12291:12438:12555:12683:12740:12760:12895:13439:14181:14659:14721:14819:21080:21222:21451:21627:30051:30054:30062:30070:30091,0,RBL:none,CacheIP:none,Bayesian:0.5,0.5,0.5,Netcheck:none,DomainCache:0,MSF:not bulk,SPF:,MSBL:0,DNSBL:none,Custom_rules:0:0:0,LFtime:2,LUA_SUMMARY:none X-HE-Tag: spark53_179f70643d35 X-Filterd-Recvd-Size: 4480 Message-ID: <1496776882.1968.27.camel@perches.com> Subject: Re: [PATCH v2] checkpatch: Change format of --color argument to --color[=WHEN] From: Joe Perches To: John Brooks Cc: Andy Whitcroft , linux-kernel@vger.kernel.org, Andrew Morton In-Reply-To: <1496768832-31004-1-git-send-email-john@fastquake.com> References: <1496704230.1968.5.camel@perches.com> <1496768832-31004-1-git-send-email-john@fastquake.com> Content-Type: text/plain; charset="ISO-8859-1" Date: Tue, 06 Jun 2017 12:21:22 -0700 Mime-Version: 1.0 X-Mailer: Evolution 3.22.6-1ubuntu1 Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, 2017-06-06 at 13:07 -0400, John Brooks wrote: > 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. It is possible to add --nocolor and --no-color to the arguments for GetOptions to keep the old behavior intact. I think this works: --- scripts/checkpatch.pl | 35 +++++++++++++++++++++++++++++------ 1 file changed, 29 insertions(+), 6 deletions(-) diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl index 4b9569fa931b..372d541c2c46 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" || $_ eq "-color") { + $_ = "--color=$color"; + } +} + GetOptions( 'q|quiet+' => \$quiet, 'tree!' => \$tree, @@ -211,7 +220,9 @@ GetOptions( 'codespell!' => \$codespell, 'codespellfile=s' => \$codespellfile, '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, 'version' => \$help ) or help(1); @@ -237,6 +248,18 @@ if ($#ARGV < 0) { 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 { my ($hashRef, $arrayRef) = @_; @@ -1881,7 +1904,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 +1915,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) {