Skip to content

Commit

Permalink
feature #54238 [Console] Add ArgvInput::getRawTokens() (lyrixx)
Browse files Browse the repository at this point in the history
This PR was merged into the 7.1 branch.

Discussion
----------

[Console] Add `ArgvInput::getRawTokens()`

| 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:

```php
#!/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](https://github.com/symfony/symfony/assets/408368/d16d28ae-5aff-4df8-9978-216448bf1491)

---

> [!NOTE]
> In castor, we also strip all options until the command name
> ```php
> $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?

Commits
-------

b5ab0e3 [Console] Add `ArgvInput::getRawTokens()`
  • Loading branch information
stof committed Mar 14, 2024
2 parents 7e4f02e + b5ab0e3 commit 965283a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/Symfony/Component/Console/CHANGELOG.md
@@ -1,6 +1,11 @@
CHANGELOG
=========

7.1
---

* Add `ArgvInput::getRawTokens()`

7.0
---

Expand Down
10 changes: 10 additions & 0 deletions src/Symfony/Component/Console/Input/ArgvInput.php
Expand Up @@ -345,6 +345,16 @@ public function getParameterOption(string|array $values, string|bool|int|float|a
return $default;
}

/**
* Returns un-parsed and not validated tokens.
*
* @return list<string>
*/
public function getRawTokens(): array
{
return $this->tokens;
}

/**
* Returns a stringified representation of the args passed to the command.
*/
Expand Down

0 comments on commit 965283a

Please sign in to comment.