Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Console] Allow to return all tokens after the command name #54347

Merged
merged 1 commit into from Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 21 additions & 2 deletions src/Symfony/Component/Console/Input/ArgvInput.php
Expand Up @@ -66,7 +66,7 @@
protected function parse(): void
{
$parseOptions = true;
$this->parsed = $this->tokens;

Check failure on line 69 in src/Symfony/Component/Console/Input/ArgvInput.php

View workflow job for this annotation

GitHub Actions / Psalm

InaccessibleProperty

src/Symfony/Component/Console/Input/ArgvInput.php:69:25: InaccessibleProperty: Cannot access private property Symfony\Component\Console\Completion\CompletionInput::$tokens from context Symfony\Component\Console\Input\ArgvInput (see https://psalm.dev/054)

Check failure on line 69 in src/Symfony/Component/Console/Input/ArgvInput.php

View workflow job for this annotation

GitHub Actions / Psalm

InaccessibleProperty

src/Symfony/Component/Console/Input/ArgvInput.php:69:25: InaccessibleProperty: Cannot access private property Symfony\Component\Console\Completion\CompletionInput::$tokens from context Symfony\Component\Console\Input\ArgvInput (see https://psalm.dev/054)
while (null !== $token = array_shift($this->parsed)) {
$parseOptions = $this->parseToken($token, $parseOptions);
}
Expand Down Expand Up @@ -348,11 +348,30 @@
/**
* Returns un-parsed and not validated tokens.
*
* @param bool $strip Whether to return the raw parameters (false) or the values after the command name (true)
*
* @return list<string>
*/
public function getRawTokens(): array
public function getRawTokens(bool $strip = false): array
{
return $this->tokens;
if (!$strip) {
return $this->tokens;
}

$parameters = [];
$keep = false;
foreach ($this->tokens as $value) {
if (!$keep && $value === $this->getFirstArgument()) {
$keep = true;

continue;
}
if ($keep) {
$parameters[] = $value;
}
}

return $parameters;
}

/**
Expand Down
27 changes: 27 additions & 0 deletions src/Symfony/Component/Console/Tests/Input/ArgvInputTest.php
Expand Up @@ -562,4 +562,31 @@ public function testParseOptionWithValueOptionalGivenEmptyAndOptionalArgument()
$this->assertEquals(['foo' => '0'], $input->getOptions(), '->parse() parses optional options with empty value as null');
$this->assertEquals(['name' => 'bar'], $input->getArguments(), '->parse() parses optional arguments');
}

public function testGetRawTokensFalse()
{
$input = new ArgvInput(['cli.php', '--foo', 'bar']);
$this->assertSame(['--foo', 'bar'], $input->getRawTokens());
}

/**
* @dataProvider provideGetRawTokensTrueTests
*/
public function testGetRawTokensTrue(array $argv, array $expected)
{
$input = new ArgvInput($argv);
$this->assertSame($expected, $input->getRawTokens(true));
}

public static function provideGetRawTokensTrueTests(): iterable
{
yield [['app/console', 'foo:bar'], []];
yield [['app/console', 'foo:bar', '--env=prod'], ['--env=prod']];
yield [['app/console', 'foo:bar', '--env', 'prod'], ['--env', 'prod']];
yield [['app/console', '--no-ansi', 'foo:bar', '--env', 'prod'], ['--env', 'prod']];
yield [['app/console', '--no-ansi', 'foo:bar', '--env', 'prod'], ['--env', 'prod']];
yield [['app/console', '--no-ansi', 'foo:bar', 'argument'], ['argument']];
yield [['app/console', '--no-ansi', 'foo:bar', 'foo:bar'], ['foo:bar']];
yield [['app/console', '--no-ansi', 'foo:bar', '--', 'argument'], ['--', 'argument']];
}
}