use Symfony\Component\Console\Input\InputInterface; class GreetCommand extends Command { protected function configure() { $this ->setName('greet') ->setDescription('Greet someone') ->addArgument('name', InputArgument::REQUIRED, 'The name of the person to greet') ; } protected function execute(InputInterface $input, OutputInterface $output) { $name = $input->getArgument('name'); $output->writeln('Hello, '.$name.'!'); } }
use Symfony\Component\Console\Input\InputInterface; class GreetCommand extends Command { protected function configure() { $this ->setName('greet') ->setDescription('Greet someone') ->addArgument('name', InputArgument::OPTIONAL, 'The name of the person to greet') ; } protected function execute(InputInterface $input, OutputInterface $output) { if ($input->hasArgument('name')) { $name = $input->getArgument('name'); } else { $name = $input->getOption('default-name'); } $output->writeln('Hello, '.$name.'!'); } }In this code, we have defined a command named "greet" that has an optional "name" argument. When the command is executed, the InputInterface object is passed to the execute() method, and the optional "name" argument is retrieved using the hasArgument() and getInputOption() methods. If the "name" argument is not provided, the value of the "default-name" option is used instead.