Skip to content

Commit 2bdc09c

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

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

console/input.rst

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -825,6 +825,58 @@ 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+
use Symfony\Component\Console\Input\RawInputInterface;
837+
use Symfony\Component\Console\Output\OutputInterface;
838+
839+
public function __invoke(RawInputInterface $input, OutputInterface $output): int
840+
{
841+
// returns only arguments explicitly passed by the user (no defaults)
842+
$rawArguments = $input->getRawArguments();
843+
844+
// returns only options explicitly passed by the user (no defaults)
845+
$rawOptions = $input->getRawOptions();
846+
847+
// ...
848+
}
849+
850+
The ``unparse()`` method converts parsed options back to their CLI string
851+
representation (e.g. ``['--format=json', '--verbose']``), which is useful when
852+
forwarding options to a child process::
853+
854+
use Symfony\Component\Console\Input\RawInputInterface;
855+
use Symfony\Component\Console\Output\OutputInterface;
856+
use Symfony\Component\Process\Process;
857+
858+
public function __invoke(RawInputInterface $input, OutputInterface $output): int
859+
{
860+
$process = new Process([
861+
PHP_BINARY, 'bin/console', 'app:child-command',
862+
...$input->getRawArguments(),
863+
...$input->unparse(),
864+
]);
865+
$process->mustRun();
866+
867+
// ...
868+
}
869+
870+
You can also pass an array of option names to ``unparse()`` to only include
871+
specific options::
872+
873+
// only include --format and --verbose in the unparsed output
874+
$unparsed = $input->unparse(['format', 'verbose']);
875+
876+
.. versionadded:: 8.1
877+
878+
The ``RawInputInterface`` was introduced in Symfony 8.1.
879+
828880
.. _console-input-completion:
829881

830882
Adding Argument/Option Value Completion

0 commit comments

Comments
 (0)