Skip to content

Commit

Permalink
feature #54347 [Console] Allow to return all tokens after the command…
Browse files Browse the repository at this point in the history
… 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
  • Loading branch information
fabpot committed Mar 21, 2024
2 parents 759b6e1 + cef1979 commit a51e675
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
23 changes: 21 additions & 2 deletions src/Symfony/Component/Console/Input/ArgvInput.php
Expand Up @@ -348,11 +348,30 @@ public function getParameterOption(string|array $values, string|bool|int|float|a
/**
* 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']];
}
}

0 comments on commit a51e675

Please sign in to comment.