Skip to content

Instantly share code, notes, and snippets.

@wouterj
Last active May 25, 2024 13:32
Show Gist options
  • Save wouterj/fb94f7c8adf05192eee70fb3895b7d47 to your computer and use it in GitHub Desktop.
Save wouterj/fb94f7c8adf05192eee70fb3895b7d47 to your computer and use it in GitHub Desktop.
Symfony Command standalone usage
<?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();
<?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