Skip to content

Commit 82b048e

Browse files
committed
Add -V and --version CLI arguments
1 parent dfc94ff commit 82b048e

File tree

2 files changed

+80
-1
lines changed

2 files changed

+80
-1
lines changed

Scripts/DocCodeExamples/Config.php

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,10 @@ class Config
9595
'arg' => '--help',
9696
'desc' => 'Print this help.',
9797
],
98+
[
99+
'arg' => '-V, --version',
100+
'desc' => 'Display the current version of this script.',
101+
],
98102
],
99103
// phpcs:enable
100104
];
@@ -108,6 +112,8 @@ class Config
108112
'--exclude=',
109113
'--ignore-sniffs=',
110114
'--help',
115+
'-V',
116+
'--version',
111117
];
112118

113119
/**
@@ -137,12 +143,26 @@ public function getProperty(string $propertyName)
137143
return $this->{$propertyName};
138144
}
139145

146+
/**
147+
* Retrieve the version number of this script.
148+
*
149+
* @return string
150+
*/
151+
public function getVersion()
152+
{
153+
$text = 'PHPCSDevTools: XML documentation code examples checker version ';
154+
$text .= \file_get_contents(__DIR__ . '/../../VERSION');
155+
$text .= \PHP_EOL . 'by PHPCSDevTools Contributors' . \PHP_EOL . \PHP_EOL;
156+
157+
return $text;
158+
}
159+
140160
/**
141161
* Process the received command arguments.
142162
*
143163
* @return void
144164
*/
145-
protected function processCliCommand()
165+
private function processCliCommand()
146166
{
147167
$this->projectRoot = Helper::normalizePath(\getcwd());
148168
$args = $_SERVER['argv'];
@@ -154,11 +174,18 @@ protected function processCliCommand()
154174

155175
if (isset($argsFlipped['--help'])) {
156176
$helpText = HelpTextFormatter::format($this->helpTexts, HelpTextFormatter::isColorSupported());
177+
$this->writer->toStdout($this->getVersion());
157178
$this->writer->toStdout($helpText);
158179
$this->executeCheck = false;
159180
return;
160181
}
161182

183+
if (isset($argsFlipped['-V']) || isset($argsFlipped['--version'])) {
184+
$this->writer->toStdout($this->getVersion());
185+
$this->executeCheck = false;
186+
return;
187+
}
188+
162189
foreach ($args as $arg) {
163190
if (\strpos($arg, '-') === 0 && $this->isUnsupportedNamedArgument($arg) === true) {
164191
throw new \RuntimeException("Unsupported argument $arg");

Tests/DocCodeExamples/ConfigTest.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,20 @@ public static function dataProcessCliCommand(): array
336336
'executeCheck' => false,
337337
],
338338
],
339+
'Version argument short' => [
340+
'command' => './phpcs-check-doc-examples -V',
341+
'expectedChanged' => [
342+
'projectRoot' => $projectRoot,
343+
'executeCheck' => false,
344+
],
345+
],
346+
'Version argument long' => [
347+
'command' => './phpcs-check-doc-examples --version',
348+
'expectedChanged' => [
349+
'projectRoot' => $projectRoot,
350+
'executeCheck' => false,
351+
],
352+
],
339353
];
340354

341355
// Windows only test: verify that the paths are normalized to use forward slashes.
@@ -377,6 +391,44 @@ public function testHelpOutput()
377391
$this->assertStringContainsString('phpcs-check-doc-examples', $output);
378392
}
379393

394+
/**
395+
* Test that the version command outputs the expected text.
396+
*
397+
* @dataProvider dataVersionOutput
398+
*
399+
* @param string $command The command to run.
400+
*
401+
* @return void
402+
*/
403+
public function testVersionOutput($command)
404+
{
405+
$regex = '`^PHPCSDevTools: XML documentation code examples checker version'
406+
. ' [0-9]{1,}\.[0-9]{1,}\.[0-9]{1,}(?:-(?:alpha|beta|RC)\S+)?'
407+
. '[\r\n]+by PHPCSDevTools Contributors[\r\n]*$`';
408+
409+
$_SERVER['argv'] = \explode(' ', $command);
410+
new Config($this->writer);
411+
412+
$this->assertMatchesRegularExpression($regex, $this->writer->getStdout());
413+
}
414+
415+
/**
416+
* Data provider for the testVersionOutput method.
417+
*
418+
* @return array<string, array<string, string>>
419+
*/
420+
public static function dataVersionOutput()
421+
{
422+
return [
423+
'-V' => [
424+
'command' => 'phpcs-check-doc-examples -V',
425+
],
426+
'--version' => [
427+
'command' => 'phpcs-check-doc-examples --version',
428+
],
429+
];
430+
}
431+
380432
/**
381433
* Helper method: retrieve the current values of the Config properties as an array.
382434
*

0 commit comments

Comments
 (0)