Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[Console] Updated section 'Building a single Command Application' with the new 'SingleCommandApplication' #12917

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 16, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[Console] Updated section 'Building a single Command Application' wit…
…h the new 'SingleCommandApplication'
  • Loading branch information
lyrixx committed Jan 15, 2020
commit 70d523c7c581f973e2ee589f3ce4946077a718e9
40 changes: 23 additions & 17 deletions 40 components/console/single_command_tool.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,36 @@ 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 declaring a single command application::
In such case, having to pass the command name each time is tedious.

.. versionadded:: 5.1

The class :class:`Symfony\\Component\\Console\\SingleCommandApplication` was
introduced in Symfony 5.1.

Fortunately, it is possible to remove this need by declaring a single command
application::

#!/usr/bin/env php
<?php
require __DIR__.'/vendor/autoload.php';

use Symfony\Component\Console\Application;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

(new Application('echo', '1.0.0'))
->register('echo')
->addArgument('foo', InputArgument::OPTIONAL, 'The directory')
->addOption('bar', null, InputOption::VALUE_REQUIRED)
->setCode(function(InputInterface $input, OutputInterface $output) {
// output arguments and options
})
->getApplication()
->setDefaultCommand('echo', true) // Single command application
use Symfony\Component\Console\SingleCommandApplication;

(new SingleCommandApplication())
->setName('My Super Command') // Optional
->setVersion('1.0.0') // Optional
->addArgument('foo', InputArgument::OPTIONAL, 'The directory')
->addOption('bar', null, InputOption::VALUE_REQUIRED)
->setCode(function (InputInterface $input, OutputInterface $output) {
// output arguments and options
})
->run();

The method :method:`Symfony\\Component\\Console\\Application::setDefaultCommand`
accepts a boolean as second parameter. If true, the command ``echo`` will then
always be used, without having to pass its name.

You can still register a command as usual::

#!/usr/bin/env php
Expand All @@ -49,3 +51,7 @@ You can still register a command as usual::

$application->setDefaultCommand($command->getName(), true);
$application->run();

The method :method:`Symfony\\Component\\Console\\Application::setDefaultCommand`
accepts a boolean as second parameter. If true, the command ``echo`` will then
always be used, without having to pass its name.
Morty Proxy This is a proxified and sanitized view of the page, visit original site.