Re: Idea for an OOP frontend for Console/Getopt.php
| From: | Greg Beaver | Date: | Thu, 29 Jan 2004 17:35:00 +0000 |
| Subject: | Re: Idea for an OOP frontend for Console/Getopt.php | ||
| References: | 1 | Groups: | php.pear.dev |
| Request: | Send a blank email to pear-dev+get-25333@lists.php.net to get a copy of this message | ||
Hi,
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()
Your method would be most useful for people who wish to have extended functionality over an array. I can't think of a good example of when this would be necessary. Every case of data processing should be separate from the input collection, otherwise you limit yourself to only one kind of data input. The only way to guarantee that a project could use both console and web input would be to have two input processing classes, one that uses your code and calls setValue($key, $value) for every command-line option, and another that reads from $_GET/$_POST and calls setValue() with those options. 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.
In any case, my main objection currently is that OOP should be truly OOP and truly necessary.
Greg
Christian Hammers wrote:
Hello The maintainer of Console/Getopt.php suggested me to post here as he does not plan to do anything like the proposed although he did not object that it would be a good idea. What do you think about this? Maybe I just did miss a module or it's already in some CVS tree.. Would anybody like to implement it (or would I have to do it :-)) bye, -christian- ------------------------------------------------------------------------ Subject: Idea for an OOP frontend for Console/Getopt.php From: Christian Hammers <ch@lathspell.de> Date: Mon, 19 Jan 2004 02:25:09 +0100 To: Andrei Zmievski <andrei@php.net> Hello Compared to Pearls Getopt::Long, I find the PHP verions still quite uncomfortable to use. As most of PEAR is object oriented anyway I had the idea of creating a little frontend for Console/Getopt.php. $args = new GetoptObj(); print $args->parametername; You find a rough version attached. What do you think of it? Is something like that planned by you or were there strong reasons against it? bye, -christian- ------------------------------------------------------------------------ #!/usr/bin/php4 -q <?php require_once 'Console/Getopt.php'; # Object oriented wrapper for Console::Getopt. # # Known limitations: # - Argument names are unique keys and may not given more than once# on the command line (they would overwrite the object attribute.# - Argument names may not start with an underscore ('_') as this# char is reserved for marking variables as class internal.class GetoptObj {var $_further_args;# constructor function GetoptObj($shortargs, $longargs) { $con = new Console_Getopt();$args = $con->readPHPArgv(); $args_shifted = $args; array_shift($args_shifted);$res = $con->getopt( $args_shifted, $shortargs, $longargs );$this->_further_args = $res[1];foreach (array_merge($res[0]) as $param) { $key = $param[0]; $value = $param[1];# Can't be represented in non-array mode. if (isset($this->$key)) { trigger_error("Options may not given more than once ($key).", E_USER_ERROR); }# Normalise parameter names. if (substr($key, 0, 2) == "--") { $key = substr($key, 2); }# Do not overwrite our class internal and thus reserved attributes. # (internal error - should never have been allowed to getopt()!) if (substr($key, 0, 1) == "_") { trigger_error("Parameter names may not start with '_' ($key).", E_USER_ERROR); }# Attach this parameter as object variable. $this->$key = $value; } }# Returns all further arguments that were not parsed by getopt. function getFurtherArgs() { return $this->_further_args; }} ## ## Test ## # # PEAR::setErrorHandling(PEAR_ERROR_DIE); # # $args = new GetoptObj("x:", array("gugu=")); # # var_dump($args); # print("args->gugu = ".$args->gugu."\n"); # $a = $args->getFurtherArgs(); # print("args->.... = ".$a[0]."\n"); # # ---> ./GetoptObj.php --gugu=gaga -x 1 hallo ?>