@@ -314,29 +314,34 @@ The above code can be simplified as follows because ``false !== null``::
314
314
Fetching The Raw Command Input
315
315
------------------------------
316
316
317
- Sometimes, you may need to fetch the raw input that was passed to the command.
318
- This is useful when you need to parse the input yourself or when you need to
319
- pass the input to another command without having to worry about the number
320
- of arguments or options defined in your own command. This can be achieved
321
- thanks to the
322
- :method: `Symfony\\ Component\\ Console\\ Input\\ ArgvInput::getRawTokens ` method::
317
+ Symfony provides a :method: `Symfony\\ Component\\ Console\\ Input\\ ArgvInput::getRawTokens `
318
+ method to fetch the raw input that was passed to the command. This is useful if
319
+ you want to parse the input yourself or when you need to pass the input to another
320
+ command without having to worry about the number of arguments or options::
323
321
324
322
// ...
325
323
use Symfony\Component\Process\Process;
326
324
327
325
protected function execute(InputInterface $input, OutputInterface $output): int
328
326
{
329
- // pass the raw input of your command to the "ls" command
330
- $process = new Process(['ls', ...$input->getRawTokens(true)]);
327
+ // if this command was run as:
328
+ // php bin/console app:my-command foo --bar --baz=3 --qux=value1 --qux=value2
329
+
330
+ $tokens = $input->getRawTokens();
331
+ // $tokens = ['app:my-command', 'foo', '--bar', '--baz=3', '--qux=value1', '--qux=value2'];
332
+
333
+ // pass true as argument to not include the original command name
334
+ $tokens = $input->getRawTokens(true);
335
+ // $tokens = ['foo', '--bar', '--baz=3', '--qux=value1', '--qux=value2'];
336
+
337
+ // pass the raw input to any other command (from Symfony or the operating system)
338
+ $process = new Process(['app:other-command', ...$input->getRawTokens(true)]);
331
339
$process->setTty(true);
332
340
$process->mustRun();
333
341
334
342
// ...
335
343
}
336
344
337
- You can include the current command name in the raw tokens by passing ``true ``
338
- to the ``getRawTokens `` method only parameter.
339
-
340
345
.. versionadded :: 7.1
341
346
342
347
The :method: `Symfony\\ Component\\ Console\\ Input\\ ArgvInput::getRawTokens `
0 commit comments