Package home | Report new bug | New search | Development Roadmap Status: Open | Feedback | All | Closed Since Version 1.2.2

Request #13038 Remove dependency on $_SERVER
Submitted: 2008-02-01 15:42 UTC
From: pielud Assigned: izi
Status: Closed Package: Console_CommandLine (version 1.0.0RC1)
PHP Version: 5.2.5 OS: debian linux
Roadmaps: (Not assigned)    
Subscription  


 [2008-02-01 15:42 UTC] pielud (Dana Pieluszczak)
Description: ------------ Would it be possible to remove the dependency on $_SERVER to get argv/argc? Instead they could be passed to the constructor or to the parse() method. This will promote code reuse and make the class more testable. See code sample below. Test script: --------------- <?php // passing argv/argc to the constructor $parser = new Console_CommandLine(array('argv' => $_SERVER['argv'], 'argc' => $_SERVER['argc'])); // passing argv/argc to parse(), this seems like the better solution // since the same parser object could be reused several times (like in a unit test) $parser = new Console_CommandLine(); $parser->addOption('path', array( 'short_name' => '-p', 'long_name' => '--path', 'description' => 'path to the dir', 'action' => 'StoreString', 'default' => '/tmp' )); $parser->parse($_SERVER['argv'], $_SERVER['argc']); // the method signature of parse could be something like this, // to support backwards compatibility public function parse($argv=$_SERVER['argv'], $argc=$_SERVER['argc'], $beginAt=0) ?>

Comments

 [2008-02-01 16:03 UTC] pielud (Dana Pieluszczak)
Apparently you can't default function arguments like that, and that also isn't backwards compatible, parse should be something like this: <?php ... public function parse($beginAt=0, $argv=null, $argc=null) { $argv = $argv ? $argv : $_SERVER['argv']; $argc = $argc ? $argc : $_SERVER['argc']; ... } ?>
 [2008-02-01 18:13 UTC] izi (David Jean Louis)
Dana, This change would be useful indeed, the method signature should be: public function parse($userArgc=null, $userArgv=null, $beginAt=0) { } because $argv and $argc can exists in the global scope if the php.ini directive "register_argc_argv" is set to "on". I put $userArgc first mainly to be consistant with the c syntax: int main(int argc, char *argv[]) Thanks for your report.