History

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.

John Brooks committed 9 years ago

Commit 737c0761 parent 8d81ae01 file · +29 −6Browse files

Showing the changes to scripts/checkpatch.pl (1 of 1 file). Show all

Unified · Split

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