@@ -8,31 +8,31 @@ When building a command line tool, you may not need to provide several commands.
8
8
In such case, having to pass the command name each time is tedious. Fortunately,
9
9
it is possible to remove this need by declaring a single command application::
10
10
11
+ .. versionadded :: 5.0
12
+
13
+ The class :class: `Symfony\\ Component\\ Console\\ SingleCommandApplication ` was
14
+ introduced in Symfony 5.1.
15
+
11
16
#!/usr/bin/env php
12
17
<?php
13
18
require __DIR__.'/vendor/autoload.php';
14
19
15
- use Symfony\Component\Console\Application;
16
20
use Symfony\C omponent\C onsole\I nput\I nputArgument;
17
21
use Symfony\C omponent\C onsole\I nput\I nputInterface;
18
22
use Symfony\C omponent\C onsole\I nput\I nputOption;
19
23
use Symfony\C omponent\C onsole\O utput\O utputInterface;
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
24
+ use Symfony \C omponent \C onsole \S ingleCommandApplication;
25
+
26
+ (new SingleCommandApplication() )
27
+ ->setName('My Super Command') // Optional
28
+ ->setVersion('1.0.0') // Optional
29
+ ->addArgument('foo', InputArgument::OPTIONAL, 'The directory')
30
+ ->addOption('bar', null, InputOption::VALUE_REQUIRED)
31
+ ->setCode(function(InputInterface $input, OutputInterface $output) {
32
+ // output arguments and options
33
+ })
30
34
->run();
31
35
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
-
36
36
You can still register a command as usual::
37
37
38
38
#!/usr/bin/env php
@@ -49,3 +49,7 @@ You can still register a command as usual::
49
49
50
50
$application->setDefaultCommand($command->getName(), true);
51
51
$application->run();
52
+
53
+ The method :method: `Symfony\\ Component\\ Console\\ Application::setDefaultCommand `
54
+ accepts a boolean as second parameter. If true, the command ``echo `` will then
55
+ always be used, without having to pass its name.
0 commit comments