Пример #1
0
 /**
  * Make a new Subcommand
  *
  * @param string|array $name The long name of the subcommand, or an array with all the properties.
  * @param string $help The help text for this option.
  * @param CommandLineParser|null $parser A parser for this subcommand. 
  */
 public function __construct($name, $help = '', $parser = null)
 {
     if (is_array($name) && isset($name['name'])) {
         foreach ($name as $key => $value) {
             $this->{'_' . $key} = $value;
         }
     } else {
         $this->_name = $name;
         $this->_help = $help;
         $this->_parser = $parser;
     }
     if ($this->_parser) {
         $this->_parser->command($this->_name);
     }
 }
Пример #2
0
 /**
  * Generate the usage for a shell based on its arguments and options.
  * Usage strings favor short options over the long ones. and optional args will
  * be indicated with []
  *
  * @return string
  */
 protected function _generateUsage()
 {
     $usage = ['coretyson ' . $this->_parser->command()];
     $subcommands = $this->_parser->subcommands();
     if (!empty($subcommands)) {
         $usage[] = '<subcommand>';
     }
     $options = [];
     foreach ($this->_parser->options() as $option) {
         $options[] = $option->usage();
     }
     if (count($options) > $this->_maxOptions) {
         $options = ['[options]'];
     }
     $usage = array_merge($usage, $options);
     $args = [];
     foreach ($this->_parser->arguments() as $argument) {
         $args[] = $argument->usage();
     }
     if (count($args) > $this->_maxArgs) {
         $args = ['[arguments]'];
     }
     $usage = array_merge($usage, $args);
     return implode(' ', $usage);
 }
Пример #3
0
 /**
  * Dispatch a request.
  *
  * @return bool
  * @throws ConsoleException
  * @throws MissingShellException
  * @throws MissingShellMethodException
  */
 protected function _dispatch()
 {
     $parser = new CommandLineParser();
     $shellListTask = new ShellListTask();
     $shells = [];
     foreach ($shellListTask->getShellList() as $shellName) {
         $sh = $this->findShell($shellName);
         if ($sh) {
             if (!$sh instanceof AbstractShell) {
                 $class = get_class($sh);
                 throw new ConsoleException("{$class} must extend AbstractShell");
             }
             $parser->addSubcommand(new CommandLineSubCommand(["name" => $shellName, "parser" => $sh->getParser()]));
             $shells[$shellName] = $sh;
         }
     }
     $parser->process($this->args);
     $subcommand = $parser->triggeredSubCommand();
     if ($subcommand) {
         $sh = $shells[$subcommand];
         $sh->console($this);
         if (!method_exists($sh, 'main')) {
             throw new MissingShellMethodException([$sh, 'main']);
         }
         return $sh->main($this->args);
     } else {
         $this->out($parser->helpText());
         return 0;
     }
 }