/** * Returns an ascii art version of the help * * This method uses the given configuration and parameters * to create and format an help text for the options you defined * in your config parameter. You can supply a header and a footer * as well as the maximum length of a line. If you supplied * descriptions for your options, they will be used as well. * * By default, it returns something like this: * <pre> * Usage: myscript.php [-dv --formats] <-fw --filters> * * -f --files values(2) Set the source and destination image files. * -w --width=<value> Set the new width of the image. * -d --debug Switch to debug mode. * --formats values(1-3) Set the image destination format. (jpegbig, * jpegsmall) * -fi --filters values(1-...) Set the filters to be applied to the image upon * conversion. The filters will be used in the order * they are set. * -v --verbose (optional)value Set the verbose level. (3) * </pre> * * @access public * @param array your args configuration * @param string the header for the help. If it is left null, * a default header will be used, starting by Usage: * @param string the footer for the help. This could be used * to supply a description of the error the user made * @param int help lines max length * @return string the formatted help text */ static function getHelp($config, $helpHeader = null, $helpFooter = '', $maxlength = 78) { // Start with an empty help message and build it piece by piece $help = ''; // If no user defined header, build the default header. if (!isset($helpHeader)) { // Get the optional, required and "paramter" names for this config. list($optional, $required, $params) = Console_Getargs::getOptionalRequired($config); // Start with the file name. if (isset($_SERVER['SCRIPT_NAME'])) { $filename = basename($_SERVER['SCRIPT_NAME']); } else { $filename = $argv[0]; } $helpHeader = 'Usage: ' . $filename . ' '; // Add the optional arguments and required arguments. $helpHeader .= $optional . ' ' . $required . ' '; // Add any parameters that are needed. $helpHeader .= $params . "\n\n"; } // Build the list of options and definitions. $i = 0; foreach ($config as $long => $def) { // Break the names up if there is more than one for an option. $shortArr = array(); if (isset($def['short'])) { $shortArr = explode('|', $def['short']); } $longArr = explode('|', $long); // Column one is the option name displayed as "-short, --long [additional info]" // Add the short option name. $col1[$i] = !empty($shortArr) ? '-' . $shortArr[0] . ' ' : ''; // Add the long option name. $col1[$i] .= '--' . $longArr[0]; // Get the min and max to show needed/optional values. $max = $def['max']; $min = isset($def['min']) ? $def['min'] : $max; if ($max === 1 && $min === 1) { // One value required. $col1[$i] .= '=<value>'; } else { if ($max > 1) { if ($min === $max) { // More than one value needed. $col1[$i] .= ' values(' . $max . ')'; } else { if ($min === 0) { // Argument takes optional value(s). $col1[$i] .= ' values(optional)'; } else { // Argument takes a range of values. $col1[$i] .= ' values(' . $min . '-' . $max . ')'; } } } else { if ($max === 1 && $min === 0) { // Argument can take at most one value. $col1[$i] .= ' (optional)value'; } else { if ($max === -1) { // Argument can take unlimited values. if ($min > 0) { $col1[$i] .= ' values(' . $min . '-...)'; } else { $col1[$i] .= ' (optional)values'; } } } } } // Column two is the description if available. if (isset($def['desc'])) { $col2[$i] = $def['desc']; } else { $col2[$i] = ''; } // Add the default value(s) if there are any/ if (isset($def['default'])) { if (is_array($def['default'])) { $col2[$i] .= ' (' . implode(', ', $def['default']) . ')'; } else { $col2[$i] .= ' (' . $def['default'] . ')'; } } $i++; } // Figure out the maximum length for column one. $arglen = 0; foreach ($col1 as $txt) { $length = strlen($txt); if ($length > $arglen) { $arglen = $length; } } // The maximum length for each description line. $desclen = $maxlength - $arglen; $padding = str_repeat(' ', $arglen); foreach ($col1 as $k => $txt) { // Wrap the descriptions. if (strlen($col2[$k]) > $desclen) { $desc = wordwrap($col2[$k], $desclen, "\n " . $padding); } else { $desc = $col2[$k]; } // Push everything together. $help .= str_pad($txt, $arglen) . ' ' . $desc . "\n"; } // Put it all together. return $helpHeader . $help . $helpFooter; }
/** * cliParse - parse command line arguments, and return the values. * Will die with help message if --help or error is found.. * * @param {String} $classname name of class - should be loaded.. * @return {Array|false} array of key=>value arguments.. or false if not parsed * */ function cliParse($classname) { // cli static $classname::$cli_opts try { // look up the parent tree for core opts. $cls = new ReflectionClass($classname); if (method_exists($classname, 'cli_opts')) { $val = $classname::cli_opts(); } else { $val = $cls->getStaticPropertyValue('cli_opts'); } $val = is_array($val) ? $val : array(); while ($cls = $cls->getParentClass()) { //var_dump($cls); try { if (method_exists($cls->name, 'cli_opts')) { $cn = $cls->name; $vadd = $cn::cli_opts(); } else { $vadd = $cls->getStaticPropertyValue('cli_opts'); } $val = array_merge($val, is_array($vadd) ? $vadd : array()); } catch (Exception $e) { continue; } } } catch (Exception $e) { echo "Warning: {$e->getMessage()}\n"; } if (empty($val)) { return false; } $val = array_merge(self::$cli_opts, $val); require_once 'Console/Getargs.php'; $ar = $_SERVER['argv']; $call = array(array_shift($ar)); // remove index.php $call[] = array_shift($ar); // remove our class... //var_dump($ar); $newargs = Console_Getargs::factory($val, $ar); if (!is_a($newargs, 'PEAR_Error')) { return $newargs->getValues(); } list($optional, $required, $params) = Console_Getargs::getOptionalRequired($val); $helpHeader = 'Usage: php ' . implode(' ', $call) . ' ' . $optional . ' ' . $required . ' ' . $params . "\n\n"; if ($newargs->getCode() === CONSOLE_GETARGS_ERROR_USER) { // User put illegal values on the command line. echo Console_Getargs::getHelp($val, $helpHeader, "\n\n" . $newargs->getMessage(), 78, 4) . "\n\n"; exit; } if ($newargs->getCode() === CONSOLE_GETARGS_HELP) { // User needs help. echo Console_Getargs::getHelp($val, $helpHeader, NULL, 78, 4) . "\n\n"; exit; } die($newargs->getMessage()); }