Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

[Console] Allow to return all tokens after the command name#54347

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
fabpot merged 1 commit intosymfony:7.1fromlyrixx:console-raw-tokens
Mar 21, 2024

Conversation

lyrixx
Copy link
Member

@lyrixxlyrixx commentedMar 20, 2024
edited
Loading

QA
Branch?7.1
Bug fix?no
New feature?yes
Deprecations?no
Issues
LicenseMIT

follows#54238

Now, we can make it works at the command level (previous PR was at the application level)

#!/usr/bin/env php<?phpuseSymfony\Component\Console\Application;useSymfony\Component\Console\Command\Command;useSymfony\Component\Process\Process;require__DIR__ .'/vendor/autoload.php';$command =newCommand('ls');$command->ignoreValidationErrors();$command->setCode(function ($input) {$p =newProcess(['ls', ...$input->getRawTokens(true)]);$p->setTty(true);$p->mustRun();});$app =newApplication();$app->add($command);$app->run();

image

@derrabusderrabus changed the title[Console] Allow to returns all tokens after the command name[Console] Allow to return all tokens after the command nameMar 20, 2024
@stof
Copy link
Member

should we also implement this in ArrayInput (and deprecate not implementing in child classes of Input to have it in all Input implementations in 8.0) ?

@lyrixx
Copy link
MemberAuthor

lyrixx commentedMar 21, 2024
edited
Loading

should we also implement this in ArrayInput (and deprecate not implementing in child classes of Input to have it in all Input implementations in 8.0) ?

I though about this, and It's not necessary IMHO. since you construct the array yourself, before calling new ArrayInput, you already have the array by definition.

More over, I don't really this any use case for that.

Anyway, I started to implements it:

diff --git a/src/Symfony/Component/Console/Input/ArrayInput.php b/src/Symfony/Component/Console/Input/ArrayInput.phpindex d27ff411ee..5f4439faf1 100644--- a/src/Symfony/Component/Console/Input/ArrayInput.php+++ b/src/Symfony/Component/Console/Input/ArrayInput.php@@ -87,6 +87,35 @@ class ArrayInput extends Input         return $default;     }+    /**+     * 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(bool $strip = false): array+    {+        if (!$strip) {+            return $this->parameters;+        }++        $parameters = [];+        $keep = false;+        foreach ($this->parameters as $name => $value) {+            if (!$keep && $value === $this->getFirstArgument()) {+                $keep = true;++                continue;+            }+            if ($keep) {+                $parameters[$name] = $value;+            }+        }++        return $parameters;+    }+     /**      * Returns a stringified representation of the args passed to the command.      */diff --git a/src/Symfony/Component/Console/Tests/Input/ArrayInputTest.php b/src/Symfony/Component/Console/Tests/Input/ArrayInputTest.phpindex d6fe32bb3a..f965e3c58e 100644--- a/src/Symfony/Component/Console/Tests/Input/ArrayInputTest.php+++ b/src/Symfony/Component/Console/Tests/Input/ArrayInputTest.php@@ -170,4 +170,28 @@ class ArrayInputTest extends TestCase         $input = new ArrayInput(['array_arg' => ['val_1', 'val_2']]);         $this->assertSame('val_1 val_2', (string) $input);     }+++    public function testGetRawTokensFalse()+    {+        $input = new ArrayInput(['cli.php', '--foo', 'bar']);+        $this->assertSame(['cli.php', '--foo', 'bar'], $input->getRawTokens());+    }++    /**+     * @dataProvider provideGetRawTokensTrueTests+     */+    public function testGetRawTokensTrue(array $argv, array $expected)+    {+        $input = new ArrayInput($argv);+        $this->assertSame($expected, $input->getRawTokens(true));+    }++    public static function provideGetRawTokensTrueTests(): iterable+    {+        yield [['command' => 'foo:bar'], []];+        yield [['command' => 'foo:bar', '--no-ansi' => true], ['--no-ansi' => true]];+        yield [['command' => 'foo:bar', 'name' => 'john'], ['name' => 'john']];+        yield [['--no-ansi' => true, 'command' => 'foo:bar', 'name' => 'john'], ['name' => 'john']];+    } }

But when I see the tests, especially the very last one, I really think it does not make sens to add it to the array input, and by extension to input

@fabpot
Copy link
Member

Thank you@lyrixx.

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@stofstofstof left review comments

@fabpotfabpotfabpot approved these changes

@nicolas-grekasnicolas-grekasnicolas-grekas approved these changes

@chalasrchalasrAwaiting requested review from chalasrchalasr is a code owner

Assignees
No one assigned
Projects
None yet
Milestone
7.1
Development

Successfully merging this pull request may close these issues.

5 participants
@lyrixx@stof@fabpot@nicolas-grekas@carsonbot

[8]ページ先頭

©2009-2025 Movatter.jp