Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit a51e675

Browse filesBrowse files
committed
feature #54347 [Console] Allow to return all tokens after the command name (lyrixx)
This PR was merged into the 7.1 branch. Discussion ---------- [Console] Allow to return all tokens after the command name | Q | A | ------------- | --- | Branch? | 7.1 | Bug fix? | no | New feature? | yes | Deprecations? | no | Issues | | License | MIT follows #54238 Now, we can make it works at the command level (previous PR was at the application level) ```php #!/usr/bin/env php <?php use Symfony\Component\Console\Application; use Symfony\Component\Console\Command\Command; use Symfony\Component\Process\Process; require __DIR__ . '/vendor/autoload.php'; $command = new Command('ls'); $command->ignoreValidationErrors(); $command->setCode(function ($input) { $p = new Process(['ls', ...$input->getRawTokens(true)]); $p->setTty(true); $p->mustRun(); }); $app = new Application(); $app->add($command); $app->run(); ``` ![image](https://github.com/symfony/symfony/assets/408368/39b47b14-c2bb-4df6-ad79-26a2fe888523) Commits ------- cef1979 [Console] Allow to returns all tokens after the command name
2 parents 759b6e1 + cef1979 commit a51e675
Copy full SHA for a51e675

File tree

2 files changed

+48
-2
lines changed
Filter options

2 files changed

+48
-2
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Console/Input/ArgvInput.php
+21-2Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Console/Tests/Input/ArgvInputTest.php
+27Lines 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)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.