From b29ab899a445a2686189a9266e6ec8fea4b3cae4 Mon Sep 17 00:00:00 2001 From: Daniel Gomes Date: Sat, 4 Jan 2014 23:32:19 +0000 Subject: [PATCH 1/9] Documented the Change the Default Command in the Console component --- ...tool.rst => changing_default_behavior.rst} | 45 ++++++++++++++++++- components/console/index.rst | 2 +- 2 files changed, 44 insertions(+), 3 deletions(-) rename components/console/{single_command_tool.rst => changing_default_behavior.rst} (62%) diff --git a/components/console/single_command_tool.rst b/components/console/changing_default_behavior.rst similarity index 62% rename from components/console/single_command_tool.rst rename to components/console/changing_default_behavior.rst index 27942df40f2..33ffc6ee1e6 100644 --- a/components/console/single_command_tool.rst +++ b/components/console/changing_default_behavior.rst @@ -1,8 +1,49 @@ .. index:: - single: Console; Single command application + single: Console; Changing the Default Behavior + +Changing the Default Behavior +============================= + +When building a command line tool, you may need to customize it to fit your needs. +Probably you want to change the Default Command that the Application runs or +maybe you just want to run a Single Command instead of have to pass the command +name each time. Fortunately it is possible to do both. + +Changing the Default Command +---------------------------- + +By default the Application will always run the ListCommand. In order to change +the default command you just need to pass the command name you want to run by +default to the :method:`Symfony\\Component\\Console\\Application::setDefaultCommand` +method:: + + #!/usr/bin/env php + add($command); + $application->setDefaultCommand($command->getName()); + $application->run() + +Test the new console command by running the following + +.. code-block:: bash + + $ app/console Fabien + +This will print the following to the command line: + +.. code-block:: text + + Hello Fabien 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, diff --git a/components/console/index.rst b/components/console/index.rst index c814942d018..ba15bd2c176 100644 --- a/components/console/index.rst +++ b/components/console/index.rst @@ -6,6 +6,6 @@ Console introduction usage - single_command_tool + changing_default_behavior events helpers/index From 11c7174059df940a1a5e75584727c452304355f8 Mon Sep 17 00:00:00 2001 From: Daniel Gomes Date: Sun, 5 Jan 2014 14:42:59 +0000 Subject: [PATCH 2/9] Added the version number where the setDefaultCommand was introduced --- components/console/changing_default_behavior.rst | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/components/console/changing_default_behavior.rst b/components/console/changing_default_behavior.rst index 33ffc6ee1e6..eb44d128a12 100644 --- a/components/console/changing_default_behavior.rst +++ b/components/console/changing_default_behavior.rst @@ -4,6 +4,10 @@ Changing the Default Behavior ============================= +.. versionadded:: 2.5, + The :method:`Symfony\\Component\\Console\\Application::setDefaultCommand` + method was introduced in version 2.5 + When building a command line tool, you may need to customize it to fit your needs. Probably you want to change the Default Command that the Application runs or maybe you just want to run a Single Command instead of have to pass the command From 60e2b3ec4466b3db6450bb76da1d4f6cb0f8c368 Mon Sep 17 00:00:00 2001 From: Daniel Gomes Date: Sun, 5 Jan 2014 14:47:30 +0000 Subject: [PATCH 3/9] Added the delete document to avoid broken urls and added a notice that the document was moved to another location --- components/console/single_command_tool.rst | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 components/console/single_command_tool.rst diff --git a/components/console/single_command_tool.rst b/components/console/single_command_tool.rst new file mode 100644 index 00000000000..c4a477ded13 --- /dev/null +++ b/components/console/single_command_tool.rst @@ -0,0 +1,4 @@ +Building a Single Command Application +===================================== + +This Document was moved to :doc:`/components/console/changing_default_behaviour` From af9eac4f97b1918031c02d3dc395997012682e7a Mon Sep 17 00:00:00 2001 From: Daniel Gomes Date: Sun, 5 Jan 2014 14:55:02 +0000 Subject: [PATCH 4/9] Changed the code to remove references to Symfony Framework since it's the standalone component --- components/console/changing_default_behavior.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/components/console/changing_default_behavior.rst b/components/console/changing_default_behavior.rst index eb44d128a12..0dbbb64ed60 100644 --- a/components/console/changing_default_behavior.rst +++ b/components/console/changing_default_behavior.rst @@ -23,9 +23,9 @@ method:: #!/usr/bin/env php Date: Sun, 5 Jan 2014 15:12:09 +0000 Subject: [PATCH 5/9] Updated references to the new document --- components/console/introduction.rst | 2 +- components/map.rst.inc | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/components/console/introduction.rst b/components/console/introduction.rst index 4ca2bcfe8c1..d7217176400 100644 --- a/components/console/introduction.rst +++ b/components/console/introduction.rst @@ -525,7 +525,7 @@ Learn More! ----------- * :doc:`/components/console/usage` -* :doc:`/components/console/single_command_tool` +* :doc:`/components/console/changing_default_behavior` .. _Packagist: https://packagist.org/packages/symfony/console .. _ANSICON: https://github.com/adoxa/ansicon/downloads diff --git a/components/map.rst.inc b/components/map.rst.inc index a5f2d0f8db7..842097c5ef2 100644 --- a/components/map.rst.inc +++ b/components/map.rst.inc @@ -19,7 +19,7 @@ * :doc:`/components/console/introduction` * :doc:`/components/console/usage` - * :doc:`/components/console/single_command_tool` + * :doc:`/components/console/changing_default_behavior` * :doc:`/components/console/events` * :doc:`/components/console/helpers/index` From 012456d9f5a13455c4271d1fdc888600c415ebef Mon Sep 17 00:00:00 2001 From: Daniel Gomes Date: Sun, 5 Jan 2014 15:20:41 +0000 Subject: [PATCH 6/9] Moved `versionadded` to the right section --- components/console/changing_default_behavior.rst | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/components/console/changing_default_behavior.rst b/components/console/changing_default_behavior.rst index 0dbbb64ed60..e89332b4c9d 100644 --- a/components/console/changing_default_behavior.rst +++ b/components/console/changing_default_behavior.rst @@ -4,10 +4,6 @@ Changing the Default Behavior ============================= -.. versionadded:: 2.5, - The :method:`Symfony\\Component\\Console\\Application::setDefaultCommand` - method was introduced in version 2.5 - When building a command line tool, you may need to customize it to fit your needs. Probably you want to change the Default Command that the Application runs or maybe you just want to run a Single Command instead of have to pass the command @@ -16,6 +12,10 @@ name each time. Fortunately it is possible to do both. Changing the Default Command ---------------------------- +.. versionadded:: 2.5, +The :method:`Symfony\\Component\\Console\\Application::setDefaultCommand` + method was introduced in version 2.5 + By default the Application will always run the ListCommand. In order to change the default command you just need to pass the command name you want to run by default to the :method:`Symfony\\Component\\Console\\Application::setDefaultCommand` From c23f34e0f5f9bd6984a2e01aed5d768e1ed82753 Mon Sep 17 00:00:00 2001 From: Daniel Gomes Date: Sun, 5 Jan 2014 22:39:12 +0000 Subject: [PATCH 7/9] Applied some suggestions --- .../console/changing_default_behavior.rst | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/components/console/changing_default_behavior.rst b/components/console/changing_default_behavior.rst index e89332b4c9d..244d9ed6af3 100644 --- a/components/console/changing_default_behavior.rst +++ b/components/console/changing_default_behavior.rst @@ -5,21 +5,20 @@ Changing the Default Behavior ============================= When building a command line tool, you may need to customize it to fit your needs. -Probably you want to change the Default Command that the Application runs or -maybe you just want to run a Single Command instead of have to pass the command -name each time. Fortunately it is possible to do both. +Probably you want to change the default command that the Application runs or +maybe you just want to run a single command instead of have to pass the command +name each time. Fortunately, it is possible to do both. Changing the Default Command ---------------------------- .. versionadded:: 2.5, -The :method:`Symfony\\Component\\Console\\Application::setDefaultCommand` - method was introduced in version 2.5 + The :method:`Symfony\\Component\\Console\\Application::setDefaultCommand` + method was introduced in version 2.5. By default the Application will always run the ListCommand. In order to change the default command you just need to pass the command name you want to run by -default to the :method:`Symfony\\Component\\Console\\Application::setDefaultCommand` -method:: +default to the ``setDefaultCommand`` method:: #!/usr/bin/env php Date: Tue, 7 Jan 2014 15:16:45 +0000 Subject: [PATCH 8/9] Applyed suggestions from @fabpot and @stof --- .../console/changing_default_behavior.rst | 116 ------------------ .../console/changing_default_command.rst | 67 ++++++++++ components/console/index.rst | 3 +- components/console/introduction.rst | 3 +- components/console/single_command_tool.rst | 70 ++++++++++- 5 files changed, 140 insertions(+), 119 deletions(-) delete mode 100644 components/console/changing_default_behavior.rst create mode 100644 components/console/changing_default_command.rst diff --git a/components/console/changing_default_behavior.rst b/components/console/changing_default_behavior.rst deleted file mode 100644 index 244d9ed6af3..00000000000 --- a/components/console/changing_default_behavior.rst +++ /dev/null @@ -1,116 +0,0 @@ -.. index:: - single: Console; Changing the Default Behavior - -Changing the Default Behavior -============================= - -When building a command line tool, you may need to customize it to fit your needs. -Probably you want to change the default command that the Application runs or -maybe you just want to run a single command instead of have to pass the command -name each time. Fortunately, it is possible to do both. - -Changing the Default Command ----------------------------- - -.. versionadded:: 2.5, - The :method:`Symfony\\Component\\Console\\Application::setDefaultCommand` - method was introduced in version 2.5. - -By default the Application will always run the ListCommand. In order to change -the default command you just need to pass the command name you want to run by -default to the ``setDefaultCommand`` method:: - - #!/usr/bin/env php - add($command); - $application->setDefaultCommand($command->getName()); - $application->run() - -Test the new console command by running the following - -.. code-block:: bash - - $ app/console Fabien - -This will print the following to the command line: - -.. code-block:: text - - Hello Fabien - -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 extending the application:: - - namespace Acme\Tool; - - use Symfony\Component\Console\Application; - use Symfony\Component\Console\Input\InputInterface; - - class MyApplication extends Application - { - /** - * Gets the name of the command based on input. - * - * @param InputInterface $input The input interface - * - * @return string The command name - */ - protected function getCommandName(InputInterface $input) - { - // This should return the name of your command. - return 'my_command'; - } - - /** - * Gets the default commands that should always be available. - * - * @return array An array of default Command instances - */ - protected function getDefaultCommands() - { - // Keep the core default commands to have the HelpCommand - // which is used when using the --help option - $defaultCommands = parent::getDefaultCommands(); - - $defaultCommands[] = new MyCommand(); - - return $defaultCommands; - } - - /** - * Overridden so that the application doesn't expect the command - * name to be the first argument. - */ - public function getDefinition() - { - $inputDefinition = parent::getDefinition(); - // clear out the normal first argument, which is the command name - $inputDefinition->setArguments(); - - return $inputDefinition; - } - } - -When calling your console script, the ``MyCommand`` command will then always -be used, without having to pass its name. - -Executing the application can also be simplified:: - - #!/usr/bin/env php - run(); diff --git a/components/console/changing_default_command.rst b/components/console/changing_default_command.rst new file mode 100644 index 00000000000..a90e298223a --- /dev/null +++ b/components/console/changing_default_command.rst @@ -0,0 +1,67 @@ +.. index:: + single: Console; Changing the Default Command + +Changing the Default Command +============================ + +.. versionadded:: 2.5, + The :method:`Symfony\\Component\\Console\\Application::setDefaultCommand` + method was introduced in version 2.5. + +By default the Application will always run the ``ListCommand``. In order to change +the default command you just need to pass the command name you want to run by +default to the ``setDefaultCommand`` method:: + + namespace Acme\Command; + + use Symfony\Component\Console\Command\Command; + use Symfony\Component\Console\Input\InputInterface; + use Symfony\Component\Console\Output\OutputInterface; + + class HelloWorldCommand extends Command + { + protected function configure() + { + $this->setName('hello:world') + ->setDescription('Outputs \'Hello World\''); + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + $output->writeln('Hello World'); + } + } + +Executing the application and changing the default Command:: + + // application.php + + use Acme\Command\HelloWorldCommand; + use Symfony\Component\Console\Application; + + $command = new HelloWorldCommand(); + $application = new Application(); + $application->add($command); + $application->setDefaultCommand($command->getName()); + $application->run(); + +Test the new default console command by running the following + +.. code-block:: bash + + $ php application.php + +This will print the following to the command line: + +.. code-block:: text + + Hello Fabien + +.. tip:: + + The feature was a limitation since you cannot use the Command ``arguments``. + +Learn More! +----------- + +* :doc:`/components/console/single_command_tool` diff --git a/components/console/index.rst b/components/console/index.rst index ba15bd2c176..f974291a8e0 100644 --- a/components/console/index.rst +++ b/components/console/index.rst @@ -6,6 +6,7 @@ Console introduction usage - changing_default_behavior + changing_default_command + single_command_tool events helpers/index diff --git a/components/console/introduction.rst b/components/console/introduction.rst index d7217176400..c742c333f89 100644 --- a/components/console/introduction.rst +++ b/components/console/introduction.rst @@ -525,7 +525,8 @@ Learn More! ----------- * :doc:`/components/console/usage` -* :doc:`/components/console/changing_default_behavior` +* :doc:`/components/console/single_command_tool` +* :doc:`/components/console/changing_default_command` .. _Packagist: https://packagist.org/packages/symfony/console .. _ANSICON: https://github.com/adoxa/ansicon/downloads diff --git a/components/console/single_command_tool.rst b/components/console/single_command_tool.rst index c4a477ded13..a5ba82ceca5 100644 --- a/components/console/single_command_tool.rst +++ b/components/console/single_command_tool.rst @@ -1,4 +1,72 @@ +.. index:: +single: Console; Single command application + Building a Single Command Application ===================================== -This Document was moved to :doc:`/components/console/changing_default_behaviour` +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 extending the application:: + + namespace Acme\Tool; + + use Symfony\Component\Console\Application; + use Symfony\Component\Console\Input\InputInterface; + + class MyApplication extends Application + { + /** + * Gets the name of the command based on input. + * + * @param InputInterface $input The input interface + * + * @return string The command name + */ + protected function getCommandName(InputInterface $input) + { + // This should return the name of your command. + return 'my_command'; + } + + /** + * Gets the default commands that should always be available. + * + * @return array An array of default Command instances + */ + protected function getDefaultCommands() + { + // Keep the core default commands to have the HelpCommand + // which is used when using the --help option + $defaultCommands = parent::getDefaultCommands(); + + $defaultCommands[] = new MyCommand(); + + return $defaultCommands; + } + + /** + * Overridden so that the application doesn't expect the command + * name to be the first argument. + */ + public function getDefinition() + { + $inputDefinition = parent::getDefinition(); + // clear out the normal first argument, which is the command name + $inputDefinition->setArguments(); + + return $inputDefinition; + } + } + +When calling your console script, the command ``MyCommand`` will then always +be used, without having to pass its name. + +You can also simplify how you execute the application:: + + #!/usr/bin/env php + run(); From c1b2aad00e47ddd9c02732f24ed2ce3960210e03 Mon Sep 17 00:00:00 2001 From: Daniel Gomes Date: Thu, 9 Jan 2014 09:17:19 +0000 Subject: [PATCH 9/9] Applied suggestions from Ryan --- components/console/changing_default_command.rst | 8 ++++---- components/console/single_command_tool.rst | 2 +- components/map.rst.inc | 3 ++- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/components/console/changing_default_command.rst b/components/console/changing_default_command.rst index a90e298223a..a2072c795ba 100644 --- a/components/console/changing_default_command.rst +++ b/components/console/changing_default_command.rst @@ -1,5 +1,5 @@ .. index:: - single: Console; Changing the Default Command + single: Console; Changing the Default Command Changing the Default Command ============================ @@ -8,7 +8,7 @@ Changing the Default Command The :method:`Symfony\\Component\\Console\\Application::setDefaultCommand` method was introduced in version 2.5. -By default the Application will always run the ``ListCommand``. In order to change +will always run the ``ListCommand`` when no command name is passed. In order to change the default command you just need to pass the command name you want to run by default to the ``setDefaultCommand`` method:: @@ -45,7 +45,7 @@ Executing the application and changing the default Command:: $application->setDefaultCommand($command->getName()); $application->run(); -Test the new default console command by running the following +Test the new default console command by running the following: .. code-block:: bash @@ -59,7 +59,7 @@ This will print the following to the command line: .. tip:: - The feature was a limitation since you cannot use the Command ``arguments``. + This feature has a limitation: you cannot use it with any Command arguments. Learn More! ----------- diff --git a/components/console/single_command_tool.rst b/components/console/single_command_tool.rst index a5ba82ceca5..02fc24a565f 100644 --- a/components/console/single_command_tool.rst +++ b/components/console/single_command_tool.rst @@ -1,5 +1,5 @@ .. index:: -single: Console; Single command application + single: Console; Single command application Building a Single Command Application ===================================== diff --git a/components/map.rst.inc b/components/map.rst.inc index 842097c5ef2..7a5c74eeff6 100644 --- a/components/map.rst.inc +++ b/components/map.rst.inc @@ -19,7 +19,8 @@ * :doc:`/components/console/introduction` * :doc:`/components/console/usage` - * :doc:`/components/console/changing_default_behavior` + * :doc:`/components/console/single_command_tool` + * :doc:`/components/console/changing_default_command` * :doc:`/components/console/events` * :doc:`/components/console/helpers/index`