Skip to content

Commit e00380e

Browse files
committed
Add CLI arguments to control colors in the output
1 parent 91156d2 commit e00380e

File tree

4 files changed

+68
-3
lines changed

4 files changed

+68
-3
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,8 @@ directories|files One or more specific directories or files to exa
172172
--exclude=<dir1,dir2> Comma-delimited list of relative paths of directories to
173173
exclude from the scan.
174174
--ignore-sniffs=<sniff1,sniff2> Comma-delimited list of sniffs to ignore.
175+
--colors Enable colors in console output. (disables auto detection of color support)
176+
--no-colors Disable colors in console output.
175177
--help Print the script help page.
176178
-V, --version Display the current version of this script.
177179
```

Scripts/DocCodeExamples/Config.php

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,14 @@ class Config
108108
'arg' => '-V, --version',
109109
'desc' => 'Display the current version of this script.',
110110
],
111+
[
112+
'arg' => '--colors',
113+
'desc' => 'Enable colors in console output. (disables auto detection of color support)',
114+
],
115+
[
116+
'arg' => '--no-colors',
117+
'desc' => 'Disable colors in console output.',
118+
],
111119
],
112120
// phpcs:enable
113121
];
@@ -123,6 +131,8 @@ class Config
123131
'--help',
124132
'-V',
125133
'--version',
134+
'--colors',
135+
'--no-colors',
126136
];
127137

128138
/**
@@ -132,8 +142,7 @@ class Config
132142
*/
133143
public function __construct(Writer $writer)
134144
{
135-
$this->writer = $writer;
136-
$this->showColored = HelpTextFormatter::isColorSupported();
145+
$this->writer = $writer;
137146
$this->processCliCommand();
138147
}
139148

@@ -182,6 +191,14 @@ private function processCliCommand()
182191

183192
$argsFlipped = \array_flip($args);
184193

194+
if (isset($argsFlipped['--no-colors'])) {
195+
$this->showColored = false;
196+
} elseif (isset($argsFlipped['--colors'])) {
197+
$this->showColored = true;
198+
} else {
199+
$this->showColored = HelpTextFormatter::isColorSupported();
200+
}
201+
185202
if (isset($argsFlipped['--help'])) {
186203
$helpText = HelpTextFormatter::format($this->helpTexts, $this->showColored);
187204
$this->writer->toStdout($this->getVersion());

Tests/DocCodeExamples/ConfigTest.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ final class ConfigTest extends XTestCase
3434
'excludedDirs' => [],
3535
'ignoredSniffs' => [],
3636
'executeCheck' => true,
37+
'showColored' => true,
3738
];
3839

3940
/**
@@ -350,6 +351,46 @@ public static function dataProcessCliCommand(): array
350351
'executeCheck' => false,
351352
],
352353
],
354+
'Colors argument' => [
355+
'command' => './phpcs-check-doc-examples --colors',
356+
'expectedChanged' => [
357+
'projectRoot' => $projectRoot,
358+
'targetPaths' => [
359+
$projectRoot,
360+
],
361+
'showColored' => true,
362+
],
363+
],
364+
'No colors argument' => [
365+
'command' => './phpcs-check-doc-examples --no-colors',
366+
'expectedChanged' => [
367+
'projectRoot' => $projectRoot,
368+
'targetPaths' => [
369+
$projectRoot,
370+
],
371+
'showColored' => false,
372+
],
373+
],
374+
'Both colors and no-colors arguments (no-colors should win)' => [
375+
'command' => './phpcs-check-doc-examples --no-colors --colors',
376+
'expectedChanged' => [
377+
'projectRoot' => $projectRoot,
378+
'targetPaths' => [
379+
$projectRoot,
380+
],
381+
'showColored' => false,
382+
],
383+
],
384+
'Both colors and no-colors arguments in reverse order (no-colors should win)' => [
385+
'command' => './phpcs-check-doc-examples --colors --no-colors',
386+
'expectedChanged' => [
387+
'projectRoot' => $projectRoot,
388+
'targetPaths' => [
389+
$projectRoot,
390+
],
391+
'showColored' => false,
392+
],
393+
],
353394
];
354395

355396
// Windows only test: verify that the paths are normalized to use forward slashes.

Tests/DocCodeExamples/EndToEndTest.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,12 @@ public static function tearDownFixturesAfterClass()
9797
public function testScriptBasicExecution(string $cliArgs, int $expectedExitCode, string $expectedStdout, string $expectedStderr)
9898
{
9999
$scriptPath = self::maybeConvertDirectorySeparators(self::SCRIPT_PATH);
100-
$command = \sprintf(
100+
101+
// Force --no-colors to avoid issues with the tests as colors might be auto enabled or
102+
// disabled depending on the environment.
103+
$cliArgs .= ' --no-colors';
104+
105+
$command = \sprintf(
101106
'php %s %s',
102107
$scriptPath,
103108
$cliArgs

0 commit comments

Comments
 (0)