Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit fd09392

Browse files
committed
[Console] Allow to returns all tokens after the command name
1 parent a9133af commit fd09392

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed

src/Symfony/Component/Console/Input/ArgvInput.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -348,11 +348,29 @@ public function getParameterOption(string|array $values, string|bool|int|float|a
348348
/**
349349
* Returns un-parsed and not validated tokens.
350350
*
351+
* @param bool $strip Whether to return the raw parameters (false) or the values after the command name (true)
351352
* @return list<string>
352353
*/
353-
public function getRawTokens(): array
354+
public function getRawTokens(bool $strip = false): array
354355
{
355-
return $this->tokens;
356+
if (!$strip) {
357+
return $this->tokens;
358+
}
359+
360+
$parameters = [];
361+
$keep = false;
362+
foreach ($this->tokens as $value) {
363+
if ($value === $this->getFirstArgument()) {
364+
$keep = true;
365+
366+
continue;
367+
}
368+
if ($keep) {
369+
$parameters[] = $value;
370+
}
371+
}
372+
373+
return $parameters;
356374
}
357375

358376
/**

src/Symfony/Component/Console/Tests/Input/ArgvInputTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -562,4 +562,30 @@ public function testParseOptionWithValueOptionalGivenEmptyAndOptionalArgument()
562562
$this->assertEquals(['foo' => '0'], $input->getOptions(), '->parse() parses optional options with empty value as null');
563563
$this->assertEquals(['name' => 'bar'], $input->getArguments(), '->parse() parses optional arguments');
564564
}
565+
566+
public function testGetRawTokensFalse()
567+
{
568+
$input = new ArgvInput(['cli.php', '--foo', 'bar']);
569+
$this->assertSame(['--foo', 'bar'], $input->getRawTokens());
570+
}
571+
572+
/**
573+
* @dataProvider provideGetRawTokensTrueTests
574+
*/
575+
public function testGetRawTokensTrue(array $argv, array $expected)
576+
{
577+
$input = new ArgvInput($argv);
578+
$this->assertSame($expected, $input->getRawTokens(true));
579+
}
580+
581+
public static function provideGetRawTokensTrueTests(): iterable
582+
{
583+
yield [['app/console', 'foo:bar'], []];
584+
yield [['app/console', 'foo:bar', '--env=prod'], ['--env=prod']];
585+
yield [['app/console', 'foo:bar', '--env', 'prod'], ['--env', 'prod']];
586+
yield [['app/console', '--no-ansi', 'foo:bar', '--env', 'prod'], ['--env', 'prod']];
587+
yield [['app/console', '--no-ansi', 'foo:bar', '--env', 'prod'], ['--env', 'prod']];
588+
yield [['app/console', '--no-ansi', 'foo:bar', 'argument'], ['argument']];
589+
yield [['app/console', '--no-ansi', 'foo:bar', '--', 'argument'], ['--', 'argument']];
590+
}
565591
}

0 commit comments

Comments
 (0)