10000 [Console] Allow to returns all tokens after the command name · symfony/symfony@cef1979 · GitHub
[go: up one dir, main page]

Skip to content

Commit cef1979

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

File tree

2 files changed

+48
-2
lines changed

2 files changed

+48
-2
lines changed

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

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -348,11 +348,30 @@ 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)
352+
*
351353
* @return list<string>
352354
*/
353-
public function getRawTokens(): array
355+
public function getRawTokens(bool $strip = false): array
354356
{
355-
return $this->tokens;
357+
if (!$strip) {
358+
return $this->tokens;
359+
}
360+
361+
$parameters = [];
362+
$keep = false;
363+
foreach ($this->tokens as $value) {
364+
if (!$keep && $value === $this->getFirstArgument()) {
365+
$keep = true;
366+
367+
continue;
368+
}
369+
if ($keep) {
370+
$parameters[] = $value;
371+
}
372+
}
373+
374+
return $parameters;
356375
}
357376

358377
/**

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -562,4 +562,31 @@ 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', 'foo:bar'], ['foo:bar']];
590+
yield [['app/console', '--no-ansi', 'foo:bar', '--', 'argument'], ['--', 'argument']];
591+
}
565592
}

0 commit comments

Comments
 (0)
0