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] Add SingleCommandApplication to ease creation of Single Command Application #34819

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 9, 2020

Conversation

lyrixx
Copy link
Member

@lyrixx lyrixx commented Dec 4, 2019

Q A
Branch? master
Bug fix? no
New feature? yes
Deprecations? no
Tickets Fix #34293
License MIT
Doc PR

<?php

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

use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\SingleCommandApplication;

(new SingleCommandApplication())
    ->setName('My Super Command') // Optional
    ->setVersion('1.0.0') // Optional
    ->setProcessTitle('my_proc_title') // Optional
    ->addArgument('who', InputArgument::OPTIONAL, 'Who', 'World')  // Optional
    ->setCode(function(InputInterface $input, OutputInterface $output) {
        $output->writeln(sprintf('Hello %s!', $input->getArgument('who')));
    })
    ->run()
;

Note: I tried this too, and it works as expected:

class MyCommand extends SingleCommandApplication
{
    public function execute(InputInterface $input, OutputInterface $output)
    {
        $output->writeln('hello');

        return 0;
    }
}

new MyCommand();

@lyrixx lyrixx force-pushed the console-single-command branch 2 times, most recently from c985204 to 83ba9b0 Compare December 4, 2019 16:52
src/Symfony/Component/Console/SingleApplicationCommand.php Outdated Show resolved Hide resolved
@lyrixx lyrixx force-pushed the console-single-command branch from 83ba9b0 to 516713a Compare December 4, 2019 16:58
@lyrixx lyrixx changed the title [Console] Add SingleApplicationCommand to ease creation of Single Command Application [Console] Add SingleCommandApplication to ease creation of Single Command Application Dec 4, 2019
@nicolas-grekas nicolas-grekas added this to the next milestone Dec 4, 2019
Copy link
Member

@fabpot fabpot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me.

@rybakit

This comment has been minimized.

@lyrixx

This comment has been minimized.

@rybakit

This comment has been minimized.

@lyrixx

This comment has been minimized.

@chalasr

This comment has been minimized.

@lyrixx

This comment has been minimized.

@nicolas-grekas

This comment has been minimized.

@fabpot

This comment has been minimized.

@chalasr

This comment has been minimized.

@lyrixx

This comment has been minimized.

@chalasr

This comment has been minimized.

@lyrixx

This comment has been minimized.

@lyrixx lyrixx force-pushed the console-single-command branch from 7284543 to 0ee2fff Compare December 12, 2019 15:11
@lyrixx
Copy link
Member Author

lyrixx commented Dec 12, 2019

OK, I updated the PR to add the run() command. Hope you like it!

Copy link
Member

@chalasr chalasr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me.
It might be worth throwing from methods like setAliases() which make no sense for such command, but that's for another PR.
Build failures unrelated

@yceruto

This comment has been minimized.

@lyrixx
Copy link
Member Author

lyrixx commented Jan 8, 2020

Failures on appveyor & travis are not related

src/Symfony/Component/Console/SingleCommandApplication.php Outdated Show resolved Hide resolved
@lyrixx lyrixx force-pushed the console-single-command branch 2 times, most recently from 70c168e to d050196 Compare January 8, 2020 11:40
…mand Application

```
<?php

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

use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\SingleCommandApplication;

(new SingleCommandApplication())
    ->setName('My Super Command') // Optional
    ->setVersion('1.0.0') // Optional
    ->setProcessTitle('my_proc_title') // Optional
    ->addArgument('who', InputArgument::OPTIONAL, 'Who', 'World')  // Optional
    ->setCode(function(InputInterface $input, OutputInterface $output): int {
        $output->writeln(sprintf('Hello %s!', $input->getArgument('who')));

        return 0;
    })
    ->run()
;

```
@lyrixx lyrixx force-pushed the console-single-command branch from d050196 to 4af513d Compare January 8, 2020 11:40
@chalasr
Copy link
Member

chalasr commented Jan 9, 2020

Thank you @lyrixx.

chalasr added a commit that referenced this pull request Jan 9, 2020
…n of Single Command Application (lyrixx)

This PR was merged into the 5.1-dev branch.

Discussion
----------

[Console] Add SingleCommandApplication to ease creation of Single Command Application

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| Deprecations? | no
| Tickets       | Fix #34293
| License       | MIT
| Doc PR        |

---

```php
<?php

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

use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\SingleCommandApplication;

(new SingleCommandApplication())
    ->setName('My Super Command') // Optional
    ->setVersion('1.0.0') // Optional
    ->setProcessTitle('my_proc_title') // Optional
    ->addArgument('who', InputArgument::OPTIONAL, 'Who', 'World')  // Optional
    ->setCode(function(InputInterface $input, OutputInterface $output) {
        $output->writeln(sprintf('Hello %s!', $input->getArgument('who')));
    })
    ->run()
;

```

---

Note: I tried this too, and it works as expected:

```php

class MyCommand extends SingleCommandApplication
{
    public function execute(InputInterface $input, OutputInterface $output)
    {
        $output->writeln('hello');

        return 0;
    }
}

new MyCommand();
```

Commits
-------

4af513d [Console] Add SingleCommandApplication to ease creation of Single Command Application
@chalasr chalasr merged commit 4af513d into symfony:master Jan 9, 2020
@lyrixx lyrixx deleted the console-single-command branch January 9, 2020 07:34
@nicolas-grekas nicolas-grekas modified the milestones: next, 5.1 May 4, 2020
@fabpot fabpot mentioned this pull request May 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Console] Ease creation of single command application
8 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.