create option result from array() OptionResult::create($spec, array( 'key' => 'value' ), array( ... arguments ... ) );
Inheritance: implements IteratorAggregat\IteratorAggregate, implements ArrayAcces\ArrayAccess
示例#1
0
 /**
  * @param Logger       $logger
  * @param OptionResult $options
  * @param string       $downloader
  *
  * @return BaseDownloader
  */
 public static function getInstance(Logger $logger, OptionResult $options, $downloader = null)
 {
     if (is_string($downloader)) {
         //if we specific a downloader class clearly, then it's the only choice
         if (class_exists($downloader) && is_subclass_of($downloader, 'PhpBrew\\Downloader\\BaseDownloader')) {
             return new $downloader($logger, $options);
         }
         $downloader = array($downloader);
     }
     if (empty($downloader)) {
         $downloader = array_keys(self::$availableDownloaders);
     }
     //if --downloader presents, we will use it as the first choice, even if the caller specific downloader by alias/array
     if ($options->has('downloader')) {
         $logger->info("Found --downloader option, try to use {$options->downloader} as default downloader.");
         $downloader = array_merge(array($options->downloader), $downloader);
     }
     $instance = self::create($logger, $options, $downloader);
     if ($instance === null) {
         $logger->debug('Downloader not found, falling back to command-based downloader.');
         //if all downloader not available, maybe we should throw exceptions here instead of returning null?
         return self::create($logger, $options, self::$fallbackDownloaders);
     } else {
         return $instance;
     }
 }
示例#2
0
 public function setParsedOptions($parsedOptions)
 {
     $this->parsedOptions = $parsedOptions;
     //early detect if -h is given, display help and finish processing
     if ($this->parsedOptions->has('help')) {
         $this->printOptions();
         die;
     }
 }
 public function testRemoveColumn()
 {
     $schema = new AuthorSchema();
     $schema->column('cellphone')->varchar(30);
     $this->buildSchemaTables([$schema], true);
     AutomaticMigration::options($options = new OptionCollection());
     $migrate = new AutomaticMigration($this->conn, $this->queryDriver, $this->logger, OptionResult::create($options, []));
     $migrate->upgrade([$schema]);
 }
 function testOption()
 {
     $option = new \GetOptionKit\OptionResult();
     ok($option);
     $specs = new \GetOptionKit\OptionCollection();
     $specs->add('name:', 'name');
     $result = \GetOptionKit\OptionResult::create($specs, array('name' => 'c9s'), array('arg1'));
     ok($result);
     ok($result->arguments);
     ok($result->name);
     is('c9s', $result->name);
     is($result->arguments[0], 'arg1');
 }
