8000 [Console Component] Make creating single command app easier · Issue #9564 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Console Component] Make creating single command app easier #9564

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
soxofaan opened this issue Nov 21, 2013 · 11 comments
Closed

[Console Component] Make creating single command app easier #9564

soxofaan opened this issue Nov 21, 2013 · 11 comments

Comments

@soxofaan
Copy link

Hi,

(first attempt at contributing to symfony here)

I was reading on creating single command applications with Symfony console (because that's what I mostly need) and that felt a bit cumbersome/too much boilerplate.

Would there be interest in including something like the helper class below in the Symfony\Component\Console namespace, to make creating single command applications easier?

class SingleCommandApplication extends Application
{

    private $singleCommandName;

    public function __construct(Command $command, $name = 'UNKNOWN', $version = 'UNKNOWN')
    {
        parent::__construct($name, $version);

        // Add the given command as single (publicly accessible) command.
        $this->add($command);
        $this->singleCommandName = $command->getName();

        // Override the Application's definition so that it does not
        // require a command name as first argument.
        $this->getDefinition()->setArguments();
    }

    protected function getCommandName(InputInterface $input)
    {
        return $this->singleCommandName;
    }
}

Usage example (based on http://symfony.com/doc/current/components/console/introduction.html):

$application = new SingleCommandApplication(new GreetCommand());
$application->run();

I omitted docblocks and such to keep it to the point.
I just wanted to collect some quick feedback on this idea before making this a real pull request.

thanks.

@cordoval
Copy link
Contributor

not sure if you saw the other discussions going on with this, but maybe this is different, but doubt it 👶

@stof
Copy link
Member
8000
stof commented Nov 22, 2013

@cordoval I think this could be possible, as it is the proper way to do it. It is the way used in Behat for instance (the implementation is a bit different, but the idea is the same)

The previous discussion was trying to make the command name optional while keeping the possibility to have multiple commands in the application, which was causing lots of issues.
But a SingleCommandApplication which only allows running 1 command would be fine IMO.

@soxofaan
Copy link
Author

@cordoval you mean #3857 ? Yes, I saw it, but that one tries to change Application's behaviour regarding "no command given" cases, which is not the right way, as @stof mentioned. I also read #4104, but with that one you lose the features of an Application (error handling, help, ...).

My proposal is to add another class (subclassing from Application to inherit all the good stuff) which expects you to define (only) one command (through the constructor), being the single publicly accessible command, for which a command argument would be silly and thus can be dropped.
It's based on http://symfony.com/doc/current/components/console/single_command_tool.html but abstracted to avoid the boilerplate code if you want to make multiple single command apps :)

@stof
Copy link
Member
stof commented Nov 24, 2013

@soxofaan please open a PR for your proposal

I see one improvement compared to your code: it should prevent adding new commands by throwing an exception in add()

@soxofaan
Copy link
Author

I thought about that, but it still might be interesting to add multiple commands which still can be called internally from the "main" command ($application->find($commandName'); etc ...)

8000

@soxofaan
Copy link
Author

I started implementing at https://github.com/soxofaan/symfony/compare/ticket9564-console-single-command-app

Not completely ready to make a PR:
The application help now renders a synopsis like

commandname [-b|--bar] [items1] ... [itemsN]

but I'd want that it does not show commandname

I'm still figuring out if that is possible to to in a clean way

@wouterj
Copy link
Member
wouterj commented Nov 25, 2013

Could you please open a [WIP] (Work In Progress) PR, that'll make it a lot easier to review and discuss the implementation

@soxofaan
Copy link
Author

sure: #9609

soxofaan pushed a commit to soxofaan/symfony that referenced this issue Nov 27, 2013
Added SingleCommandApplication to simplify creating and
using Applications that only provide one Command.
soxofaan pushed a commit to soxofaan/symfony that referenced this issue Feb 1, 2014
Added SingleCommandApplication to simplify creating and
using Applications that only provide one Command.
soxofaan pushed a commit to soxofaan/symfony that referenced this issue Feb 1, 2014
Added SingleCommandApplication to simplify creating and
using Applications that only provide one Command.
soxofaan pushed a commit to soxofaan/symfony that referenced this issue Mar 16, 2014
Added SingleCommandApplication to simplify creating and
using Applications that only provide one Command.
soxofaan pushed a commit to soxofaan/symfony that referenced this issue Mar 16, 2014
Added SingleCommandApplication to simplify creating and
using Applications that only provide one Command.
@rquadling
Copy link
Contributor

Is this feature going to become part of Symfony?

@TomasVotruba
Copy link
Contributor

Check Symfony docs: Building a single Command Application

It's nicely explained there

@javiereguiluz
Copy link
Member

Closing this issue because the related PR was closed in favor of the newer proposal explained in #16906.

fabpot added a commit that referenced this issue Jun 15, 2016
This PR was merged into the 3.2-dev branch.

Discussion
----------

[Console] Better support for one command app

| Q             | A
| ------------- | ---
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #9564
| License       | MIT

Hello;

I write many CLI application, and "single command" in cli application is not so easy to write.
This is why I propose this patch. IMHO, this PR could replaces #9609.

See it in application:
```php
#!/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', 'foo')
        ->addOption('bar', null, InputOption::VALUE_REQUIRED, 'Foobar', 'bar')
        ->setCode(function(InputInterface $input, OutputInterface $output) {
            $output->writeln('start');
            $output->writeln($input->getArgument('foo'));
            $output->writeln($input->getOption('bar'));
        })
    ->getApplication()
    ->setSingleCommand('echo')
    ->run();
```

Some usage:
```
>(3)[{..}eg/dev/github.com/symfony/symfony](console-one-app) php test.php
start
foo
bar
```
```
>(3)[{..}eg/dev/github.com/symfony/symfony](console-one-app) php test.php  "first argument"
start
first argument
bar
```
```
>(3)[{..}eg/dev/github.com/symfony/symfony](console-one-app) php test.php  "first argument" --bar="first option"
start
first argument
first option
```
```
>(3)[{..}eg/dev/github.com/symfony/symfony](console-one-app) php test.php  "first argument" --bar="first option" --help
Usage:
  echo [options] [--] [<foo>]

Arguments:
  foo                   The directory [default: "foo"]

Options:
      --bar=BAR         Foobar [default: "bar"]
  -h, --help            Display this help message
  -q, --quiet           Do not output any message
  -V, --version         Display this application version
      --ansi            Force ANSI output
      --no-ansi         Disable ANSI output
  -n, --no-interaction  Do not ask any interactive question
  -v|vv|vvv, --verbose  Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug
```

Commits
-------

4a9bb1d [Console] Better support for one command app
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants
0