169 self::CONFIG_RULEMODE => self::MODE_ZEND,
170 self::CONFIG_DASHDASH =>
true,
171 self::CONFIG_IGNORECASE =>
false,
172 self::CONFIG_PARSEALL =>
true,
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" 252 '$_SERVER["argv"] is not set, but Zend_Console_Getopt cannot work without this information.' 257 $this->_progname = $_SERVER[
'argv'][0];
260 if (!is_array($argv)) {
261 $argv = array_slice($_SERVER[
'argv'], 1);
294 if (isset($this->_ruleMap[$key])) {
295 $key = $this->_ruleMap[$key];
296 return isset($this->_options[$key]);
311 if (isset($this->_ruleMap[$key])) {
312 $key = $this->_ruleMap[$key];
313 $this->_options[$key] =
$value;
336 if (isset($this->_ruleMap[$key])) {
337 $key = $this->_ruleMap[$key];
338 unset($this->_options[$key]);
352 if(!is_array($argv)) {
353 #require_once 'Zend/Console/Getopt/Exception.php'; 355 "Parameter #1 to addArguments should be an array");
357 $this->_argv = array_merge($this->_argv, $argv);
358 $this->_parsed =
false;
372 if(!is_array($argv)) {
373 #require_once 'Zend/Console/Getopt/Exception.php'; 375 "Parameter #1 to setArguments should be an array");
377 $this->_argv = $argv;
378 $this->_parsed =
false;
392 if (isset($getoptConfig)) {
393 foreach ($getoptConfig as $key =>
$value) {
411 if ($configKey !==
null) {
412 $this->_getoptConfig[$configKey] = $configValue;
426 $ruleMode = $this->_getoptConfig[
'ruleMode'];
427 switch ($this->_getoptConfig[
'ruleMode']) {
443 $method =
'_addRulesMode' . ucfirst($ruleMode);
446 $this->_parsed =
false;
459 foreach ($this->_options as $flag =>
$value) {
460 $s[] = $flag .
'=' . (
$value ===
true ?
'true' :
$value);
462 return implode(
' ', $s);
478 foreach ($this->_options as $flag =>
$value) {
496 foreach ($this->_options as $flag =>
$value) {
497 $j[
'options'][] = array(
508 #require_once 'Zend/Json.php'; 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));
529 $optionNode->setAttribute(
'parameter', utf8_encode(
$value));
531 $optionsNode->appendChild($optionNode);
533 $xml = $doc->saveXML();
545 return array_keys($this->_options);
561 if ($this->_getoptConfig[self::CONFIG_IGNORECASE]) {
562 $flag = strtolower($flag);
564 if (isset($this->_ruleMap[$flag])) {
565 $flag = $this->_ruleMap[$flag];
566 if (isset($this->_options[$flag])) {
567 return $this->_options[$flag];
595 $usage =
"Usage: {$this->_progname} [ options ]\n";
598 foreach ($this->_rules as
$rule) {
600 if (is_array(
$rule[
'alias'])) {
601 foreach (
$rule[
'alias'] as $flag) {
602 $flags[] = (strlen($flag) == 1 ?
'-' :
'--') . $flag;
605 $linepart[
'name'] = implode(
'|', $flags);
606 if (isset(
$rule[
'param']) &&
$rule[
'param'] !=
'none') {
607 $linepart[
'name'] .=
' ';
608 switch (
$rule[
'param']) {
610 $linepart[
'name'] .=
"[ <{$rule['paramType']}> ]";
613 $linepart[
'name'] .=
"<{$rule['paramType']}>";
617 if (strlen($linepart[
'name']) > $maxLen) {
618 $maxLen = strlen($linepart[
'name']);
620 $linepart[
'help'] =
'';
621 if (isset(
$rule[
'help'])) {
622 $linepart[
'help'] .=
$rule[
'help'];
624 $lines[] = $linepart;
626 foreach ($lines as $linepart) {
627 $usage .= sprintf(
"%s %s\n",
628 str_pad($linepart[
'name'], $maxLen),
646 foreach ($aliasMap as $flag =>
$alias)
648 if ($this->_getoptConfig[self::CONFIG_IGNORECASE]) {
649 $flag = strtolower($flag);
652 if (!isset($this->_ruleMap[$flag])) {
655 $flag = $this->_ruleMap[$flag];
656 if (isset($this->_rules[
$alias]) || isset($this->_ruleMap[
$alias])) {
658 #require_once 'Zend/Console/Getopt/Exception.php'; 660 "Option \"$o\" is being defined more than once.");
662 $this->_rules[$flag][
'alias'][] =
$alias;
663 $this->_ruleMap[
$alias] = $flag;
679 foreach ($helpMap as $flag => $help)
681 if (!isset($this->_ruleMap[$flag])) {
684 $flag = $this->_ruleMap[$flag];
685 $this->_rules[$flag][
'help'] = $help;
701 if ($this->_parsed ===
true) {
705 $this->_options = array();
706 $this->_remainingArgs = array();
707 while (count($argv) > 0) {
708 if ($argv[0] ==
'--') {
710 if ($this->_getoptConfig[self::CONFIG_DASHDASH]) {
711 $this->_remainingArgs = array_merge($this->_remainingArgs, $argv);
715 if (substr($argv[0], 0, 2) ==
'--') {
717 }
else if (substr($argv[0], 0, 1) ==
'-' && (
'-' != $argv[0] || count($argv) >1)) {
719 }
else if($this->_getoptConfig[self::CONFIG_PARSEALL]) {
720 $this->_remainingArgs[] = array_shift($argv);
726 $this->_remainingArgs = array_merge($this->_remainingArgs, $argv);
730 $this->_parsed =
true;
740 if (
$rule[
'param'] ===
'required') {
743 $defined = $defined ===
true ? true : array_key_exists(
$alias, $this->_options);
745 if ($defined ===
false) {
746 #require_once 'Zend/Console/Getopt/Exception.php'; 748 'Option "$alias" requires a parameter.',
766 $optionWithParam = ltrim(array_shift($argv),
'-');
767 $l = explode(
'=', $optionWithParam, 2);
768 $flag = array_shift(
$l);
769 $param = array_shift(
$l);
771 array_unshift($argv, $param);
786 $flagCluster = ltrim(array_shift($argv),
'-');
787 foreach (str_split($flagCluster) as $flag) {
802 if ($this->_getoptConfig[self::CONFIG_IGNORECASE]) {
803 $flag = strtolower($flag);
805 if (!isset($this->_ruleMap[$flag])) {
806 #require_once 'Zend/Console/Getopt/Exception.php'; 808 "Option \"$flag\" is not recognized.",
811 $realFlag = $this->_ruleMap[$flag];
812 switch ($this->_rules[$realFlag][
'param']) {
814 if (count($argv) > 0 && substr($argv[0], 0, 1) !=
'-') {
815 $param = array_shift($argv);
818 #require_once 'Zend/Console/Getopt/Exception.php'; 820 "Option \"$flag\" requires a parameter.",
825 if (count($argv) > 0 && substr($argv[0], 0, 1) !=
'-') {
826 $param = array_shift($argv);
835 $this->_options[$realFlag] = $param;
851 if (isset($this->_rules[$flag][
'paramType'])) {
852 $type = $this->_rules[$flag][
'paramType'];
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\".",
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\".",
886 $ruleArray = array();
893 preg_match_all(
'/([a-zA-Z0-9]:?)/',
$rules, $ruleArray);
894 foreach ($ruleArray[1] as
$rule) {
896 $flag = substr(
$rule, 0, 1);
897 if ($this->_getoptConfig[self::CONFIG_IGNORECASE]) {
898 $flag = strtolower($flag);
900 $r[
'alias'][] = $flag;
901 if (substr(
$rule, 1, 1) ==
':') {
902 $r[
'param'] =
'required';
903 $r[
'paramType'] =
'string';
905 $r[
'param'] =
'none';
907 $this->_rules[$flag] = $r;
908 $this->_ruleMap[$flag] = $flag;
921 foreach (
$rules as $ruleCode => $helpMessage)
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);
931 $flagList = $ruleCode;
932 $delimiter = $paramType =
null;
934 if ($this->_getoptConfig[self::CONFIG_IGNORECASE]) {
935 $flagList = strtolower($flagList);
937 $flags = explode(
'|', $flagList);
939 $mainFlag = $flags[0];
940 foreach ($flags as $flag) {
942 #require_once 'Zend/Console/Getopt/Exception.php'; 944 "Blank flag not allowed in rule \"$ruleCode\".");
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.");
952 $this->_ruleMap[$flag] = $mainFlag;
953 $rule[
'alias'][] = $flag;
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.");
960 $this->_ruleMap[$flag] = $mainFlag;
961 $rule[
'alias'][] = $flag;
964 if (isset($delimiter)) {
965 switch ($delimiter) {
967 $rule[
'param'] =
'required';
971 $rule[
'param'] =
'optional';
973 switch (substr($paramType, 0, 1)) {
975 $rule[
'paramType'] =
'word';
978 $rule[
'paramType'] =
'integer';
982 $rule[
'paramType'] =
'string';
985 $rule[
'param'] =
'none';
987 $rule[
'help'] = $helpMessage;
988 $this->_rules[$mainFlag] =
$rule;
setOptions($getoptConfig)
_checkParameterType($flag, $param)
setOption($configKey, $configValue)
_parseSingleOption($flag, &$argv)
_addRulesModeZend($rules)
_parseShortOptionCluster(&$argv)
static encode($valueToEncode, $cycleCheck=false, $options=array())
__construct($rules, $argv=null, $getoptConfig=array())
if(isset($opts->o)) if(! $usingStdout) $l
if(!isset($_GET['name'])) $name