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] Add ArgvInput::getRawTokens() #54238

Merged
merged 1 commit into from Mar 14, 2024

Conversation

lyrixx
Copy link
Member

@lyrixx lyrixx commented Mar 11, 2024

Q A
Branch? 7.1
Bug fix? no
New feature? yes
Deprecations? no
Issues -
License MIT

Many times, I had to access raw tokens, and each time I used reflection or other hacks
to get theses values. So I think it's time to expose this property properly.

For example, if you want to create a command that wrap a proces, with "pass thru" arguments,
you need that.


Examples:

#!/usr/bin/env php
<?php

use Symfony\Component\Console\SingleCommandApplication;
use Symfony\Component\Process\Process;

require __DIR__ . '/vendor/autoload.php';

$command = new SingleCommandApplication('ls');
$command->ignoreValidationErrors();
$command->setCode(function ($input) {
    $p = new Process(['ls', ...$input->getRawTokens()]);
    $p->setTty(true);
    $p->mustRun();
});
$command->run();

image


Note

In castor, we also strip all options until the command name

$parameters = [];
$keep = false;
foreach ($input->getRawTokens() as $value) {
    if ($value === $input->getFirstArgument()) {
        $keep = true;

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

It could be nice to add support for this too?

@stof
Copy link
Member

stof commented Mar 14, 2024

Thank you @lyrixx.

@stof stof merged commit 965283a into symfony:7.1 Mar 14, 2024
7 of 10 checks passed
@lyrixx lyrixx deleted the console-export-row-tokens branch March 14, 2024 18:14
fabpot added a commit that referenced this pull request Mar 21, 2024
… 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants