#!/usr/bin/env php error("Please run this script via CLI!"); exit(1); } if (count($argv) === 1 || in_array($argv[1], ["h", "-h", "help", "--help"], true)) { $climate->out($help); exit(0); } if (!in_array($argv[1], $commands)) { $climate->br()->error(" Unknown command '{$argv[1]}'. Use --help for a list of available commands."); $suggestion = \Kelunik\AcmeClient\suggestCommand($argv[1], $commands); if ($suggestion) { $climate->br()->out(" Did you mean '$suggestion'?"); } $climate->br(); exit(1); } /** @var \Kelunik\AcmeClient\Commands\Command $class */ $class = "Kelunik\\AcmeClient\\Commands\\" . ucfirst($argv[1]); $definition = $class::getDefinition(); try { $args = $argv; unset($args[1]); $climate->arguments->add($definition); $climate->arguments->parse(array_values($args)); } catch (Exception $e) { if (count($argv) === 3 && in_array($argv[2], ["h", "-h", "--help", "help"], true)) { $climate->usage(["{$binary} {$argv[1]}"]); $climate->br(); exit(0); } else { $climate->usage(["{$binary} {$argv[1]}"]); $climate->br(); $climate->error($e->getMessage()); $climate->br(); exit(1); } } $injector = new Injector; $injector->share($climate); $injector->share(new AcmeFactory); $command = $injector->make($class); Amp\run(function () use ($command, $climate) { $handler = function ($e) use ($climate) { $error = (string) $e; $lines = explode("\n", $error); $lines = array_filter($lines, function ($line) { return strlen($line) && $line[0] !== "#" && $line !== "Stack trace:"; }); foreach ($lines as $line) { $climate->error($line)->br(); } exit(1); }; try { $exitCode = (yield $command->execute($climate->arguments)); if ($exitCode === null) { exit(0); } exit($exitCode); } catch (Throwable $e) { $handler($e); } catch (Exception $e) { $handler($e); } Amp\stop(); });