use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; class SayHelloCommand extends Command { protected function configure() { $this->setName('say:hello') ->setDescription('Say hello to someone') ->addArgument('name', InputArgument::REQUIRED, 'Name of the person to say hello to.'); } protected function execute(InputInterface $input, OutputInterface $output) { if ($input->hasArgument('name')) { $name = $input->getArgument('name'); $output->writeln("Hello, $name!"); } else { $output->writeln('Please provide a name.'); } } }
use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; class GreetCommand extends Command { protected function configure() { $this->setName('greet') ->setDescription('Greet someone') ->addArgument('name', InputArgument::OPTIONAL, 'Name of the person to greet.', 'World'); } protected function execute(InputInterface $input, OutputInterface $output) { if ($input->hasArgument('name')) { $name = $input->getArgument('name'); $output->writeln("Hello, $name!"); } else { $output->writeln('Hello, World!'); } } }In both examples, we have used the InputInterface class from the Symfony\Component\Console\Input package library to check if an argument was passed to the console command using the hasArgument method.