parser = ArgumentParser::create() ->requiredString('email', 'User email address') ->requiredString('name', 'Full name of the user') ->choice('role', ['user', 'admin', 'moderator'], false, 'user', 'User role') ->flag('force', 'f', 'Skip confirmation prompts') ->flag('notify', null, 'Send welcome email to user') ->integer('quota', false, 100, 'Storage quota in MB') ->build(); } #[ConsoleCommand('user:create', 'Create a new user account')] public function execute(ConsoleInput $input, ConsoleOutputInterface $output): ExitCode { try { // Set the enhanced parser for this command $input->setArgumentParser($this->parser); // Validate all required arguments $input->validate(); // Get typed arguments with automatic validation $email = new Email($input->getString('email')); $name = $input->getString('name'); $role = $input->getString('role'); $force = $input->getBool('force'); $notify = $input->getBool('notify'); $quota = $input->getInt('quota'); // Display what we're about to do $output->writeLine("Creating user with the following details:"); $output->writeLine(" Email: {$email->toString()}"); $output->writeLine(" Name: {$name}"); $output->writeLine(" Role: {$role}"); $output->writeLine(" Quota: {$quota} MB"); $output->writeLine(" Notify: " . ($notify ? 'Yes' : 'No')); $output->newLine(); // Confirm unless forced if (! $force && ! $input->confirm('Create this user?', false)) { $output->writeInfo('User creation cancelled.'); return ExitCode::SUCCESS; } // Simulate user creation $output->writeSuccess("User '{$name}' created successfully!"); if ($notify) { $output->writeInfo("Welcome email sent to {$email->toString()}"); } return ExitCode::SUCCESS; } catch (\InvalidArgumentException $e) { $output->writeError($e->getMessage()); $this->showHelp($output); return ExitCode::INVALID_INPUT; } catch (\Exception $e) { $output->writeError("Failed to create user: " . $e->getMessage()); return ExitCode::GENERAL_ERROR; } } #[ConsoleCommand('user:create:help', 'Show help for user:create command')] public function showHelp(ConsoleInput $input, ConsoleOutputInterface $output): ExitCode { $helpGenerator = new HelpGenerator($output); $helpGenerator->display( 'user:create', $this->parser->getDefinitions(), 'Create a new user account with email, name, and optional settings.' ); return ExitCode::SUCCESS; } /** * Example usage methods to demonstrate all features */ public function getExampleUsages(): array { return [ // Basic usage 'php console user:create john@example.com "John Doe"', // With role 'php console user:create john@example.com "John Doe" --role=admin', // With short flags 'php console user:create john@example.com "John Doe" -f --notify', // With quota 'php console user:create john@example.com "John Doe" --quota=500 --role=moderator', // All options 'php console user:create john@example.com "John Doe" --role=admin --force --notify --quota=1000', ]; } }