use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; class HelloWorldCommand extends Command { protected function configure() { $this ->setName('hello') ->setDescription('Outputs "Hello, World!"'); } protected function execute(InputInterface $input, OutputInterface $output) { $output->writeln('Hello, World!'); } } $command = new HelloWorldCommand(); $command->setCode('echo "Hello, World!";');
use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; class CalculateSumCommand extends Command { protected function configure() { $this ->setName('sum') ->setDescription('Calculates the sum of two numbers') ->addArgument('num1') ->addArgument('num2'); } protected function execute(InputInterface $input, OutputInterface $output) { $num1 = $input->getArgument('num1'); $num2 = $input->getArgument('num2'); $output->writeln($num1 + $num2); } } $command = new CalculateSumCommand(); $command->setCode(function(InputInterface $input, OutputInterface $output) { $num1 = $input->getArgument('num1'); $num2 = $input->getArgument('num2'); $output->writeln($num1 + $num2); });In this example, we define a `CalculateSumCommand` class that extends the `Command` class. We use the `setName`, `setDescription`, and `addArgument` methods to define the name, description, and arguments of the command. We then define the code to be executed when the command is run using a closure instead of a string. The closure takes two arguments, `$input` and `$output`, and calculates the sum of the two arguments passed to the command. The package library is `symfony/console`.