/** * Execute the action with the value entered by the user. * * @param mixed $value the option value * @param array $params an array of optional parameters * * @return string * @access public */ public function execute($value = false, $params = array()) { if (!is_numeric($value)) { include_once 'Console/CommandLine/Exception'; throw Console_CommandLine_Exception::build('OPTION_VALUE_TYPE_ERROR', array('name' => $this->option->name, 'type' => 'float', 'value' => $value), $this->parser); } $this->setResult((int) $value); }
/** * Format the value $value according to the action of the option and * update the passed Console_CommandLine_Result object. * * @param mixed $value the value to format * @param object $result a Console_CommandLine_Result instance * @param object $parser a Console_CommandLine instance * * @return void * @access public */ public function dispatchAction($value, $result, $parser) { // check value is in option choices if (!empty($this->choices) && !in_array($value, $this->choices)) { throw Console_CommandLine_Exception::build('OPTION_VALUE_NOT_VALID', array('name' => $this->name, 'choices' => implode('", "', $this->choices), 'value' => $value), $parser); } $actionInfo = Console_CommandLine::$actions[$this->action]; if (true === $actionInfo[1]) { // we have a "builtin" action $tokens = explode('_', $actionInfo[0]); include_once implode('/', $tokens) . '.php'; } $clsname = $actionInfo[0]; $action = new $clsname($result, $this, $parser); $action->execute($value, $this->action_params); }
/** * Parse the command line token and modify *by reference* the $options and * $args arrays. * * @param string $token the command line token to parse * @param object $result the Console_CommandLine_Result instance * @param array &$args the argv array * @param int $argc number of lasting args * * @return void * @access protected * @throws Exception on user errors */ protected function parseToken($token, $result, &$args, $argc) { static $lastopt = false; static $stopflag = false; $last = $argc === 0; $token = trim($token); if (!$stopflag && $lastopt) { if (substr($token, 0, 1) == '-') { if ($lastopt->argument_optional) { $this->_dispatchAction($lastopt, '', $result); if ($lastopt->action != 'StoreArray') { $lastopt = false; } } else { if (isset($result->options[$lastopt->name])) { // case of an option that expect a list of args $lastopt = false; } else { throw Console_CommandLine_Exception::build('OPTION_VALUE_REQUIRED', array('name' => $lastopt->name), $this); } } } else { // when a StoreArray option is positioned last, the behavior // is to consider that if there's already an element in the // array, and the commandline expects one or more args, we // leave last tokens to arguments if ($lastopt->action == 'StoreArray' && !empty($result->options[$lastopt->name]) && count($this->args) > $argc + count($args)) { $args[] = $token; return; } $this->_dispatchAction($lastopt, $token, $result); if ($lastopt->action != 'StoreArray') { $lastopt = false; } return; } } if (!$stopflag && substr($token, 0, 2) == '--') { // a long option $optkv = explode('=', $token, 2); if (trim($optkv[0]) == '--') { // the special argument "--" forces in all cases the end of // option scanning. $stopflag = true; return; } $opt = $this->findOption($optkv[0]); if (!$opt) { throw Console_CommandLine_Exception::build('OPTION_UNKNOWN', array('name' => $optkv[0]), $this); } $value = isset($optkv[1]) ? $optkv[1] : false; if (!$opt->expectsArgument() && $value !== false) { throw Console_CommandLine_Exception::build('OPTION_VALUE_UNEXPECTED', array('name' => $opt->name, 'value' => $value), $this); } if ($opt->expectsArgument() && $value === false) { // maybe the long option argument is separated by a space, if // this is the case it will be the next arg if ($last && !$opt->argument_optional) { throw Console_CommandLine_Exception::build('OPTION_VALUE_REQUIRED', array('name' => $opt->name), $this); } // we will have a value next time $lastopt = $opt; return; } if ($opt->action == 'StoreArray') { $lastopt = $opt; } $this->_dispatchAction($opt, $value, $result); } else { if (!$stopflag && substr($token, 0, 1) == '-') { // a short option $optname = substr($token, 0, 2); if ($optname == '-') { // special case of "-" passed on the command line, it should be // treated as an argument $args[] = $optname; return; } $opt = $this->findOption($optname); if (!$opt) { throw Console_CommandLine_Exception::build('OPTION_UNKNOWN', array('name' => $optname), $this); } // parse other options or set the value // in short: handle -f<value> and -f <value> $next = substr($token, 2, 1); // check if we must wait for a value if ($next === false) { if ($opt->expectsArgument()) { if ($last && !$opt->argument_optional) { throw Console_CommandLine_Exception::build('OPTION_VALUE_REQUIRED', array('name' => $opt->name), $this); } // we will have a value next time $lastopt = $opt; return; } $value = false; } else { if (!$opt->expectsArgument()) { if ($nextopt = $this->findOption('-' . $next)) { $this->_dispatchAction($opt, false, $result); $this->parseToken('-' . substr($token, 2), $result, $args, $last); return; } else { throw Console_CommandLine_Exception::build('OPTION_UNKNOWN', array('name' => $next), $this); } } if ($opt->action == 'StoreArray') { $lastopt = $opt; } $value = substr($token, 2); } $this->_dispatchAction($opt, $value, $result); } else { // We have an argument. // if we are in POSIX compliant mode, we must set the stop flag to // true in order to stop option parsing. if (!$stopflag && $this->force_posix) { $stopflag = true; } $args[] = $token; } } }