/** * {@inheritdoc} */ public function run($data) { if (!is_array($data)) { throw new InvalidArgumentException("invalid value provided for 'data'; expecting in indexed array " . "of command line options (use \$argv)"); } try { $parser = new ArgumentParser(array_slice($data, 1)); $parser->parseInto($this->task); } catch (Exception $ex) { $cmd = Cli::getScriptName(); Env::error($ex->getMessage() . "\n" . "use '@[bold]{$cmd} --help@[reset]' for help\n\n"); } if ($this->task->validate() === false) { $errors = $this->task->getErrors(); Env::error($this->formatErrors($errors)); } return $this->task->run($this->task->exportValues()); }
/** * Create a compact version of basic usage instructions * * @param boolean $header Whether or not to include a help header * @return string */ public function createUsage($header = false) { $ret = ''; $bools = []; $others = []; foreach ($this->fields as $field) { $name = $field->getName(); $usage = $this->createFieldUsage($field); if ($field instanceof BooleanField) { $bools[$name] = $usage; } else { $others[$name] = $usage; } } $opts = array_merge(array_values($bools), array_values($others)); $cmd = Cli::getScriptName(); $maxWidth = min(Cli::getWidth(), 78); if ($header === true) { $ret .= $this->createHeader('usage'); $tmp = " {$cmd}"; } else { $tmp = "usage: {$cmd}"; } $indent = strlen($tmp); foreach ($opts as $opt) { if (strlen($tmp) + strlen($opt) >= $maxWidth) { $ret .= "{$tmp}\n"; $tmp = str_pad('', $indent, ' ') . " {$opt}"; } else { $tmp .= " {$opt}"; } } $ret .= $tmp; return $ret; }