cvs: php4 /pear/Console Getopt.php

From: Date: Sun, 06 Jan 2002 20:17:00 +0000
Subject: cvs: php4 /pear/Console Getopt.php
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-1857@lists.php.net to get a copy of this message
cox Sun Jan 6 15:17:00 2002 EDT Modified files: /php4/pear/Console Getopt.php Log: Added readPHPArgv() function that will safely read the $argv PHP array across different PHP configurations. Will take care on register_globals and register_argc_argv ini directives and the new $_SERVER vars Index: php4/pear/Console/Getopt.php diff -u php4/pear/Console/Getopt.php:1.9 php4/pear/Console/Getopt.php:1.10 --- php4/pear/Console/Getopt.php:1.9 Tue Dec 25 22:58:44 2001 +++ php4/pear/Console/Getopt.php Sun Jan 6 15:17:00 2002 @@ -17,7 +17,7 @@ // | Authors: Andrei Zmievski <andrei@ispi.net> | // +----------------------------------------------------------------------+ // -// $Id: Getopt.php,v 1.9 2001/12/26 03:58:44 cox Exp $ +// $Id: Getopt.php,v 1.10 2002/01/06 20:17:00 cox Exp $ require_once 'PEAR.php'; @@ -54,9 +54,9 @@ * * Most of the semantics of this function are based on GNU getopt_long(). * - * @param $args array an array of command-line arguments - * @param $short_options string specifies the list of allowed short options - * @param $long_options array specifies the list of allowed long options + * @param array $args an array of command-line arguments + * @param string $short_options specifies the list of allowed short options + * @param array $long_options specifies the list of allowed long options * * @return array two-element array containing the list of parsed options and * the non-option arguments @@ -66,6 +66,10 @@ */ function getopt($args, $short_options, $long_options = null) { + // in case you pass directly readPHPArgv() as the first arg + if (PEAR::isError($args)) { + return $args; + } $opts = array(); $non_opts = array(); @@ -189,6 +193,29 @@ return new Getopt_Error("unrecognized option --$opt\n"); } + + /** + * Safely read the $argv PHP array across different PHP configurations. + * Will take care on register_globals and register_argc_argv ini directives + * + * @access public + * @return mixed the $argv PHP array or PEAR error if not registered + */ + function readPHPArgv() + { + global $argv; + if (!is_array($argv)) { + if (!is_array($_SERVER['argv'])) { + if (!is_array($HTTP_SERVER_VARS['argv'])) { + return new Getopt_Error("Could not read cmd args (register_argc_argv=Off?)\n"); + } + return $HTTP_SERVER_VARS['argv']; + } + return $_SERVER['argv']; + } + return $argv; + } + }

« previous php.pear.cvs (#1857) next »