Re: [PHP] getopt usage?

2003-01-17 Thread Jason Wong
On Thursday 16 January 2003 06:51, Eli Criffield wrote:
 I can't seem to find any examples of code using getopt() around. I'm
 thinking not many people use php for command line shell scripts.

 Here is what i came up with for getopt();

 ?
 $options = getopt(a:bc);

 reset($options);
 while(list($key, $value) = each($options)) {
 switch ($key) {
 case 'b':
 $B=1;
 break;
 case 'a':
 $A=$value;
 break;
 case 'c':
 $C=1;
 break;
 default:
 echo unknown option;
   // print usage or something
 break;
 }
 }
 ?

 The problem with this is it never hits default in the switch. If you give
 an argument that it doesn't know about it accepts it without doing anything
 and without an error, 

getopt() has already sanitised the arguments for you thus unknown 
options/switches do not make it into $options -- meaning you cannot test for 
them! Try print_r($options) and enter differing combinations of 
options/switches and see what you get.

 same thing if you don't give a value for an argument
 thats supposed to have one (like -a above).

That you have to test for yourself. In your example above if $A is empty then 
inform the user.

 Oh and --long arguments would
 be nice too.

Yes and I'll have fries with that as well please ;-)

 So why not use the Console/Getopt.php library you ask. Because i can't
 figure out how to parse the output array. I can figure out the first part

 ?
 require 'Console/Getopt.php';

 $optclass =  new Console_Getopt;
 $args = $optclass-readPHPArgv();
 array_shift($args);

 $shortoptions = a:bc;
 $longoptions = array(long, alsolong=, doublelong==);

 $options = $optclass-getopt( $args, $shortoptions, $longoptions);
 print_r($options);
 ?

 But that returns an array with more then one dimension and i have no idea
 how to loop though to set variables like i did with getopt().

Ok, here $options consists of 2 arrays, the first ($options[0]) is an array 
containing all the valid options. To read them you can use:

  foreach ($options[0] as $opt) {
echo Option::{$opt[0]}   Value::{$opt[1]}\n;
  }

 My questions are what is the preferred way to get command line options,
 is there a way to give errors 

Yes, you manually check for them!

 and take long arguments with getopt(),

Doesn't look it.

The second array ($options[1]) just contains the rest of your command line.

Using Console_Getopt if an unknown option/switch is given a PEAR_Error is 
raised thus you have to check/trap for that.

-- 
Jason Wong - Gremlins Associates - www.gremlins.biz
Open Source Software Systems Integrators
* Web Design  Hosting * Internet  Intranet Applications Development *

/*
San Francisco isn't what it used to be, and it never was.
-- Herb Caen
*/


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




[PHP] getopt usage?

2003-01-15 Thread Eli Criffield

I can't seem to find any examples of code using getopt() around. I'm thinking not many 
people use php for command line shell scripts.

Here is what i came up with for getopt(); 

?
$options = getopt(a:bc);

reset($options);
while(list($key, $value) = each($options)) {
switch ($key) {
case 'b':
$B=1;
break;
case 'a':
$A=$value;
break;
case 'c':
$C=1;
break;
default:
echo unknown option;
// print usage or something
break;
}
}
?

The problem with this is it never hits default in the switch. If you give
an argument that it doesn't know about it accepts it without doing anything
and without an error, same thing if you don't give a value for an argument
thats supposed to have one (like -a above). Oh and --long arguments would be
nice too.

So why not use the Console/Getopt.php library you ask. Because i can't
figure out how to parse the output array. I can figure out the first part

?
require 'Console/Getopt.php';

$optclass =  new Console_Getopt;
$args = $optclass-readPHPArgv();
array_shift($args);

$shortoptions = a:bc;
$longoptions = array(long, alsolong=, doublelong==);

$options = $optclass-getopt( $args, $shortoptions, $longoptions);
print_r($options);
?

But that returns an array with more then one dimension and i have no idea
how to loop though to set variables like i did with getopt().

My questions are what is the preferred way to get command line options, 
is there a way to give errors and take long arguments with getopt(), 
and how would one parse though the array returned from $optclass-getopt? 

Code examples of anyone using getopt(); or Getopt.php would be greatly
appreciated.

thanks

eli

PS anyone ever thought of setting up a website and maybe mailing list
devoted to php as a shell scripting language (not web scripting)

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php