Example #1
0
 function getOptions($config, $argumentConfig, $arguments = false)
 {
     $program = false;
     if ($arguments === false) {
         $arguments = $GLOBALS['argv'];
         $program = $arguments[0];
         array_shift($arguments);
     }
     if (is_string($config)) {
         $config = eZCLI::parseOptionString($config, $tmpConfig);
     }
     if (is_string($argumentConfig)) {
         $argumentConfig = eZCLI::parseOptionString($argumentConfig, $tmpArgumentConfig);
     }
     $options = array();
     $helpOption = false;
     $helpText = false;
     if (isset($config['short']['h'])) {
         $helpOption = '-h';
     } else {
         if (isset($config['short']['help'])) {
             $helpOption = '--help';
         }
     }
     if ($helpOption) {
         $helpText = "\n" . "Try `{$program} {$helpOption}' for more information.";
     }
     $options['arguments'] = array();
     $arguments = array_values($arguments);
     $argumentCount = count($arguments);
     for ($i = 0; $i < $argumentCount; ++$i) {
         $argument = $arguments[$i];
         $argumentLen = strlen($argument);
         if ($argumentLen > 1 and $argument[0] == '-') {
             $argumentValue = false;
             if ($argumentLen > 2 and $argument[1] == '-') {
                 $optionName = substr($argument, 2);
                 $assignPosition = strpos($optionName, '=');
                 if ($assignPosition !== false) {
                     $argumentValue = substr($optionName, $assignPosition + 1);
                     $optionName = substr($optionName, 0, $assignPosition);
                 }
                 $optionType = 'long';
                 $optionPrefix = '--';
                 $checkNext = false;
             } else {
                 $optionName = $argument[1];
                 if ($argumentLen > 2) {
                     $argumentValue = substr($argument, 2);
                 }
                 $optionType = 'short';
                 $optionPrefix = '-';
                 $checkNext = true;
             }
             $configItem =& $config[$optionType][$optionName];
             if (isset($configItem)) {
                 $value = true;
                 $hasValue = $configItem['has-value'];
                 $hasMultipleValues = ($configItem['quantifier']['min'] > 1 or $configItem['quantifier']['max'] === false or $configItem['quantifier']['max'] > 1);
                 if ($hasValue) {
                     $hasArgumentValue = false;
                     if ($argumentValue !== false) {
                         $value = $argumentValue;
                     } else {
                         if ($checkNext and $configItem['has-value'] !== 'optional') {
                             ++$i;
                             if ($i < $argumentCount) {
                                 $hasArgumentValue = true;
                             } else {
                                 --$i;
                                 $this->error("{$program}: option `{$optionPrefix}{$optionName}' requires an argument" . $helpText);
                                 return false;
                             }
                             if ($hasArgumentValue) {
                                 $value = $arguments[$i];
                             }
                         } else {
                             if ($configItem['has-value'] !== 'optional') {
                                 $this->error("{$program}: option `{$optionPrefix}{$optionName}' requires an argument" . $helpText);
                                 return false;
                             }
                         }
                     }
                 }
                 $optionStoreName = $configItem['store-name'];
                 if (!$optionStoreName) {
                     $optionStoreName = $optionName;
                 }
                 if ($hasMultipleValues) {
                     if (!isset($options[$optionStoreName])) {
                         $options[$optionStoreName] = array();
                     }
                     $options[$optionStoreName][] = $value;
                 } else {
                     $options[$optionStoreName] = $value;
                 }
             } else {
                 $this->error("{$program}: invalid option `{$optionPrefix}{$optionName}'" . $helpText);
                 return false;
             }
         } else {
             $options['arguments'][] = $argument;
         }
     }
     foreach ($config['list'] as $configItem) {
         $optionStoreName = $configItem['store-name'];
         if ($optionStoreName and !isset($options[$optionStoreName])) {
             $options[$optionStoreName] = null;
         }
     }
     return $options;
 }
