/** * constructor * */ public function __construct() { $console_config = $this->getConfigArray(); $configArray = array(); // cli view argument definition $configArray['view'] = array('short' => 'v', 'min' => 1, 'max' => 1, 'desc' => 'Set the view to execute.'); $console_config = array_merge($configArray, $console_config); $this->args =& Console_Getargs::factory($console_config); if (PEAR::isError($this->args)) { $mes = ''; if ($this->args->getCode() === CONSOLE_GETARGS_ERROR_USER) { $mes = Console_Getargs::getHelp($console_config, null, $this->args->getMessage()) . "\n\n"; } else { if ($this->args->getCode() === CONSOLE_GETARGS_HELP) { $mes = Console_Getargs::getHelp($console_config) . "\n\n"; } } $this->printError($mes); } }
/** * Show full help information * * @param string $footer (optional) page footer content * * @return void * @access private */ function _printUsage($footer = '') { $header = 'Usage: ' . basename($_SERVER['SCRIPT_NAME']) . " [options]\n\n"; echo Console_Getargs::getHelp($this->opts, $header, "\n{$footer}\n", 78, 2) . "\n"; }
/** * 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; }
* $Id: example.php,v 1.7 2004/10/14 20:44:41 scottmattocks Exp $ */ require_once 'Console/Getargs.php'; $config = array('files|images' => array('short' => 'f|i', 'min' => 2, 'max' => 2, 'desc' => 'Set the source and destination image files.'), 'width' => array('short' => 'w', 'min' => 1, 'max' => 1, 'desc' => 'Set the new width of the image.'), 'debug' => array('short' => 'd', 'max' => 0, 'desc' => 'Switch to debug mode.'), 'formats' => array('min' => 1, 'max' => 3, 'desc' => 'Set the image destination format.', 'default' => array('jpegbig', 'jpegsmall')), 'filters' => array('short' => 'fi', 'min' => 1, 'max' => -1, 'desc' => 'Set the filters to be applied to the image upon conversion. The filters will be used in the order they are set.'), 'verbose' => array('short' => 'v', 'min' => 0, 'max' => 1, 'desc' => 'Set the verbose level.', 'default' => 3), CONSOLE_GETARGS_PARAMS => array('min' => 1, 'max' => 2, 'desc' => 'Set the application parameters.', 'default' => 'DEFAULT')); $args =& Console_Getargs::factory($config); // Use the following two lines to test passing an array // other than $_SERVER['argv'] //$test = array('-dvw', 500, 'foo1', 'foo2'); //$args =& Console_Getargs::factory($config, $test); if (PEAR::isError($args)) { $header = "Console_Getargs Example\n" . 'Usage: ' . basename($_SERVER['SCRIPT_NAME']) . " [options]\n\n"; if ($args->getCode() === CONSOLE_GETARGS_ERROR_USER) { echo Console_Getargs::getHelp($config, $header, $args->getMessage()) . "\n"; } else { if ($args->getCode() === CONSOLE_GETARGS_HELP) { echo Console_Getargs::getHelp($config, $header) . "\n"; // To see the automatic header uncomment this line //echo Console_Getargs::getHelp($config)."\n"; } } exit; } echo 'Verbose: ' . $args->getValue('verbose') . "\n"; echo 'Formats: ' . (is_array($args->getValue('formats')) ? implode(', ', $args->getValue('formats')) . "\n" : $args->getValue('formats') . "\n"); echo 'Files: ' . ($args->isDefined('files') ? implode(', ', $args->getValue('files')) . "\n" : "undefined\n"); if ($args->isDefined('fi')) { echo 'Filters: ' . (is_array($args->getValue('fi')) ? implode(', ', $args->getValue('fi')) . "\n" : $args->getValue('fi') . "\n"); } else { echo "Filters: undefined\n"; } echo 'Width: ' . $args->getValue('w') . "\n";
function Version() { $config = array("server" => array('short' => 's', 'max' => 1, 'min' => 1, 'desc' => "server URL", 'default' => ""), "debug" => array('short' => 'd', 'max' => 1, 'min' => 1, 'desc' => "debug level", 'default' => "0")); $args =& Console_Getargs::factory($config); if (PEAR::isError($args)) { die(Console_Getargs::getHelp($config) . "\n"); } echo "PHP Fedora Client class version " . FedoraClient::VERSION . "\n"; $client = new FedoraPkgdb(array('debug' => intval($args->getValue('debug')), 'server' => $args->getValue('server'))); echo "pkgdb version " . $client->getVersion() . "\n"; }
/** * the framework can be run without a database even if it's configured. * to support this, we need to handle things like * --pman-nodatabase=1 on the command line. * * * @returns array() - args, false - nothing matched / invalid, true = help! * */ function parseDefaultOpts() { require_once 'Console/Getargs.php'; $ar = $_SERVER['argv']; $call = array(array_shift($ar)); // remove index.php $call[] = array_shift($ar); //var_dump($ar); $val = self::$cli_opts; $newargs = Console_Getargs::factory($val, $ar); if (is_a($newargs, 'PEAR_Error')) { if ($newargs->getCode() === CONSOLE_GETARGS_ERROR_USER) { // since we do not handle all the arguemnts here... // skip errors if we find unknown arguments. if (preg_match('/^Unknown argument/', $newargs->getMessage())) { return false; } // 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) { return true; // hel } return false; } // now handle real arguments.. $ret = $newargs->getValues(); foreach ($ret as $k => $v) { switch ($k) { case 'pman-nodatabase': //echo "Turning off database"; $this->ff->nodatabase = true; break; default: die("need to fix option {$k}"); } } return false; }
$db_type = prompt_var('Database type', 'mysql', $defaults ? @$options['database'] : null); $db_user = prompt_var('Database user', $FrameworkSetup->suggestUserName(), $defaults ? @$options['user'] : null); $db_pass = prompt_var('Database password', '', $defaults ? @$options['password'] : null); if (!@NewADOConnection("{$db_type}://{$db_user}:{$db_pass}@localhost")) { echo Ak::t('Could not connect to the database' . "\n"); set_db_user_and_pass($FrameworkSetup, $db_user, $db_pass, $db_type, false); } } $config = array('user' => array('short' => 'u', 'max' => 1, 'min' => 1, 'default' => 'root', 'desc' => 'Database user'), 'password' => array('short' => 'p', 'max' => 1, 'min' => 1, 'default' => '', 'desc' => 'Database password'), 'database' => array('short' => 'd', 'max' => 1, 'min' => 1, 'default' => 'mysql', 'desc' => 'Database type'), 'name' => array('short' => 'n', 'max' => 1, 'min' => 1, 'default' => AkInflector::underscore(basename(AK_BASE_DIR)), 'desc' => 'Database name. This is the name of your database. It will be prefixed with _dev and _tests for non production environments.'), 'languages' => array('short' => 'l', 'max' => 1, 'min' => 1, 'default' => 'en', 'desc' => 'Language codes for this application.'), 'interactive' => array('short' => 'i', 'max' => 0, 'min' => 0, 'default' => false, 'desc' => 'Interactive mode.')); $args =& Console_Getargs::factory($config); if (PEAR::isError($args)) { if ($args->getCode() === CONSOLE_GETARGS_ERROR_USER) { echo Console_Getargs::getHelp($config, null, $args->getMessage()) . "\n"; } else { if ($args->getCode() === CONSOLE_GETARGS_HELP) { echo @Console_Getargs::getHelp($config) . "\n"; } } exit; } $options = $args->getValues(); $FrameworkSetup = new FrameworkSetup(); $FrameworkSetup->setDefaultOptions(); $FrameworkSetup->getAvailableDatabases(); $app_name = empty($options['interactive']) ? $options['name'] : prompt_var('Database name. This is the name of your database. It will be prefixed with _dev and _tests for non production environments.', $options['name']); $db_user = $db_pass = $db_type = ''; set_db_user_and_pass($FrameworkSetup, $db_user, $db_pass, $db_type); $FrameworkSetup->setDatabaseUser($db_user, 'admin'); $FrameworkSetup->setDatabasePassword($db_pass, 'admin'); $FrameworkSetup->setDatabaseType($db_type); foreach (array('development', 'production', 'testing') as $mode) {
* Fetch the statistics for a torrent * * Usage: * # php scrape.php -t file * * @author Markus Tacker <*****@*****.**> * @version $Id$ */ // Includes require_once 'File/Bittorrent2/Decode.php'; require_once 'Console/Getargs.php'; // Get filename from command line $args_config = array('torrent' => array('short' => 't', 'min' => 1, 'max' => 1, 'desc' => 'Filename of the torrent')); $args =& Console_Getargs::factory($args_config); if (PEAR::isError($args) or !($torrent = $args->getValue('torrent'))) { echo Console_Getargs::getHelp($args_config) . "\n"; exit; } if (!is_readable($torrent)) { echo 'ERROR: "' . $torrent . "\" is not readable.\n"; exit; } // Decode the torrent $File_Bittorrent2_Decode = new File_Bittorrent2_Decode(); $File_Bittorrent2_Decode->decodeFile($torrent); echo "\nStatistics\n"; echo "++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n"; echo 'Tracker: ' . $File_Bittorrent2_Decode->getAnnounce() . "\n"; echo 'info hash: ' . $File_Bittorrent2_Decode->getInfoHash() . "\n"; foreach ($File_Bittorrent2_Decode->getStats() as $key => $val) { echo str_pad($key . ':', 20) . $val . "\n";
function get_console_options_for($description, $console_configuration) { global $script_name, $argv; $args =& Console_Getargs::factory($console_configuration); if (PEAR::isError($args)) { $replacements = array('-p --parameters values(1-...)' => 'install plugin_name,URL ...', 'Usage: ' . basename(__FILE__) => "Usage: {$script_name}", '[param1] ' => 'plugin_name PLUGIN_URL'); echo "\n{$description}\n" . str_repeat('-', strlen($description) + 1) . "\n"; if ($args->getCode() === CONSOLE_GETARGS_ERROR_USER) { echo str_replace(array_keys($replacements), array_values($replacements), Console_Getargs::getHelp($console_configuration, null, $args->getMessage())) . "\n"; } else { if ($args->getCode() === CONSOLE_GETARGS_HELP) { echo str_replace(array_keys($replacements), array_values($replacements), @Console_Getargs::getHelp($console_configuration)) . "\n"; } } exit; } return $args->getValues(); }
/** * Constructor. * * This will create an instance of the example object. If * you pass an array of arguments on construction, that * array will be used instead of the default agument list. * * @access public * @param array $argArray The optional argument list. * @return void */ function Example($argArray = NULL) { // Get the config array require_once 'Getargs.php'; $config = $this->getConfigArray(); // Get the arguments. if (is_array($argArray)) { // Use given arg list not $_SERVER['argv']. $args =& Console_Getargs::factory($config, $argArray); } else { // Use $_SERVER['argv']. $args =& Console_Getargs::factory($config); } // Check for errors. if (PEAR::isError($args)) { if ($args->getCode() === CONSOLE_GETARGS_ERROR_USER) { // User put illegal values on the command line. echo Console_Getargs::getHelp($config, NULL, $args->getMessage()) . "\n"; } else { if ($args->getCode() === CONSOLE_GETARGS_HELP) { // User needs help. echo Console_Getargs::getHelp($config) . "\n"; } } exit; } else { // Assign the member vars. $this->debug = $args->getValue('debug'); $this->file = $args->getValue(CONSOLE_GETARGS_PARAMS); $this->showLines = $args->getValue('showlines'); $this->verbose = $args->getValue('verbose'); $this->findWords = $args->getValue('find'); // Make sure the file is readable. if (!@is_readable($this->file)) { $msg = $this->file . ' is not readable.'; Console_Getargs::getHelp($config, NULL, $msg); } } }
/** * Prints the help message for the benchmark * * @param array $args The arguments to be listed * * @return void */ function printHelp($args) { $header = "Php Benchmark Example\n" . 'Usage: ' . basename($_SERVER['SCRIPT_NAME']) . " [options]\n\n"; if ($args->getCode() === CONSOLE_GETARGS_ERROR_USER) { echo Console_Getargs::getHelp($this->config, $header, $args->getMessage()) . "\n"; } else { if ($args->getCode() === CONSOLE_GETARGS_HELP) { echo Console_Getargs::getHelp($this->config, $header) . "\n"; } } }
/** * Test getValues('long') and getValues('short') with two arguments * * @access public * @return void */ function testMultiple() { $config = array('name' => array('short' => 'n', 'min' => 1, 'max' => 1, 'desc' => 'One argument.'), 'email' => array('short' => 'e', 'min' => 2, 'max' => 2, 'desc' => 'Two arguments.')); $args = array('-n', 'arg1', '-e', '*****@*****.**', '*****@*****.**'); $message = implode(' ', $args); $obj =& Console_Getargs::factory($config, $args); if (PEAR::isError($obj)) { $this->fail("'{$message}' " . $obj->getMessage()); } else { $this->assertEquals(array('name' => $args[1], 'email' => array($args[3], $args[4])), $obj->getValues('long'), "'{$message}' Incorrect value returned"); $this->assertEquals(array('n' => $args[1], 'e' => array($args[3], $args[4])), $obj->getValues('short'), "'{$message}' Incorrect value returned"); } $args = array('--name', 'arg1', '--email', '*****@*****.**', '*****@*****.**'); $message = implode(' ', $args); $obj =& Console_Getargs::factory($config, $args); if (PEAR::isError($obj)) { $this->fail("'{$message}' " . $obj->getMessage()); } else { $this->assertEquals(array('name' => $args[1], 'email' => array($args[3], $args[4])), $obj->getValues('long'), "'{$message}' Incorrect value returned"); $this->assertEquals(array('n' => $args[1], 'e' => array($args[3], $args[4])), $obj->getValues('short'), "'{$message}' Incorrect value returned"); } }
/** * @file * This file is a simple, commandline front end for the included * PES_File_PDF_Splitter class. It allows for splitting a multi-page PDF file * into several PNG files, with each page being a single image ones. * Configuration options are described below, or can be read by running * "php pdf_splitter.php --help" on the command line. */ // The used PEAR Console_Getargs class isn't PHP5 compliant, so hide // hide the depreciated error messages. error_reporting(E_ALL ^ E_DEPRECATED); include 'Console/Getargs.php'; include 'PES/File/PDF/Splitter.php'; $config = array('x_res' => array('desc' => 'The x resolution used to read in the PDF', 'min' => 0, 'max' => 1, 'default' => 100), 'y_res' => array('desc' => 'The y resolution used to read in the PDF', 'min' => 0, 'max' => 1, 'default' => 100), 'dest' => array('desc' => 'The path to write the created, child PNG pages to', 'min' => 0, 'max' => 1, 'default' => __DIR__), 'source' => array('desc' => 'The PDF file to split into PNG page files.', 'min' => 1, 'max' => 1)); $args = Console_Getargs::factory($config); if (PEAR::isError($args)) { if ($args->getCode() === CONSOLE_GETARGS_ERROR_USER) { echo Console_Getargs::getHelp($config, NULL, $args->getMessage()); } else { if ($args->getCode() === CONSOLE_GETARGS_HELP) { echo Console_Getargs::getHelp($config); } } } else { $splitter = new PES_File_PDF_Splitter(); $created_files = $splitter->setPDFPath($args->getValue('source'))->setXResolution($args->getValue('x_res') ?: 100)->setYResolution($args->getValue('y_res') ?: 100)->convertToPNG($args->getValue('dest') ?: __DIR__); echo 'Wrote ' . count($created_files) . ' PNG files:' . PHP_EOL; foreach ($created_files as $a_file) { echo ' - ' . $a_file . PHP_EOL; } }
function testAlias() { $config = array('name|alias' => array('short' => 'n|a', 'min' => 0, 'max' => 0, 'desc' => 'Unlimited number of values with alias.')); $args = array('-n', '-a', '--name', '--alias'); foreach ($args as $arg) { $message = $arg; $obj =& Console_Getargs::factory($config, array($arg)); if (PEAR::isError($obj)) { $this->fail("'{$message}' " . $obj->getMessage()); } else { $this->assertTrue($obj->isDefined('name'), "Value 'name' is not defined"); $this->assertTrue($obj->isDefined('alias'), "Value 'alias' is not defined"); $this->assertTrue($obj->isDefined('n'), "Value 'n' is not defined"); $this->assertTrue($obj->isDefined('a'), "Value 'a' is not defined"); } } }