/** * Initialize options. * * @param array $options Options, as described in * \Hoa\Console\GetOption. * @return array */ public function setOptions(array $options) { $old = $this->options; $this->options = $options; $rule = $this->router->getTheRule(); $variables = $rule[Router::RULE_VARIABLES]; if (isset($variables['_tail'])) { $this->parser->parse($variables['_tail']); $this->_options = new Console\GetOption($this->options, $this->parser); } return $old; }
/** * Resolve option ambiguity. Please see the special pipette entry * “ambiguous” in the self::__construct method. * For a smarter resolving, you could use the console kit (please, see * Hoa\Console\Dispatcher\Kit). * * @param array $solutions Solutions. * @return void * @throws \Hoa\Console\Exception */ public function resolveOptionAmbiguity(array $solutions) { if (!isset($solutions['solutions']) || !isset($solutions['value']) || !isset($solutions['option'])) { throw new Exception('Cannot resolve option ambiguity because the given solution ' . 'seems to be corruped.', 1); } $choices = $solutions['solutions']; if (1 > count($choices)) { throw new Exception('Cannot resolve ambiguity, fix your typo in the option %s :-).', 2, $solutions['option']); } $theSolution = $choices[0]; foreach ($this->_options as $option) { if ($theSolution == $option[self::OPTION_NAME] || $theSolution == $option[self::OPTION_VAL]) { $argument = $option[self::OPTION_HAS_ARG]; $value = $solutions['value']; if (self::NO_ARGUMENT === $argument) { if (!is_bool($value)) { $this->_parser->transferSwitchToInput($theSolution, $value); } } elseif (self::REQUIRED_ARGUMENT === $argument && !is_string($value)) { throw new Exception('The argument %s requires a value (it is not a switch).', 3, $theSolution); } unset($this->_pipette[null]); $this->_pipette[] = [$option[self::OPTION_VAL], $value]; $this->_pipette[null] = null; return; } } return; }
/** * Prepare the pipette. * * @param array $options The option definition. * @param \Hoa\Console\Core\Cli\Parser $parser The parser. * @return void * @throws \Hoa\Console\Exception */ public function __construct(array $options, Parser $parser) { $this->_options = $options; $this->_parser = $parser; if (empty($options)) { $this->_pipette[null] = null; return; } $names = []; foreach ($options as $i => $option) { if (isset($option[self::OPTION_NAME])) { $names[$option[self::OPTION_NAME]] = $i; } if (isset($option[self::OPTION_VAL])) { $names[$option[self::OPTION_VAL]] = $i; } } $_names = array_keys($names); $switches = $parser->getSwitches(); foreach ($switches as $name => $value) { if (false === in_array($name, $_names)) { if (1 === strlen($name)) { $this->_pipette[] = ['__ambiguous', ['solutions' => [], 'value' => $value, 'option' => $name]]; continue; } $haystack = implode(';', $_names); $differences = (int) ceil(strlen($name) / 3); $searched = Ustring\Search::approximated($haystack, $name, $differences); $solutions = []; foreach ($searched as $s) { $h = substr($haystack, $s['i'], $s['l']); if (false !== strpos($h, ';') || false !== in_array($h, array_keys($switches)) || false === in_array($h, $_names)) { continue; } $solutions[] = $h; } if (empty($solutions)) { continue; } $this->_pipette[] = ['__ambiguous', ['solutions' => $solutions, 'value' => $value, 'option' => $name]]; continue; } $option = $options[$names[$name]]; $argument = $option[self::OPTION_HAS_ARG]; if (self::NO_ARGUMENT === $argument) { if (!is_bool($value)) { $parser->transferSwitchToInput($name, $value); } } elseif (self::REQUIRED_ARGUMENT === $argument && !is_string($value)) { throw new Exception('The argument %s requires a value (it is not a switch).', 0, $name); } $this->_pipette[] = [$option[self::OPTION_VAL], $value]; } $this->_pipette[null] = null; reset($this->_pipette); return; }