8000 [2.2][Console] command's name required when added to an application by brikou · Pull Request #4104 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[2.2][Console] command's name required when added to an application #4104

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

Closed
wants to merge 1 commit into from

Conversation

brikou
Copy link
Contributor
@brikou brikou commented Apr 25, 2012

Build Status


Here is an application with a single purpose/command : listing a directory as seen here

<?php
// console.php
// usage: php console.php ls <dir>

require_once __DIR__.'/../../../../../vendor/autoload.php';

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

$console = new Application();
$console
    ->register('ls')
    ->setDefinition(array(
        new InputArgument('dir', InputArgument::REQUIRED, 'Directory name'),
    ))
    ->setDescription('Displays the files in the given directory')
    ->setCode(function (InputInterface $input, OutputInterface $output) {
        $dir = $input->getArgument('dir');

        $output->writeln(sprintf('Dir listing for <info>%s</info>', $dir));
    })
;
$console->run();

This simple command can be refactored into this...

<?php
// ls.php
// usage: php ls.php <dir>

require_once __DIR__.'/../../../../../vendor/autoload.php';

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Output\ConsoleOutput;

$command = new Command("ANameAsItIsMandatory");
$command
    ->setDefinition(array(
        new InputArgument('dir', InputArgument::REQUIRED, 'Directory name'),
    ))
    ->setDescription('Displays the files in the given directory')
    ->setCode(function (InputInterface $input, OutputInterface $output) {
        $dir = $input->getArgument('dir');

        $output->writeln(sprintf('Dir listing for <info>%s</info>', $dir));
    })
;

$command->run(new ArgvInput(), new ConsoleOutput());

The conceptual problem is that command's name is required even if not needed/used.
This PR makes the name required only when this command is attached to an application.

@stof
Copy link
Member
stof commented Oct 13, 2012

@brikou The issue if you run the command directly instead of using an application is that you loose some features:

  • you have to reimplement the display of the help of the command whereas the application provides the --help option;
  • you have to reimplement the handling of exceptions yourself;
  • you have to reimplement the option used to control the interactivity, color support and verbosity of the console as the application is responsible to handle them.

A better way to achieve single-command applications can be found in symfony/symfony-docs#1810 (not merged yet so not yet in the online doc)

@stof
Copy link
Member
stof commented Oct 13, 2012

@fabpot what do you think about it ? should we still merge it or close it ?

@fabpot
Copy link
Member
fabpot commented Oct 14, 2012

Closing it in favor of the doc update.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants
0