コード例 #1
0
 /**
  * Executes 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
  * @throws Console_CommandLine_Exception
  */
 public function execute($value = false, $params = array())
 {
     if (!is_numeric($value)) {
         throw Console_CommandLine_Exception::factory('OPTION_VALUE_TYPE_ERROR', array('name' => $this->option->name, 'type' => 'float', 'value' => $value), $this->parser);
     }
     $this->setResult((double) $value);
 }
コード例 #2
0
ファイル: StoreInt.php プロジェクト: hnw/php425
 /**
  * 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);
 }
コード例 #3
0
 /**
  * Parses the command line token and modifies *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;
     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::factory('OPTION_VALUE_REQUIRED', array('name' => $lastopt->name), $this, $this->messages);
                 }
             }
         } 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)) {
                 if (!is_null($token)) {
                     $args[] = $token;
                 }
                 return;
             }
             if (!is_null($token) || $lastopt->action == 'Password') {
                 $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::factory('OPTION_UNKNOWN', array('name' => $optkv[0]), $this, $this->messages);
         }
         $value = isset($optkv[1]) ? $optkv[1] : false;
         if (!$opt->expectsArgument() && $value !== false) {
             throw Console_CommandLine_Exception::factory('OPTION_VALUE_UNEXPECTED', array('name' => $opt->name, 'value' => $value), $this, $this->messages);
         }
         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::factory('OPTION_VALUE_REQUIRED', array('name' => $opt->name), $this, $this->messages);
             }
             // 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 "-": try to read stdin
                 $args[] = file_get_contents('php://stdin');
                 return;
             }
             $opt = $this->findOption($optname);
             if (!$opt) {
                 throw Console_CommandLine_Exception::factory('OPTION_UNKNOWN', array('name' => $optname), $this, $this->messages);
             }
             // 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::factory('OPTION_VALUE_REQUIRED', array('name' => $opt->name), $this, $this->messages);
                     }
                     // 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::factory('OPTION_UNKNOWN', array('name' => $next), $this, $this->messages);
                     }
                 }
                 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;
             }
             if (!is_null($token)) {
                 $args[] = $token;
             }
         }
     }
 }
コード例 #4
0
ファイル: CommandLine.php プロジェクト: peopleplan/Pyrus
 /**
  * Parses the command line arguments and returns a
  * PEAR2\Console\CommandLine\Result instance.
  *
  * @param integer $userArgc Number of arguments (optional)
  * @param array   $userArgv Array containing arguments (optional)
  *
  * @return PEAR2\Console\CommandLine\Result The result instance
  * @throws Exception on user errors
  */
 public function parse($userArgc = null, $userArgv = null)
 {
     $this->addBuiltinOptions();
     if ($userArgc !== null && $userArgv !== null) {
         $argc = $userArgc;
         $argv = $userArgv;
     } else {
         list($argc, $argv) = $this->getArgcArgv();
     }
     // build an empty result
     $result = new CommandLine\Result();
     if (!$this instanceof CommandLine\Command) {
         // remove script name if we're not in a subcommand
         array_shift($argv);
         $argc--;
     }
     // will contain arguments
     $args = array();
     foreach ($this->options as $name => $option) {
         $result->options[$name] = $option->default;
     }
     // parse command line tokens
     while ($argc--) {
         $token = array_shift($argv);
         try {
             if ($cmd = $this->_getSubCommand($token)) {
                 $result->command_name = $cmd->name;
                 $result->command = $cmd->parse($argc, $argv);
                 break;
             } else {
                 $this->parseToken($token, $result, $args, $argc);
             }
         } catch (Exception $exc) {
             throw $exc;
         }
     }
     // Parse a null token to allow any undespatched actions to be despatched.
     $this->parseToken(null, $result, $args, 0);
     // Check if an invalid subcommand was specified. If there are
     // subcommands and no arguments, but an argument was provided, it is
     // an invalid subcommand.
     if (count($this->commands) > 0 && count($this->args) === 0 && count($args) > 0) {
         throw CommandLine\Exception::factory('INVALID_SUBCOMMAND', array('command' => $args[0]), $this, $this->messages);
     }
     // if subcommand_required is set to true we must check that we have a
     // subcommand.
     if (count($this->commands) && $this->subcommand_required && !$result->command_name) {
         throw Console_CommandLine_Exception::factory('SUBCOMMAND_REQUIRED', array('commands' => implode(array_keys($this->commands), ', ')), $this, $this->messages);
     }
     // minimum argument number check
     $argnum = 0;
     foreach ($this->args as $name => $arg) {
         if (!$arg->optional) {
             $argnum++;
         }
     }
     if (count($args) < $argnum) {
         throw CommandLine\Exception::factory('ARGUMENT_REQUIRED', array('argnum' => $argnum, 'plural' => $argnum > 1 ? 's' : ''), $this, $this->messages);
     }
     // handle arguments
     $c = count($this->args);
     foreach ($this->args as $name => $arg) {
         $c--;
         if ($arg->multiple) {
             $result->args[$name] = $c ? array_splice($args, 0, -$c) : $args;
         } else {
             $result->args[$name] = array_shift($args);
         }
         if (!$result->args[$name] && $arg->optional && $arg->default) {
             $result->args[$name] = $arg->default;
         }
     }
     // dispatch deferred options
     foreach ($this->_dispatchLater as $optArray) {
         $optArray[0]->dispatchAction($optArray[1], $optArray[2], $this);
     }
     return $result;
 }
