Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Getopt.php
Go to the documentation of this file.
1 <?php
128 {
129 
135  const MODE_ZEND = 'zend';
136  const MODE_GNU = 'gnu';
137 
142  const PARAM_REQUIRED = '=';
143  const PARAM_OPTIONAL = '-';
144  const TYPE_STRING = 's';
145  const TYPE_WORD = 'w';
146  const TYPE_INTEGER = 'i';
147 
156  const CONFIG_RULEMODE = 'ruleMode';
157  const CONFIG_DASHDASH = 'dashDash';
158  const CONFIG_IGNORECASE = 'ignoreCase';
159  const CONFIG_PARSEALL = 'parseAll';
160 
168  protected $_getoptConfig = array(
169  self::CONFIG_RULEMODE => self::MODE_ZEND,
170  self::CONFIG_DASHDASH => true,
171  self::CONFIG_IGNORECASE => false,
172  self::CONFIG_PARSEALL => true,
173  );
174 
180  protected $_argv = array();
181 
187  protected $_progname = '';
188 
194  protected $_rules = array();
195 
201  protected $_ruleMap = array();
202 
209  protected $_options = array();
210 
216  protected $_remainingArgs = array();
217 
223  protected $_parsed = false;
224 
242  public function __construct($rules, $argv = null, $getoptConfig = array())
243  {
244  if (!isset($_SERVER['argv'])) {
245  #require_once 'Zend/Console/Getopt/Exception.php';
246  if (ini_get('register_argc_argv') == false) {
248  "argv is not available, because ini option 'register_argc_argv' is set Off"
249  );
250  } else {
252  '$_SERVER["argv"] is not set, but Zend_Console_Getopt cannot work without this information.'
253  );
254  }
255  }
256 
257  $this->_progname = $_SERVER['argv'][0];
258  $this->setOptions($getoptConfig);
259  $this->addRules($rules);
260  if (!is_array($argv)) {
261  $argv = array_slice($_SERVER['argv'], 1);
262  }
263  if (isset($argv)) {
264  $this->addArguments((array)$argv);
265  }
266  }
267 
280  public function __get($key)
281  {
282  return $this->getOption($key);
283  }
284 
291  public function __isset($key)
292  {
293  $this->parse();
294  if (isset($this->_ruleMap[$key])) {
295  $key = $this->_ruleMap[$key];
296  return isset($this->_options[$key]);
297  }
298  return false;
299  }
300 
308  public function __set($key, $value)
309  {
310  $this->parse();
311  if (isset($this->_ruleMap[$key])) {
312  $key = $this->_ruleMap[$key];
313  $this->_options[$key] = $value;
314  }
315  }
316 
322  public function __toString()
323  {
324  return $this->toString();
325  }
326 
333  public function __unset($key)
334  {
335  $this->parse();
336  if (isset($this->_ruleMap[$key])) {
337  $key = $this->_ruleMap[$key];
338  unset($this->_options[$key]);
339  }
340  }
341 
350  public function addArguments($argv)
351  {
352  if(!is_array($argv)) {
353  #require_once 'Zend/Console/Getopt/Exception.php';
355  "Parameter #1 to addArguments should be an array");
356  }
357  $this->_argv = array_merge($this->_argv, $argv);
358  $this->_parsed = false;
359  return $this;
360  }
361 
370  public function setArguments($argv)
371  {
372  if(!is_array($argv)) {
373  #require_once 'Zend/Console/Getopt/Exception.php';
375  "Parameter #1 to setArguments should be an array");
376  }
377  $this->_argv = $argv;
378  $this->_parsed = false;
379  return $this;
380  }
381 
390  public function setOptions($getoptConfig)
391  {
392  if (isset($getoptConfig)) {
393  foreach ($getoptConfig as $key => $value) {
394  $this->setOption($key, $value);
395  }
396  }
397  return $this;
398  }
399 
409  public function setOption($configKey, $configValue)
410  {
411  if ($configKey !== null) {
412  $this->_getoptConfig[$configKey] = $configValue;
413  }
414  return $this;
415  }
416 
424  public function addRules($rules)
425  {
426  $ruleMode = $this->_getoptConfig['ruleMode'];
427  switch ($this->_getoptConfig['ruleMode']) {
428  case self::MODE_ZEND:
429  if (is_array($rules)) {
430  $this->_addRulesModeZend($rules);
431  break;
432  }
433  // intentional fallthrough
434  case self::MODE_GNU:
435  $this->_addRulesModeGnu($rules);
436  break;
437  default:
443  $method = '_addRulesMode' . ucfirst($ruleMode);
444  $this->$method($rules);
445  }
446  $this->_parsed = false;
447  return $this;
448  }
449 
455  public function toString()
456  {
457  $this->parse();
458  $s = array();
459  foreach ($this->_options as $flag => $value) {
460  $s[] = $flag . '=' . ($value === true ? 'true' : $value);
461  }
462  return implode(' ', $s);
463  }
464 
474  public function toArray()
475  {
476  $this->parse();
477  $s = array();
478  foreach ($this->_options as $flag => $value) {
479  $s[] = $flag;
480  if ($value !== true) {
481  $s[] = $value;
482  }
483  }
484  return $s;
485  }
486 
492  public function toJson()
493  {
494  $this->parse();
495  $j = array();
496  foreach ($this->_options as $flag => $value) {
497  $j['options'][] = array(
498  'option' => array(
499  'flag' => $flag,
500  'parameter' => $value
501  )
502  );
503  }
504 
508  #require_once 'Zend/Json.php';
509  $json = Zend_Json::encode($j);
510 
511  return $json;
512  }
513 
519  public function toXml()
520  {
521  $this->parse();
522  $doc = new DomDocument('1.0', 'utf-8');
523  $optionsNode = $doc->createElement('options');
524  $doc->appendChild($optionsNode);
525  foreach ($this->_options as $flag => $value) {
526  $optionNode = $doc->createElement('option');
527  $optionNode->setAttribute('flag', utf8_encode($flag));
528  if ($value !== true) {
529  $optionNode->setAttribute('parameter', utf8_encode($value));
530  }
531  $optionsNode->appendChild($optionNode);
532  }
533  $xml = $doc->saveXML();
534  return $xml;
535  }
536 
542  public function getOptions()
543  {
544  $this->parse();
545  return array_keys($this->_options);
546  }
547 
558  public function getOption($flag)
559  {
560  $this->parse();
561  if ($this->_getoptConfig[self::CONFIG_IGNORECASE]) {
562  $flag = strtolower($flag);
563  }
564  if (isset($this->_ruleMap[$flag])) {
565  $flag = $this->_ruleMap[$flag];
566  if (isset($this->_options[$flag])) {
567  return $this->_options[$flag];
568  }
569  }
570  return null;
571  }
572 
578  public function getRemainingArgs()
579  {
580  $this->parse();
581  return $this->_remainingArgs;
582  }
583 
593  public function getUsageMessage()
594  {
595  $usage = "Usage: {$this->_progname} [ options ]\n";
596  $maxLen = 20;
597  $lines = array();
598  foreach ($this->_rules as $rule) {
599  $flags = array();
600  if (is_array($rule['alias'])) {
601  foreach ($rule['alias'] as $flag) {
602  $flags[] = (strlen($flag) == 1 ? '-' : '--') . $flag;
603  }
604  }
605  $linepart['name'] = implode('|', $flags);
606  if (isset($rule['param']) && $rule['param'] != 'none') {
607  $linepart['name'] .= ' ';
608  switch ($rule['param']) {
609  case 'optional':
610  $linepart['name'] .= "[ <{$rule['paramType']}> ]";
611  break;
612  case 'required':
613  $linepart['name'] .= "<{$rule['paramType']}>";
614  break;
615  }
616  }
617  if (strlen($linepart['name']) > $maxLen) {
618  $maxLen = strlen($linepart['name']);
619  }
620  $linepart['help'] = '';
621  if (isset($rule['help'])) {
622  $linepart['help'] .= $rule['help'];
623  }
624  $lines[] = $linepart;
625  }
626  foreach ($lines as $linepart) {
627  $usage .= sprintf("%s %s\n",
628  str_pad($linepart['name'], $maxLen),
629  $linepart['help']);
630  }
631  return $usage;
632  }
633 
644  public function setAliases($aliasMap)
645  {
646  foreach ($aliasMap as $flag => $alias)
647  {
648  if ($this->_getoptConfig[self::CONFIG_IGNORECASE]) {
649  $flag = strtolower($flag);
650  $alias = strtolower($alias);
651  }
652  if (!isset($this->_ruleMap[$flag])) {
653  continue;
654  }
655  $flag = $this->_ruleMap[$flag];
656  if (isset($this->_rules[$alias]) || isset($this->_ruleMap[$alias])) {
657  $o = (strlen($alias) == 1 ? '-' : '--') . $alias;
658  #require_once 'Zend/Console/Getopt/Exception.php';
660  "Option \"$o\" is being defined more than once.");
661  }
662  $this->_rules[$flag]['alias'][] = $alias;
663  $this->_ruleMap[$alias] = $flag;
664  }
665  return $this;
666  }
667 
677  public function setHelp($helpMap)
678  {
679  foreach ($helpMap as $flag => $help)
680  {
681  if (!isset($this->_ruleMap[$flag])) {
682  continue;
683  }
684  $flag = $this->_ruleMap[$flag];
685  $this->_rules[$flag]['help'] = $help;
686  }
687  return $this;
688  }
689 
699  public function parse()
700  {
701  if ($this->_parsed === true) {
702  return;
703  }
704  $argv = $this->_argv;
705  $this->_options = array();
706  $this->_remainingArgs = array();
707  while (count($argv) > 0) {
708  if ($argv[0] == '--') {
709  array_shift($argv);
710  if ($this->_getoptConfig[self::CONFIG_DASHDASH]) {
711  $this->_remainingArgs = array_merge($this->_remainingArgs, $argv);
712  break;
713  }
714  }
715  if (substr($argv[0], 0, 2) == '--') {
716  $this->_parseLongOption($argv);
717  } else if (substr($argv[0], 0, 1) == '-' && ('-' != $argv[0] || count($argv) >1)) {
718  $this->_parseShortOptionCluster($argv);
719  } else if($this->_getoptConfig[self::CONFIG_PARSEALL]) {
720  $this->_remainingArgs[] = array_shift($argv);
721  } else {
722  /*
723  * We should put all other arguments in _remainingArgs and stop parsing
724  * since CONFIG_PARSEALL is false.
725  */
726  $this->_remainingArgs = array_merge($this->_remainingArgs, $argv);
727  break;
728  }
729  }
730  $this->_parsed = true;
731  return $this;
732  }
733 
737  public function checkRequiredArguments()
738  {
739  foreach ($this->_rules as $name => $rule) {
740  if ($rule['param'] === 'required') {
741  $defined = false;
742  foreach ($rule['alias'] as $alias) {
743  $defined = $defined === true ? true : array_key_exists($alias, $this->_options);
744  }
745  if ($defined === false) {
746  #require_once 'Zend/Console/Getopt/Exception.php';
748  'Option "$alias" requires a parameter.',
749  $this->getUsageMessage()
750  );
751  }
752  }
753  }
754  }
755 
764  protected function _parseLongOption(&$argv)
765  {
766  $optionWithParam = ltrim(array_shift($argv), '-');
767  $l = explode('=', $optionWithParam, 2);
768  $flag = array_shift($l);
769  $param = array_shift($l);
770  if (isset($param)) {
771  array_unshift($argv, $param);
772  }
773  $this->_parseSingleOption($flag, $argv);
774  }
775 
784  protected function _parseShortOptionCluster(&$argv)
785  {
786  $flagCluster = ltrim(array_shift($argv), '-');
787  foreach (str_split($flagCluster) as $flag) {
788  $this->_parseSingleOption($flag, $argv);
789  }
790  }
791 
800  protected function _parseSingleOption($flag, &$argv)
801  {
802  if ($this->_getoptConfig[self::CONFIG_IGNORECASE]) {
803  $flag = strtolower($flag);
804  }
805  if (!isset($this->_ruleMap[$flag])) {
806  #require_once 'Zend/Console/Getopt/Exception.php';
808  "Option \"$flag\" is not recognized.",
809  $this->getUsageMessage());
810  }
811  $realFlag = $this->_ruleMap[$flag];
812  switch ($this->_rules[$realFlag]['param']) {
813  case 'required':
814  if (count($argv) > 0 && substr($argv[0], 0, 1) != '-') {
815  $param = array_shift($argv);
816  $this->_checkParameterType($realFlag, $param);
817  } else {
818  #require_once 'Zend/Console/Getopt/Exception.php';
820  "Option \"$flag\" requires a parameter.",
821  $this->getUsageMessage());
822  }
823  break;
824  case 'optional':
825  if (count($argv) > 0 && substr($argv[0], 0, 1) != '-') {
826  $param = array_shift($argv);
827  $this->_checkParameterType($realFlag, $param);
828  } else {
829  $param = true;
830  }
831  break;
832  default:
833  $param = true;
834  }
835  $this->_options[$realFlag] = $param;
836  }
837 
848  protected function _checkParameterType($flag, $param)
849  {
850  $type = 'string';
851  if (isset($this->_rules[$flag]['paramType'])) {
852  $type = $this->_rules[$flag]['paramType'];
853  }
854  switch ($type) {
855  case 'word':
856  if (preg_match('/\W/', $param)) {
857  #require_once 'Zend/Console/Getopt/Exception.php';
859  "Option \"$flag\" requires a single-word parameter, but was given \"$param\".",
860  $this->getUsageMessage());
861  }
862  break;
863  case 'integer':
864  if (preg_match('/\D/', $param)) {
865  #require_once 'Zend/Console/Getopt/Exception.php';
867  "Option \"$flag\" requires an integer parameter, but was given \"$param\".",
868  $this->getUsageMessage());
869  }
870  break;
871  case 'string':
872  default:
873  break;
874  }
875  return true;
876  }
877 
884  protected function _addRulesModeGnu($rules)
885  {
886  $ruleArray = array();
887 
893  preg_match_all('/([a-zA-Z0-9]:?)/', $rules, $ruleArray);
894  foreach ($ruleArray[1] as $rule) {
895  $r = array();
896  $flag = substr($rule, 0, 1);
897  if ($this->_getoptConfig[self::CONFIG_IGNORECASE]) {
898  $flag = strtolower($flag);
899  }
900  $r['alias'][] = $flag;
901  if (substr($rule, 1, 1) == ':') {
902  $r['param'] = 'required';
903  $r['paramType'] = 'string';
904  } else {
905  $r['param'] = 'none';
906  }
907  $this->_rules[$flag] = $r;
908  $this->_ruleMap[$flag] = $flag;
909  }
910  }
911 
919  protected function _addRulesModeZend($rules)
920  {
921  foreach ($rules as $ruleCode => $helpMessage)
922  {
923  // this may have to translate the long parm type if there
924  // are any complaints that =string will not work (even though that use
925  // case is not documented)
926  if (in_array(substr($ruleCode, -2, 1), array('-', '='))) {
927  $flagList = substr($ruleCode, 0, -2);
928  $delimiter = substr($ruleCode, -2, 1);
929  $paramType = substr($ruleCode, -1);
930  } else {
931  $flagList = $ruleCode;
932  $delimiter = $paramType = null;
933  }
934  if ($this->_getoptConfig[self::CONFIG_IGNORECASE]) {
935  $flagList = strtolower($flagList);
936  }
937  $flags = explode('|', $flagList);
938  $rule = array();
939  $mainFlag = $flags[0];
940  foreach ($flags as $flag) {
941  if (empty($flag)) {
942  #require_once 'Zend/Console/Getopt/Exception.php';
944  "Blank flag not allowed in rule \"$ruleCode\".");
945  }
946  if (strlen($flag) == 1) {
947  if (isset($this->_ruleMap[$flag])) {
948  #require_once 'Zend/Console/Getopt/Exception.php';
950  "Option \"-$flag\" is being defined more than once.");
951  }
952  $this->_ruleMap[$flag] = $mainFlag;
953  $rule['alias'][] = $flag;
954  } else {
955  if (isset($this->_rules[$flag]) || isset($this->_ruleMap[$flag])) {
956  #require_once 'Zend/Console/Getopt/Exception.php';
958  "Option \"--$flag\" is being defined more than once.");
959  }
960  $this->_ruleMap[$flag] = $mainFlag;
961  $rule['alias'][] = $flag;
962  }
963  }
964  if (isset($delimiter)) {
965  switch ($delimiter) {
967  $rule['param'] = 'required';
968  break;
970  default:
971  $rule['param'] = 'optional';
972  }
973  switch (substr($paramType, 0, 1)) {
974  case self::TYPE_WORD:
975  $rule['paramType'] = 'word';
976  break;
977  case self::TYPE_INTEGER:
978  $rule['paramType'] = 'integer';
979  break;
980  case self::TYPE_STRING:
981  default:
982  $rule['paramType'] = 'string';
983  }
984  } else {
985  $rule['param'] = 'none';
986  }
987  $rule['help'] = $helpMessage;
988  $this->_rules[$mainFlag] = $rule;
989  }
990  }
991 
992 }
setArguments($argv)
Definition: Getopt.php:370
const CONFIG_IGNORECASE
Definition: Getopt.php:158
addArguments($argv)
Definition: Getopt.php:350
setOptions($getoptConfig)
Definition: Getopt.php:390
_checkParameterType($flag, $param)
Definition: Getopt.php:848
_parseLongOption(&$argv)
Definition: Getopt.php:764
addRules($rules)
Definition: Getopt.php:424
$type
Definition: item.phtml:13
$value
Definition: gender.phtml:16
const CONFIG_RULEMODE
Definition: Getopt.php:156
setAliases($aliasMap)
Definition: Getopt.php:644
const PARAM_OPTIONAL
Definition: Getopt.php:143
__set($key, $value)
Definition: Getopt.php:308
setOption($configKey, $configValue)
Definition: Getopt.php:409
_parseSingleOption($flag, &$argv)
Definition: Getopt.php:800
$method
Definition: info.phtml:13
setHelp($helpMap)
Definition: Getopt.php:677
_addRulesModeGnu($rules)
Definition: Getopt.php:884
_addRulesModeZend($rules)
Definition: Getopt.php:919
const CONFIG_DASHDASH
Definition: Getopt.php:157
_parseShortOptionCluster(&$argv)
Definition: Getopt.php:784
static encode($valueToEncode, $cycleCheck=false, $options=array())
Definition: Json.php:130
__construct($rules, $argv=null, $getoptConfig=array())
Definition: Getopt.php:242
const CONFIG_PARSEALL
Definition: Getopt.php:159
if(!trim($html)) $alias
Definition: details.phtml:20
if(isset($opts->o)) if(! $usingStdout) $l
const PARAM_REQUIRED
Definition: Getopt.php:142
if(!isset($_GET['name'])) $name
Definition: log.php:14