@@ -6,68 +6,51 @@ Building a single Command Application
66
77When building a command line tool, you may not need to provide several commands.
88In 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
62- be used, without having to pass its name.
63-
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();
9+ it is possible to remove this need by 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')
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
30+ ->run();
31+
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+ Of course, you can still register a command as usual::
37+
38+ #!/usr/bin/env php
39+ <?php
40+ require __DIR__.'/vendor/autoload.php';
41+
42+ use Symfony\Component\Console\Application;
43+ use Symfony\Component\Console\Input\InputArgument;
44+ use Symfony\Component\Console\Input\InputInterface;
45+ use Symfony\Component\Console\Input\InputOption;
46+ use Symfony\Component\Console\Output\OutputInterface;
47+
48+ use Acme\Command\DefaultCommand;
49+
50+ $application = new Application('echo', '1.0.0');
51+ $command = new DefaultCommand();
52+
53+ $application->add($command);
54+
55+ $application->setDefaultCommand($command->getName(), true);
56+ $application->run();