コード例 #5
0
ファイル: Option.php プロジェクト: pear/console_commandline
 /**
  * Formats the value $value according to the action of the option and 
  * updates the passed Console_CommandLine_Result object.
  *
  * @param mixed                      $value  The value to format
  * @param Console_CommandLine_Result $result The result instance
  * @param Console_CommandLine        $parser The parser instance
  *
  * @return void
  * @throws Console_CommandLine_Exception
  */
 public function dispatchAction($value, $result, $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];
     if ($this->_action_instance === null) {
         $this->_action_instance = new $clsname($result, $this, $parser);
     }
     // check value is in option choices
     if (!empty($this->choices) && !in_array($this->_action_instance->format($value), $this->choices)) {
         throw Console_CommandLine_Exception::factory('OPTION_VALUE_NOT_VALID', array('name' => $this->name, 'choices' => implode('", "', $this->choices), 'value' => $value), $parser, $this->messages);
     }
     $this->_action_instance->execute($value, $this->action_params);
 }
コード例 #6
0
ファイル: phar-extract.php プロジェクト: richarrj/phar-util
 */
// Include the Console_CommandLine package.
require_once 'Console/CommandLine.php';
// create the parser
$parser = new Console_CommandLine(array('description' => 'Extract contents of a phar archive to a given directory', 'version' => '@package_version@', 'name' => 'phar-extract'));
$parser->addOption('public', array('short_name' => '-P', 'long_name' => '--public', 'action' => 'StoreString', 'description' => "Public key file (PEM) to verify signature.\nIf not given, <pharfilename.phar>.pubkey will be used."));
$parser->addOption('list', array('short_name' => '-l', 'long_name' => '--list', 'action' => 'StoreTrue', 'description' => "Only list the files, don't extract them."));
$parser->addArgument('phar', array('action' => 'StoreString', 'description' => "Input Phar archive filename e.g. phar.phar"));
$parser->addArgument('destination', array('action' => 'StoreString', 'description' => "Destination directory", 'optional' => true));
// run the parser
try {
    $result = $parser->parse();
    $options = $result->options;
    $args = $result->args;
    if ($options['list'] !== true && !isset($args['destination'])) {
        throw Console_CommandLine_Exception::factory('ARGUMENT_REQUIRED', array('argnum' => 2, 'plural' => 's'), $parser, $parser->messages);
    }
} catch (Exception $exc) {
    $parser->displayError($exc->getMessage());
}
echo $parser->name . ' ' . $parser->version . PHP_EOL . PHP_EOL;
// validate parameters
if (substr($args['phar'], -5) !== '.phar') {
    $parser->displayError("Input Phar must have .phar extension, {$args['phar']} given.", 2);
}
if (!file_exists($args['phar']) || !is_readable($args['phar'])) {
    $parser->displayError("Phar in '{$args['phar']}' does not exist or is not readable.", 4);
}
if ($options['public']) {
    if (!file_exists($options['public']) || !is_readable($options['public'])) {
        $parser->displayError("Public key in '{$options['public']}' does not exist or is not readable.", 4);