@@ -5,34 +5,36 @@ Building a single Command Application
55=====================================
66
77When building a command line tool, you may not need to provide several commands.
8- In such case, having to pass the command name each time is tedious. Fortunately,
9- it is possible to remove this need by declaring a single command application::
8+ In such case, having to pass the command name each time is tedious.
9+
10+ .. versionadded :: 5.1
11+
12+ The class :class: `Symfony\\ Component\\ Console\\ SingleCommandApplication ` was
13+ introduced in Symfony 5.1.
14+
15+ Fortunately, it is possible to remove this need by declaring a single command
16+ application::
1017
1118 #!/usr/bin/env php
1219 <?php
1320 require __DIR__.'/vendor/autoload.php';
1421
15- use Symfony\Component\Console\Application;
1622 use Symfony\Component\Console\Input\InputArgument;
1723 use Symfony\Component\Console\Input\InputInterface;
1824 use Symfony\Component\Console\Input\InputOption;
1925 use Symfony\Component\Console\Output\OutputInterface;
20-
21- (new Application('echo', '1.0.0'))
22- ->register('echo' )
23- ->addArgument('foo', InputArgument::OPTIONAL, 'The directory')
24- ->addOption('bar', null, InputOption::VALUE_REQUIRED)
25- ->setCode(function(InputInterface $input, OutputInterface $output) {
26- // output arguments and options
27- })
28- ->getApplication()
29- ->setDefaultCommand('echo', true) // Single command application
26+ use Symfony\Component\Console\SingleCommandApplication;
27+
28+ (new SingleCommandApplication() )
29+ ->setName('My Super Command') // Optional
30+ ->setVersion('1.0.0') // Optional
31+ ->addArgument('foo', InputArgument::OPTIONAL, 'The directory')
32+ ->addOption('bar', null, InputOption::VALUE_REQUIRED)
33+ ->setCode(function (InputInterface $input, OutputInterface $output) {
34+ // output arguments and options
35+ })
3036 ->run();
3137
32- The method :method: `Symfony\\ Component\\ Console\\ Application::setDefaultCommand `
33- accepts a boolean as second parameter. If true, the command ``echo `` will then
34- always be used, without having to pass its name.
35-
3638You can still register a command as usual::
3739
3840 #!/usr/bin/env php
@@ -49,3 +51,7 @@ You can still register a command as usual::
4951
5052 $application->setDefaultCommand($command->getName(), true);
5153 $application->run();
54+
55+ The method :method: `Symfony\\ Component\\ Console\\ Application::setDefaultCommand `
56+ accepts a boolean as second parameter. If true, the command ``echo `` will then
57+ always be used, without having to pass its name.
0 commit comments