示例#5
0
 /**
  * @param array $argv
  *
  * @return OptionResult|Option[]
  *
  * @throws Exception\RequireValueException
  * @throws Exception\InvalidOptionException
  * @throws \Exception
  */
 public function parse(array $argv)
 {
     $result = new OptionResult();
     list($argv, $extra) = $this->preprocessingArguments($argv);
     foreach ($this->specs as $opt) {
         if ($opt->defaultValue !== null) {
             $opt->setValue($opt->defaultValue);
             $result->set($opt->getId(), $opt);
         }
     }
     $len = count($argv);
     // some people might still pass only the option names here.
     $first = new Argument($argv[0]);
     if ($first->isOption()) {
         throw new Exception('parse(argv) expects the first argument to be the program name.');
     }
     for ($i = 1; $i < $len; ++$i) {
         $arg = new Argument($argv[$i]);
         // if looks like not an option, push it to argument list.
         // TODO: we might want to support argument with preceding dash (?)
         if (!$arg->isOption()) {
             $result->addArgument($arg);
             continue;
         }
         // if the option is with extra flags,
         //   split the string, and insert into the argv array
         if ($arg->withExtraFlagOptions()) {
             $extra = $arg->extractExtraFlagOptions();
             array_splice($argv, $i + 1, 0, $extra);
             $argv[$i] = $arg->arg;
             // update argument to current argv list.
             $len = count($argv);
             // update argv list length
         }
         $next = null;
         if ($i + 1 < count($argv)) {
             $next = new Argument($argv[$i + 1]);
         }
         $spec = $this->specs->get($arg->getOptionName());
         if (!$spec) {
             throw new InvalidOptionException('Invalid option: ' . $arg);
         }
         // This if expr might be unnecessary, becase we have default mode - flag
         // if ($spec->isRequired() || $spec->isMultiple() || $spec->isOptional() || $spec->isFlag()) {
         $i += $this->consumeOptionToken($spec, $arg, $next);
         $result->set($spec->getId(), $spec);
     }
     return $result;
 }
 function parse($argv)
 {
     // create new Result object.
     $result = new OptionResult();
     if (!$this->argv) {
         $this->argv = $argv;
         $this->length = count($argv);
     }
     if ($this->isEnd()) {
         return $result;
     }
     // from last parse index
     for (; $this->index < $this->length; ++$this->index) {
         $arg = new Argument($argv[$this->index]);
         /* let the application decide for: command or arguments */
         if (!$arg->isOption()) {
             # echo "stop at {$this->index}\n";
             return $result;
         }
         // if the option is with extra flags,
         //   split it out, and insert into the argv array
         //
         //   like -abc
         if ($arg->withExtraFlagOptions()) {
             $extra = $arg->extractExtraFlagOptions();
             array_splice($argv, $this->index + 1, 0, $extra);
             $argv[$this->index] = $arg->arg;
             // update argument to current argv list.
             $len = count($argv);
             // update argv list length
         }
         $next = null;
         if ($this->index + 1 < count($argv)) {
             $next = new Argument($argv[$this->index + 1]);
         }
         $spec = $this->specs->getSpec($arg->getOptionName());
         if (!$spec) {
             throw new InvalidOptionException("Invalid option: " . $arg);
         }
         if ($spec->isAttributeRequire()) {
             if (!$this->foundRequireValue($spec, $arg, $next)) {
                 throw new RequireValueException("Option '{$arg->getOptionName()}' requires a value.");
             }
             $this->takeOptionValue($spec, $arg, $next);
             if ($next && !$next->isOption()) {
                 $this->index++;
             }
             $result->set($spec->getId(), $spec);
         } elseif ($spec->isAttributeMultiple()) {
             $this->pushOptionValue($spec, $arg, $next);
             if ($next && !$next->isOption()) {
                 $this->index++;
             }
             $result->set($spec->getId(), $spec);
         } elseif ($spec->isAttributeOptional()) {
             $this->takeOptionValue($spec, $arg, $next);
             if ($spec->value && $next && !$next->isOption()) {
                 $this->index++;
             }
             $result->set($spec->getId(), $spec);
         } elseif ($spec->isAttributeFlag()) {
             $spec->value = true;
             $result->set($spec->getId(), $spec);
         } else {
             throw new Exception('Unknown attribute.');
         }
     }
     return $result;
 }
示例#7
0
 public function getDependencies()
 {
     $depGenerators = array();
     $loader = new FlavorLoader();
     $logger = $this->getLogger();
     $deps = $this->dependency();
     foreach ($deps as $depName => $options) {
         /* swap for short dependency name */
         if (is_integer($depName)) {
             $depName = $options;
             $options = array();
         }
         $depFlavor = $loader->load($depName);
         if (!$depFlavor->exists()) {
             throw new Exception("Dependency flavor {$depName} not found.");
         }
         $depGenerator = $depFlavor->getGenerator();
         $depGenerator->setLogger($logger);
         $depSpecs = new OptionSpecCollection();
         $depGenerator->options($depSpecs);
         $depOptionResult = OptionResult::create($depSpecs, @$options['options'] ?: array(), @$options['arguments'] ?: array());
         $depGenerator->setOption($depOptionResult);
         $depGenerators[] = $depGenerator;
     }
     return $depGenerators;
 }
 public function parse(array $argv)
 {
     // create new Result object.
     $result = new OptionResult();
     list($this->argv, $extra) = $this->preprocessingArguments($argv);
     $this->length = count($this->argv);
     // register option result from options with default value
     foreach ($this->specs as $opt) {
         if ($opt->defaultValue !== null) {
             $opt->setValue($opt->defaultValue);
             $result->set($opt->getId(), $opt);
         }
     }
     // from last parse index
     for (; $this->index < $this->length; ++$this->index) {
         $arg = new Argument($this->argv[$this->index]);
         /* let the application decide for: command or arguments */
         if (!$arg->isOption()) {
             # echo "stop at {$this->index}\n";
             return $result;
         }
         // if the option is with extra flags,
         //   split it out, and insert into the argv array
         //
         //   like -abc
         if ($arg->withExtraFlagOptions()) {
             $extra = $arg->extractExtraFlagOptions();
             array_splice($this->argv, $this->index + 1, 0, $extra);
             $this->argv[$this->index] = $arg->arg;
             // update argument to current argv list.
             $this->length = count($this->argv);
             // update argv list length
         }
         $next = null;
         if ($this->index + 1 < count($this->argv)) {
             $next = new Argument($this->argv[$this->index + 1]);
         }
         $spec = $this->specs->get($arg->getOptionName());
         if (!$spec) {
             throw new InvalidOptionException('Invalid option: ' . $arg);
         }
         // This if block is unnecessary
         // if ($spec->isRequired() || $spec->isMultiple() || $spec->isOptional() || $spec->isFlag()) {
         $this->index += $this->consumeOptionToken($spec, $arg, $next);
         $result->set($spec->getId(), $spec);
     }
     return $result;
 }
