Cml\Console\Commands\Help::execute PHP Метод

execute() публичный Метод

执行命令入口
public execute ( array $args, array $options = [] )
$args array 参数
$options array 选项
    public function execute(array $args, array $options = [])
    {
        $this->writeln("CmlPHP Console " . Cml::VERSION . "\n", ['foregroundColors' => [Colour::GREEN, Colour::HIGHLIGHT]]);
        $format = new Format(['indent' => 2]);
        if (empty($args)) {
            $this->writeln("Usage:");
            $this->writeln($format->format("input 'command [options] [args]' to run command or input 'help command ' to display command help info"));
            $this->writeln('');
            $options = $this->formatOptions();
            $cmdList = $this->formatCommand();
            $this->writeln("Options:");
            $this->formatEcho($format, $options);
            $this->writeln('');
            $this->writeln('Available commands:');
            $this->formatEcho($format, $cmdList[0]);
            $this->formatEcho($format, $cmdList[1]);
        } else {
            $class = new \ReflectionClass($this->console->getCommand($args[0]));
            $property = $class->getDefaultProperties();
            $description = isset($property['description']) ? $property['description'] : '';
            $help = isset($property['help']) ? $property['help'] : false;
            $arguments = isset($property['arguments']) ? $property['arguments'] : [];
            $options = isset($property['options']) ? $property['options'] : [];
            $this->writeln("Usage:");
            $this->writeln($format->format("{$args[0]} [options] [args]"));
            $this->writeln('');
            count($arguments) > 0 && ($arguments = $this->formatArguments($arguments));
            $options = $this->formatOptions($options, 'this');
            $this->writeln("Options:");
            $this->formatEcho($format, $options);
            $this->writeln('');
            if (count($arguments)) {
                $this->writeln("Arguments");
                $this->formatEcho($format, $arguments);
                $this->writeln('');
            }
            $this->writeln("Help:");
            $this->writeln($format->format($help ? $help : $description));
        }
        $this->write("\n");
    }

Usage Example

Пример #1
0
 /**
  * 运行命令
  *
  * @param array|null $argv
  *
  * @return mixed
  */
 public function run(array $argv = null)
 {
     try {
         if ($argv === null) {
             $argv = isset($_SERVER['argv']) ? array_slice($_SERVER['argv'], 1) : [];
         }
         list($args, $options) = Input::parse($argv);
         $command = count($args) ? array_shift($args) : 'help';
         if (!isset($this->commands[$command])) {
             throw new \InvalidArgumentException("Command '{$command}' does not exist");
         }
         isset($options['no-ansi']) && Colour::setNoAnsi();
         if (isset($options['h']) || isset($options['help'])) {
             $help = new Help($this);
             $help->execute([$command]);
             exit(0);
         }
         $command = explode('::', $this->commands[$command]);
         return call_user_func_array([new $command[0]($this), isset($command[1]) ? $command[1] : 'execute'], [$args, $options]);
     } catch (\Exception $e) {
         Output::writeException($e);
         exit(1);
     }
 }