Пример #1
0
 /**
  * Check value with default values
  *
  * @param  mixed &$value value to check with
  * @return bool
  * @throws Exception\InvalidArgumentException
  *         if value is required but missing
  * @access protected
  */
 protected function checkDefault(&$value)
 {
     // no value needed
     if (!$this->needValue()) {
         return true;
     }
     // value is required but empty provided
     if ($this->isValueRequired() && $value === '') {
         throw new Exception\InvalidArgumentException(Message::get(Message::GETOPT_VALUE_REQUIRED, $this->name), Message::GETOPT_VALUE_REQUIRED);
     }
     // no defaults
     if (is_null($this->rules['default'])) {
         return true;
     }
     // has defaults
     $default = $this->rules['default'];
     // enum defaults
     if (is_array($default)) {
         // set value to first default
         if ($value === '') {
             $value = $default[0];
             return true;
             // value is in defaults
         } else {
             if (in_array($value, $default)) {
                 return true;
                 // value is not right
             } else {
                 return false;
             }
         }
         // non enum default
     } else {
         // set value to default
         if ($value === '') {
             $value = $default;
         }
     }
     return true;
 }
Пример #2
0
 /**
  * Parse one argument at a time
  *
  * @param  string $arg specific argument
  * @param  array &$arguments all command line arguments
  * @return bool
  * @access protected
  */
 protected function parseOne($arg, array &$arguments)
 {
     // no value
     $value = false;
     // value serpator
     $vse = $this->configs['valueSeperator'];
     // long option
     if ($arg[1] == '-') {
         $short = false;
         $rem = substr($arg, 2);
         // --user=phossa
         if (strpos($rem, '=') !== false) {
             list($o, $value) = explode('=', $rem);
             $opts = [$o];
             // --user phossa
         } else {
             $opts = [$rem];
         }
         // short option(s) '-x' OR '-xyz'
     } else {
         $short = true;
         $opts = str_split(substr($arg, 1), 1);
     }
     // loop thru options
     foreach ($opts as $j => $opt) {
         // unknown option
         if (!isset($this->options[$opt])) {
             $this->error = Message::get(Message::GETOPT_OPTION_UNKNOWN, $opt);
             return false;
         }
         $o = $this->options[$opt];
         // --user=phossa case
         if ($value) {
             return $o->setValue($value, $vse);
         }
         // no value needed, set $o value to empty ''
         if (!$o->needValue()) {
             $o->setValue();
             continue;
         }
         // value needed but not required
         if (!$o->isValueRequired()) {
             // short option, remaining is the value
             if ($short && $j < sizeof($opts) - 1) {
                 $value = join('', array_slice($opts, $j + 1));
                 return $o->setValue($value, $vse);
                 // look at the next argument
             } else {
                 // next argument (value) not found
                 if (!(list(, $value) = each($arguments))) {
                     return $o->setValue();
                 }
                 // next argument is another option, go back
                 if ($value[0] == '-') {
                     current($arguments) ? prev($arguments) : end($arguments);
                     return $o->setValue();
                 }
                 // next argument is a value
                 return $o->setValue($value, $vse);
             }
         }
         // value is required
         if (!$short || $j == sizeof($opts) - 1) {
             // value not found
             if (!(list(, $value) = each($arguments))) {
                 $this->error = Message::get(Message::GETOPT_OPTION_MISSING, $opt);
                 return false;
             }
             // next arg is an option
             if ($value[0] == '-') {
                 $this->error = Message::get(Message::GETOPT_OPTION_MISSING, $opt);
                 return false;
             }
             return $o->setValue($value, $vse);
         } else {
             // short option
             $value = join('', array_slice($opts, $j + 1));
             return $o->setValue($value, $vse);
         }
     }
     return true;
 }