示例#9
0
 /**
  * @param array $argv
  * @return OptionResult|Option[]
  * @throws Exception\RequireValueException
  * @throws Exception\InvalidOptionException
  * @throws \Exception
  */
 public function parse(array $argv)
 {
     $result = new OptionResult();
     $argv = $this->preprocessingArguments($argv);
     foreach ($this->specs as $spec) {
         if ($spec->defaultValue !== null) {
             $result->set($spec->getId(), $spec);
         }
     }
     $len = count($argv);
     for ($i = 0; $i < $len; ++$i) {
         $arg = new Argument($argv[$i]);
         // if looks like not an option, push it to argument list.
         // TODO: we might want to support argument with preceding dash (?)
         if (!$arg->isOption()) {
             $result->addArgument($arg);
             continue;
         }
         // if the option is with extra flags,
         //   split the string, and insert into the argv array
         if ($arg->withExtraFlagOptions()) {
             $extra = $arg->extractExtraFlagOptions();
             array_splice($argv, $i + 1, 0, $extra);
             $argv[$i] = $arg->arg;
             // update argument to current argv list.
             $len = count($argv);
             // update argv list length
         }
         $next = null;
         if ($i + 1 < count($argv)) {
             $next = new Argument($argv[$i + 1]);
         }
         $spec = $this->specs->get($arg->getOptionName());
         if (!$spec) {
             throw new InvalidOptionException("Invalid option: " . $arg);
         }
         if ($spec->isRequired()) {
             if (!$this->foundRequireValue($spec, $arg, $next)) {
                 throw new RequireValueException("Option {$arg->getOptionName()} requires a value. given '{$next}'");
             }
             $this->takeOptionValue($spec, $arg, $next);
             if ($next && !$next->anyOfOptions($this->specs)) {
                 $i++;
             }
             $result->set($spec->getId(), $spec);
         } elseif ($spec->isMultiple()) {
             $this->pushOptionValue($spec, $arg, $next);
             if ($next && $next->isOption()) {
                 $i++;
             }
             $result->set($spec->getId(), $spec);
         } elseif ($spec->isOptional()) {
             $this->takeOptionValue($spec, $arg, $next);
             if (($spec->value || $spec->defaultValue) && $next && !$next->isOption()) {
                 $i++;
             }
             $result->set($spec->getId(), $spec);
         } elseif ($spec->isIncremental()) {
             $spec->increaseValue();
             $result->set($spec->getId(), $spec);
         } elseif ($spec->isFlag()) {
             $spec->setValue(true);
             $result->set($spec->getId(), $spec);
         } else {
             throw new Exception('Unknown attribute.');
         }
     }
     return $result;
 }
示例#10
0
 function parse($argv)
 {
     $result = new OptionResult();
     $len = count($argv);
     for ($i = 0; $i < $len; ++$i) {
         $arg = new Argument($argv[$i]);
         if (!$arg->isOption()) {
             $result->addArgument($arg);
             continue;
         }
         // if the option is with extra flags,
         //   split it out, and insert into the argv array
         if ($arg->withExtraFlagOptions()) {
             $extra = $arg->extractExtraFlagOptions();
             array_splice($argv, $i + 1, 0, $extra);
             $argv[$i] = $arg->arg;
             // update argument to current argv list.
             $len = count($argv);
             // update argv list length
         }
         $next = new Argument(@$argv[$i + 1]);
         $spec = $this->specs->getSpec($arg->getOptionName());
         if (!$spec) {
             throw new InvalidOptionException("Invalid option: " . $arg);
         }
         if ($spec->isAttributeRequire()) {
             if (!$this->foundRequireValue($spec, $arg, $next)) {
                 throw new RequireValueException("Option {$arg->getOptionName()} require a value.");
             }
             $this->takeOptionValue($spec, $arg, $next);
             if (!$next->isOption()) {
                 $i++;
             }
             $result->set($spec->getId(), $spec);
         } elseif ($spec->isAttributeMultiple()) {
             $this->pushOptionValue($spec, $arg, $next);
             if ($next->isOption()) {
                 $i++;
             }
             $result->set($spec->getId(), $spec);
         } elseif ($spec->isAttributeOptional()) {
             $this->takeOptionValue($spec, $arg, $next);
             if ($spec->value && !$next->isOption()) {
                 $i++;
             }
             $result->set($spec->getId(), $spec);
         } elseif ($spec->isAttributeFlag()) {
             $spec->value = true;
             $result->set($spec->getId(), $spec);
         } else {
             throw new Exception('Unknown attribute.');
         }
     }
     return $result;
 }