new InputArgument(trim($token, '?*'), InputArgument::IS_ARRAY, $description), str_ends_with($token, '*') => new InputArgument(trim($token, '*'), InputArgument::IS_ARRAY | InputArgument::REQUIRED, $description), str_ends_with($token, '?') => new InputArgument(trim($token, '?'), InputArgument::OPTIONAL, $description), (bool) preg_match('/(.+)\=\*(.+)/', $token, $matches) => new InputArgument($matches[1], InputArgument::IS_ARRAY, $description, preg_split('/,\s?/', $matches[2])), (bool) preg_match('/(.+)\=(.+)/', $token, $matches) => new InputArgument($matches[1], InputArgument::OPTIONAL, $description, $matches[2]), default => new InputArgument($token, InputArgument::REQUIRED, $description), }; } /** * Parse an option expression. * * @param string $token * @return \Symfony\Component\Console\Input\InputOption */ protected static function parseOption(string $token) { [$token, $description] = static::extractDescription($token); $matches = preg_split('/\s*\|\s*/', $token, 2); $shortcut = null; if (isset($matches[1])) { $shortcut = $matches[0]; $token = $matches[1]; } return match (true) { str_ends_with($token, '=') => new InputOption(trim($token, '='), $shortcut, InputOption::VALUE_OPTIONAL, $description), str_ends_with($token, '=*') => new InputOption(trim($token, '=*'), $shortcut, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, $description), (bool) preg_match('/(.+)\=\*(.+)/', $token, $matches) => new InputOption($matches[1], $shortcut, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, $description, preg_split('/,\s?/', $matches[2])), (bool) preg_match('/(.+)\=(.+)/', $token, $matches) => new InputOption($matches[1], $shortcut, InputOption::VALUE_OPTIONAL, $description, $matches[2]), default => new InputOption($token, $shortcut, InputOption::VALUE_NONE, $description), }; } /** * Parse the token into its token and description segments. * * @param string $token * @return array{string, string} */ protected static function extractDescription(string $token) { $parts = preg_split('/\s+:\s+/', trim($token), 2); return count($parts) === 2 ? $parts : [$token, '']; } }