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] Mention ArgvInput::getRawTokens #19713

Merged
merged 1 commit into from Apr 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 31 additions & 0 deletions console/input.rst
Expand Up @@ -311,6 +311,37 @@ The above code can be simplified as follows because ``false !== null``::
$yell = ($optionValue !== false);
$yellLouder = ($optionValue === 'louder');

Fetching The Raw Command Input
------------------------------

Sometimes, you may need to fetch the raw input that was passed to the command.
This is useful when you need to parse the input yourself or when you need to
pass the input to another command without having to worry about the number
of arguments or options defined in your own command. This can be achieved
thanks to the
:method:`Symfony\\Component\\Console\\Input\\ArgvInput::getRawTokens` method::

// ...
use Symfony\Component\Process\Process;

protected function execute(InputInterface $input, OutputInterface $output): int
{
// pass the raw input of your command to the "ls" command
$process = new Process(['ls', ...$input->getRawTokens(true)]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still have problems understanding what "a token" is in this context.

Imagine that I run a command like this:

$ php bin/console app:my-command -c -a --foo --bar=zzz arg1 -- arg2 -b

Is this the content of getRawTokens()?

['-c', '-a', '--foo', '--bar=zzz', 'arg1', '--', 'arg2', '-b']

Also, what's the purpose of the boolean argument of getRawTokens()?

Thanks!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess it is called a token because it can be anything: letters, pipes, dashes, etc. Not sure but that what makes the more sense to me 🙂

About the boolean argument, I wrote a sentence about it just under the code snippet. Should this also be present in the code snippet?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will be back in 2 days, and I have an idea in mind. Will add a proposal then.

I am currently on a phone 📱

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At the end I merged this after rewording it a bit and expanding the code examples. If we don't like anything about this, let's create a new issue or PR. Thanks!

$process->setTty(true);
$process->mustRun();

// ...
}

You can include the current command name in the raw tokens by passing ``true``
to the ``getRawTokens`` method only parameter.

.. versionadded:: 7.1

The :method:`Symfony\\Component\\Console\\Input\\ArgvInput::getRawTokens`
method was introduced in Symfony 7.1.

Adding Argument/Option Value Completion
---------------------------------------

Expand Down