Last active
May 25, 2024 13:32
-
-
Save wouterj/fb94f7c8adf05192eee70fb3895b7d47 to your computer and use it in GitHub Desktop.
Symfony Command standalone usage
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Symfony\Component\Console\Application; | |
use Symfony\Component\Console\Command\Command; | |
use Symfony\Component\Console\Input\InputArgument; | |
use Symfony\Component\Console\Input\InputInterface; | |
use Symfony\Component\Console\Input\InputOption; | |
use Symfony\Component\Console\Output\OutputInterface; | |
$app = new Application(); | |
$command = (new Command('app:my-command')) | |
->addArgument('foo', InputArgument::OPTIONAL, 'The directory') | |
->addOption('bar', null, InputOption::VALUE_REQUIRED) | |
->setCode(function (InputInterface $input, OutputInterface $output): int { | |
// ... | |
}) | |
; | |
$app->add($command); | |
// alternative: | |
$app->add(new Command('app:my-command')) | |
->addArgument('foo', InputArgument::OPTIONAL, 'The directory') | |
->addOption('bar', null, InputOption::VALUE_REQUIRED) | |
->setCode(function (InputInterface $input, OutputInterface $output): int { | |
// ... | |
}) | |
; | |
$app->run(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Symfony\Component\Console\Input\InputArgument; | |
use Symfony\Component\Console\Input\InputInterface; | |
use Symfony\Component\Console\Input\InputOption; | |
use Symfony\Component\Console\Output\OutputInterface; | |
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): int { | |
// ... | |
}) | |
->run(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment