Skip to content

Commit b1199c8

Browse files
committed
Document RawInputInterface for accessing raw arguments/options and unparsing
1 parent 4f507c0 commit b1199c8

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

console/input.rst

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -825,6 +825,56 @@ command without having to worry about the number of arguments or options::
825825
// ...
826826
}
827827

828+
Accessing Raw Arguments and Options
829+
-----------------------------------
830+
831+
While ``getRawTokens()`` returns the unparsed CLI tokens as strings, sometimes
832+
you need access to the *parsed* arguments and options without default values
833+
merged in. The :class:`Symfony\\Component\\Console\\Input\\RawInputInterface`
834+
provides methods for this:
835+
836+
* ``getRawArguments()``: returns only arguments explicitly passed by the user
837+
(without default values merged in);
838+
* ``getRawOptions()``: returns only options explicitly passed by the user
839+
(without default values merged in);
840+
* ``unparse()``: converts parsed options back to their CLI string representation
841+
(e.g. ``['--format=json', '--verbose']``).
842+
843+
You can type-hint ``RawInputInterface`` in invokable commands to access these
844+
methods::
845+
846+
// ...
847+
use Symfony\Component\Console\Input\InputInterface;
848+
use Symfony\Component\Console\Output\OutputInterface;
849+
use Symfony\Component\Process\Process;
850+
851+
protected function execute(InputInterface $input, OutputInterface $output): int
852+
{
853+
// returns only arguments explicitly passed (no defaults)
854+
$rawArguments = $input->getRawArguments();
855+
856+
// returns only options explicitly passed (no defaults)
857+
$rawOptions = $input->getRawOptions();
858+
859+
// converts parsed options back to CLI tokens, useful for forwarding
860+
// to a child process
861+
$process = new Process([
862+
'app:child-command',
863+
...$input->getRawArguments(),
864+
...$input->unparse(),
865+
]);
866+
$process->mustRun();
867+
868+
// you can also filter specific options to unparse
869+
$unparsed = $input->unparse(['format', 'verbose']);
870+
871+
// ...
872+
}
873+
874+
.. versionadded:: 8.1
875+
876+
The ``RawInputInterface`` was introduced in Symfony 8.1.
877+
828878
.. _console-input-completion:
829879

830880
Adding Argument/Option Value Completion

0 commit comments

Comments
 (0)