Example #2
0
 function getOptions($config = '', $argumentConfig = '', $optionHelp = false, $arguments = false, $useStandardOptions = true)
 {
     if (is_string($config)) {
         $config = eZCLI::parseOptionString($config, $tmpConfig);
     }
     if (is_string($argumentConfig)) {
         $argumentConfig = eZCLI::parseOptionString($argumentConfig, $tmpArgumentConfig);
     }
     if ($useStandardOptions) {
         if (!is_array($useStandardOptions)) {
             $useStandardOptions = array();
         }
         $useStandardOptions = array_merge(array('debug' => true, 'colors' => true, 'log' => true, 'siteaccess' => true, 'verbose' => true, 'root' => true, 'user' => false), $useStandardOptions);
     }
     if ($useStandardOptions) {
         $optionConfig = $config;
         $excludeOptions = array();
         $optionString = "[h|help][q|quiet]";
         $excludeOptions[] = 'h';
         $excludeOptions[] = 'help';
         $excludeOptions[] = 'q';
         $excludeOptions[] = 'quiet';
         if ($useStandardOptions['debug']) {
             $optionString .= "[d;*|debug;*]";
             $excludeOptions[] = 'd';
             $excludeOptions[] = 'debug';
         }
         if ($useStandardOptions['colors']) {
             $optionString .= "[c|colors][no-colors]";
             $excludeOptions[] = 'c';
             $excludeOptions[] = 'colors';
             $excludeOptions[] = 'no-colors';
         }
         if ($useStandardOptions['log']) {
             $optionString .= "[logfiles][no-logfiles]";
             $excludeOptions[] = 'logfiles';
             $excludeOptions[] = 'no-logfiles';
         }
         if ($useStandardOptions['siteaccess']) {
             $optionString .= "[s:|siteaccess:]";
             $excludeOptions[] = 's';
             $excludeOptions[] = 'siteaccess';
         }
         if ($useStandardOptions['user']) {
             $optionString .= "[l:|login:][p:|password:]";
             $excludeOptions[] = 'l';
             $excludeOptions[] = 'login';
             $excludeOptions[] = 'p';
             $excludeOptions[] = 'password';
         }
         if ($useStandardOptions['verbose']) {
             $optionString .= "[v*|verbose*]";
             $excludeOptions[] = 'v';
             $excludeOptions[] = 'verbose';
         }
         if ($useStandardOptions['root']) {
             $optionString .= "[r?|allow-root-user?]";
             $excludeOptions[] = 'r';
             $excludeOptions[] = 'allow-root-user';
         }
         $config = eZCLI::parseOptionString($optionString, $optionConfig);
     }
     $cli = eZCLI::instance();
     $options = $cli->getOptions($config, $argumentConfig, $arguments);
     $this->CurrentOptionConfig = $config;
     $this->CurrentOptions = $options;
     $this->CurrentStandardOptions = $useStandardOptions;
     $this->CurrentExcludeOptions = $excludeOptions;
     $this->CurrentOptionHelp = $optionHelp;
     $this->ArgumentConfig = $argumentConfig;
     if (!$options) {
         if (!$this->IsInitialized) {
             $this->initialize();
         }
         $this->shutdown(1);
     }
     if ($useStandardOptions) {
         if (function_exists('posix_getuid')) {
             if (posix_getuid() === 0) {
                 if (!$options['allow-root-user']) {
                     $cli->warning("Running scripts as root may be dangerous.");
                     $cli->warning("If you think you know what you are doing, you can run this script with the ");
                     $cli->warning("root account by appending the parameter --allow-root-user.");
                     exit(1);
                 } else {
                     // ho no, you are not going to be quiet while
                     // running with root priviledges
                     $this->setIsQuiet(false);
                     $cli = eZCLI::instance();
                     $cli->warning('With great power comes great responsibility.');
                     $cli->warning("You have 10 seconds to break the script (press Ctrl-C).");
                     sleep(10);
                 }
             }
         }
         if ($options['quiet']) {
             $this->setIsQuiet(true);
         }
         $useColors = true;
         if ($options['colors']) {
             $useColors = true;
         }
         if ($options['no-colors']) {
             $useColors = false;
         }
         $cli->setUseStyles($useColors);
         if ($options['debug']) {
             $levels = array();
             foreach ($options['debug'] as $debugOption) {
                 $levels = array_merge($levels, explode(',', $debugOption));
             }
             $allowedDebugLevels = array();
             $useDebugAccumulators = false;
             $useDebugTimingpoints = false;
             $useIncludeFiles = false;
             foreach ($levels as $level) {
                 if ($level == 'all') {
                     $useDebugAccumulators = true;
                     $allowedDebugLevels = false;
                     $useDebugTimingpoints = true;
                     break;
                 }
                 if ($level == 'accumulator') {
                     $useDebugAccumulators = true;
                     continue;
                 }
                 if ($level == 'timing') {
                     $useDebugTimingpoints = true;
                     continue;
                 }
                 if ($level == 'include') {
                     $useIncludeFiles = true;
                 }
                 if ($level == 'strict') {
                     $level = eZDebug::LEVEL_STRICT;
                 } else {
                     if ($level == 'error') {
                         $level = eZDebug::LEVEL_ERROR;
                     } else {
                         if ($level == 'warning') {
                             $level = eZDebug::LEVEL_WARNING;
                         } else {
                             if ($level == 'debug') {
                                 $level = eZDebug::LEVEL_DEBUG;
                             } else {
                                 if ($level == 'notice') {
                                     $level = eZDebug::LEVEL_NOTICE;
                                 } else {
                                     if ($level == 'timing') {
                                         $level = eZDebug::LEVEL_TIMING_POINT;
                                     }
                                 }
                             }
                         }
                     }
                 }
                 $allowedDebugLevels[] = $level;
             }
             $this->setUseDebugOutput(true);
             $this->setAllowedDebugLevels($allowedDebugLevels);
             $this->setUseDebugAccumulators($useDebugAccumulators);
             $this->setUseDebugTimingPoints($useDebugTimingpoints);
             $this->setUseIncludeFiles($useIncludeFiles);
             $this->setDebugMessage("\n\n" . str_repeat('#', 36) . $cli->style('emphasize') . " DEBUG " . $cli->style('emphasize-end') . str_repeat('#', 36) . "\n");
         }
         if (count($options['verbose']) > 0) {
             $this->setShowVerboseOutput(count($options['verbose']));
         }
         if (isset($options['siteaccess']) and $options['siteaccess']) {
             $this->setUseSiteAccess($options['siteaccess']);
         }
         if ($options['help']) {
             if (!$this->IsInitialized) {
                 $this->initialize();
             }
             $this->showHelp();
             $this->shutdown(0);
         }
         if (isset($options['login']) and $options['login']) {
             $this->setUser($options['login'], isset($options['password']) ? $options['password'] : false);
         }
     }
     return $options;
 }