Skip to content

Commit 7d7801e

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

File tree

4 files changed

+71
-4
lines changed

4 files changed

+71
-4
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: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
use PHPCSDevTools\Scripts\DocCodeExamples\Config;
1414
use PHPCSDevTools\Scripts\DocCodeExamples\Helper;
15+
use PHPCSDevTools\Scripts\Utils\HelpTextFormatter;
1516
use PHPCSDevTools\Tests\TestWriter;
1617
use Yoast\PHPUnitPolyfills\TestCases\XTestCase;
1718

@@ -34,6 +35,7 @@ final class ConfigTest extends XTestCase
3435
'excludedDirs' => [],
3536
'ignoredSniffs' => [],
3637
'executeCheck' => true,
38+
'showColored' => null, // Defined in setUpPrerequisites() as the default value depends on the environment.
3739
];
3840

3941
/**
@@ -52,7 +54,8 @@ final class ConfigTest extends XTestCase
5254
*/
5355
public function setUpPrerequisites()
5456
{
55-
$this->writer = new TestWriter();
57+
$this->writer = new TestWriter();
58+
$this->defaultSettings['showColored'] = HelpTextFormatter::isColorSupported();
5659
}
5760

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

355398
// 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)