@@ -6,68 +6,56 @@ Building a single Command Application
6
6
7
7
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
- it is possible to remove this need by extending the application::
10
-
11
- namespace Acme\Tool;
12
-
13
- use Symfony\Component\Console\Application;
14
- use Symfony\Component\Console\Input\InputInterface;
15
-
16
- class MyApplication extends Application
17
- {
18
- /**
19
- * Gets the name of the command based on input.
20
- *
21
- * @param InputInterface $input The input interface
22
- *
23
- * @return string The command name
24
- */
25
- protected function getCommandName(InputInterface $input)
26
- {
27
- // This should return the name of your command.
28
- return 'my_command';
29
- }
30
-
31
- /**
32
- * Gets the default commands that should always be available.
33
- *
34
- * @return array An array of default Command instances
35
- */
36
- protected function getDefaultCommands()
37
- {
38
- // Keep the core default commands to have the HelpCommand
39
- // which is used when using the --help option
40
- $defaultCommands = parent::getDefaultCommands();
41
-
42
- $defaultCommands[] = new MyCommand();
43
-
44
- return $defaultCommands;
45
- }
46
-
47
- /**
48
- * Overridden so that the application doesn't expect the command
49
- * name to be the first argument.
50
- */
51
- public function getDefinition()
52
- {
53
- $inputDefinition = parent::getDefinition();
54
- // clear out the normal first argument, which is the command name
55
- $inputDefinition->setArguments();
56
-
57
- return $inputDefinition;
58
- }
59
- }
60
-
61
- When calling your console script, the command ``MyCommand `` will then always
9
+ it is possible to remove this need declaring a single command application::
10
+
11
+ #!/usr/bin/env php
12
+ <?php
13
+ require __DIR__.'/vendor/autoload.php';
14
+
15
+ use Symfony\Component\Console\Application;
16
+ use Symfony\Component\Console\Input\InputArgument;
17
+ use Symfony\Component\Console\Input\InputInterface;
18
+ use Symfony\Component\Console\Input\InputOption;
19
+ use Symfony\Component\Console\Output\OutputInterface;
20
+
21
+ (new Application('echo', '1.0.0'))
22
+ ->register('echo')
23
+ ->addArgument('foo', InputArgument::OPTIONAL, 'The directory', 'foo')
24
+ ->addOption('bar', null, InputOption::VALUE_REQUIRED, 'Foobar', 'bar')
25
+ ->setCode(function(InputInterface $input, OutputInterface $output) {
26
+ $output->writeln('start');
27
+ $output->writeln($input->getArgument('foo'));
28
+ $output->writeln($input->getOption('bar'));
29
+ })
30
+ ->getApplication()
31
+ ->setDefaultCommand('echo', true)
32
+ ->run();
33
+
34
+ When calling your console script, the command ``echo `` will then always
62
35
be used, without having to pass its name.
63
36
64
- You can also simplify how you execute the application::
65
-
66
- #!/usr/bin/env php
67
- <?php
68
- // command.php
69
-
70
- use Acme\Tool\MyApplication;
71
-
72
- $application = new MyApplication();
73
- $application->run();
37
+ Of course, you can still register a command as usual::
38
+
39
+ #!/usr/bin/env php
40
+ <?php
41
+ require __DIR__.'/vendor/autoload.php';
42
+
43
+ use Symfony\Component\Console\Application;
44
+ use Symfony\Component\Console\Input\InputArgument;
45
+ use Symfony\Component\Console\Input\InputInterface;
46
+ use Symfony\Component\Console\Input\InputOption;
47
+ use Symfony\Component\Console\Output\OutputInterface;
48
+
49
+ use Acme\Command\DefaultCommand;
50
+
51
+ $application = new Application('echo', '1.0.0');
52
+ $command = new DefaultCommand();
53
+
54
+ $application->add($command);
55
+
56
+ $application->setDefaultCommand($command, true);
57
+ $application->run();
58
+
59
+ .. tip ::
60
+
61
+ You don't need to pass the command name in order to get the help.
0 commit comments