diff --git a/components/console/single_command_tool.rst b/components/console/single_command_tool.rst index 74c554c46ef..91b17b47c61 100644 --- a/components/console/single_command_tool.rst +++ b/components/console/single_command_tool.rst @@ -5,34 +5,36 @@ Building a single Command Application ===================================== When building a command line tool, you may not need to provide several commands. -In such case, having to pass the command name each time is tedious. Fortunately, -it is possible to remove this need by declaring a single command application:: +In such case, having to pass the command name each time is tedious. + +.. versionadded:: 5.1 + + The class :class:`Symfony\\Component\\Console\\SingleCommandApplication` was + introduced in Symfony 5.1. + +Fortunately, it is possible to remove this need by declaring a single command +application:: #!/usr/bin/env php register('echo') - ->addArgument('foo', InputArgument::OPTIONAL, 'The directory') - ->addOption('bar', null, InputOption::VALUE_REQUIRED) - ->setCode(function(InputInterface $input, OutputInterface $output) { - // output arguments and options - }) - ->getApplication() - ->setDefaultCommand('echo', true) // Single command application + use Symfony\Component\Console\SingleCommandApplication; + + (new SingleCommandApplication()) + ->setName('My Super Command') // Optional + ->setVersion('1.0.0') // Optional + ->addArgument('foo', InputArgument::OPTIONAL, 'The directory') + ->addOption('bar', null, InputOption::VALUE_REQUIRED) + ->setCode(function (InputInterface $input, OutputInterface $output) { + // output arguments and options + }) ->run(); -The method :method:`Symfony\\Component\\Console\\Application::setDefaultCommand` -accepts a boolean as second parameter. If true, the command ``echo`` will then -always be used, without having to pass its name. - You can still register a command as usual:: #!/usr/bin/env php @@ -49,3 +51,7 @@ You can still register a command as usual:: $application->setDefaultCommand($command->getName(), true); $application->run(); + +The method :method:`Symfony\\Component\\Console\\Application::setDefaultCommand` +accepts a boolean as second parameter. If true, the command ``echo`` will then +always be used, without having to pass its name.