-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Console] Ease creation of single command application #34293
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
Comments
Passing anonymous function like that tends to make code messy. I would prefer the use of a class. Something like this:
This way, it would be easier to reuse commands later, when the app gets bigger. This also allows to easily move command into a separate executable. And also, it uses already known and well-established interface to configure the commands. |
@jkufner the point here is to make only one command and so with the less boilerplate code. And if we could leverage the existing infrastructure, it's better. This is why I think "anonymous function like that tends to make code messy" is not relevant here. |
@lyrixx How simple and readable that minimalistic boilerplate code gets when the command needs a few options and a nice help? How can DI container inject few dependencies? The anonymous function is fine for the most trivial case, but it does not scale well to handle nontrivial cases. I think that the one extra line the class declaration requires is worth the scalability. |
Like before: (new SingleApplicationCommand())
->setHelp()
->addOption(...)
->setCode(function(InputInterface $input, OutputInterface $output) {
$output->writeln('hello');
})
;
Usually, single command does not use a DIC, because it's not really useful here. But anyway, if you want, you can inject your dependencies like following A] see the (new SingleApplicationCommand())
->setHelp()
->addOption(...)
->setCode(function(InputInterface $input, OutputInterface $output) use ($a, $b) {
$output->writeln('hello');
})
; B] Another idea (I did not tested it) (new class extends SingleApplicationCommand() {
private $a;
public function __construct($a) {
$this->a = $a;
parent::__construct();
)
public function execute(InputInterface $input, OutputInterface $output) {
$output->writeln('hello');
}
}); |
The whole discussion around closures is beside the point. With a regular command, you have the choice to either use |
…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
Description
I write lot's of "simple" Symfony CLI Application.
I already submitted many PRs to be able to use the following boilerplate code:
Even if this is already straightforward, I think we can do better.
What do you think of the following code instead:
The text was updated successfully, but these errors were encountered: