Re: Idea for an OOP frontend for Console/Getopt.php

From: Date: Sun, 01 Feb 2004 19:36:56 +0000
Subject: Re: Idea for an OOP frontend for Console/Getopt.php
References: 1 2  Groups: php.pear.dev 
Request: Send a blank email to pear-dev+get-25391@lists.php.net to get a copy of this message
Hello On Thu, Jan 29, 2004 at 12:35:00PM -0500, Greg Beaver wrote: > I could see a new method in Console_Getopt that would take 3 arguments, > the long, short options and an object. Also, any command-line that is > passed more than once could simply be added to an array > > if (isset($obj->key)) { > if (!is_array($obj->key)) { > $obj->key = array($obj->key); > } > $obj->key[] = $value; > } > > However, I see no reason for using an object as an array - this would > just slow things down. After all, what's the difference between > $obj['key'] and $obj->key? 2 characters? You still have to do isset() Sadly it's not that simple. Currently you get back something like this: array(3) { [0]=> array(2) { [0]=> string(5) "--key" [1]=> string(1) "4" } [1]=> array(2) { [0]=> string(12) "--defaultkey" [1]=> NULL } So to see if "--key" was given, you would have to do a foreach loop and check for [0]=="--key" (or do I miss a proper array function?). Well apart from that my main concern is just that the current class makes it much to complicated for simple programs to get their args. Other suggestions are: 1. Most programs allow/use a parameter only once so the programmer should have a possibillity to disallow multiple arguments to not have to check if a result is an array or a scalar value each time. Maybe via $con = new Console_Getopt(); $con->allow_multiple_keys = 0; $con->getopt(...); 2. If, for compatibility the above mentioned array struction should be kept, then some additional funktions would improve the class: $con->isset("--key") returns a bool $con->getSingle("--key") returns always a scalar $con->get("--key") returns a scalar or array depend on how often --key was given. If allow_multiple_keys=0 then always a scalar > phpDocumentor, for instance, accepts input from 3 different sources: > command-line, web form, and configuration files that are specified > through the other two forms of input. 3. The "$args=readPHPArgs(); array_shift($args);" could be done internally anyway at least as a kind of shortcut: $con = new Console_Getopt(); $con->getopt("console", ...); or $con->getopt("http_post", ...); As the first argument used to be an array this should be easy to differentiate. > In any case, my main objection currently is that OOP should be truly OOP > and truly necessary. I have nothing against procedural programming but thought that PEAR tries to be as OOP as possible and therefore proposed it this way. > Greg bye, -christian-

« previous php.pear.dev (#25391) next »