Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function split() is deprecated in /home3/vvaswani/public_html/community/columns/trog/article.php on line 101

Deprecated: Function eregi_replace() is deprecated in /home3/vvaswani/sql.php on line 301

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home3/vvaswani/public_html/community/columns/trog/article.php on line 107

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home3/vvaswani/public_html/community/columns/trog/article.php on line 109

Deprecated: Function ereg_replace() is deprecated in /home3/vvaswani/public_html/community/columns/trog/article.php on line 124

Deprecated: Function ereg_replace() is deprecated in /home3/vvaswani/public_html/community/columns/trog/article.php on line 125

Deprecated: Function split() is deprecated in /home3/vvaswani/public_html/community/columns/trog/article.php on line 127

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174
The Melonfire Community - Trog
Logo         Community
  Trog
Services
The Company
Community
Columns
Your Account
Contact Us
 
 
Using PEAR Console_Getopt To Process The PHP Command Line
Learn to process command-line options in your PHP script

| Pepperoni Or Cheese? |

To see how Console_Getopt works, copy and paste the following PHP script and save it as pizza.php:


<?php

// include class file
include ("Console/Getopt.php");

// initialize object
$cg = new Console_Getopt();

/* define list of allowed options - p = pepperoni  t = tomato  o = olives  a = anchovies  h = ham */
$allowedShortOptions = "ptoah";

// read the command line
$args = $cg->readPHPArgv();

// get the options
$ret = $cg->getopt($args, $allowedShortOptions);

// check for errors and die with an error message if there was a problem
if (PEAR::isError($ret)) {
    die ("Error in command line: " . $ret->getMessage() . "\n");
}

// display the options
print_r($ret);

?>


How does this work? It's quite simple: the first step is to create a new Console_Getopt() object, and define a list of valid command-line arguments for the program.


<?php

include ("Console/Getopt.php");

$cg = new Console_Getopt();

$allowedShortOptions = "ptoah";

?>


In this example, the program can be invoked with any or all of the options -p, -a, -t, -o and -h. I'm assuming only short options here, but you could define long options too - I'll get to that shortly. Note that the order of options in the $allowedShortOptions variable is not important.

Once the option list is defined, the Console_Getopt() object's readPHPArgv() method retrieves the command line which was used to invoke the program, by looking in the special $_SERVER['argv'] or (for back compatibility) $HTTP_SERVER_VARS['argv'] arrays.


<?php

$args = $cg->readPHPArgv();

?>


Next, the object's getopt() method parses the command line and matches it against the list of valid options defined earlier (this list will be passed to getopt() as the second argument).


<?php

$ret = $cg->getopt($args, $allowedShortOptions);

?>


In the event of an error - for example, if an invalid option is specified on the command line - a PEAR::Error() object is returned, and can be captured by the script:


<?php

if (PEAR::isError($ret)) {
    die ("Error in command line: " . $ret->getMessage() . "\n");
}

?>


If no error occurs, getopt() returns an array containing a list of the options passed, as well as a list of non-option arguments. To see what this array contains, scroll on down.


How to do Everything with PHP & MySQL
How to do Everything with PHP & MySQL, the best-selling book by Melonfire, explains how to take full advantage of PHP's built-in support for MySQL and link the results of database queries to Web pages. You'll get full details on PHP programming and MySQL database development, and then you'll learn to use these two cutting-edge technologies together. Easy-to-follow sample applications include a PHP online shopping cart, a MySQL order tracking system, and a PHP/MySQL news publishing system.

Read more, or grab your copy now!


previous page more like this  print this article  next page
 
Search...
 
In trog...
Logging With PHP
Building A Quick-And-Dirty PHP/MySQL Publishing System
Output Buffering With PHP
Date/Time Processing With PHP
Creating Web Calendars With The PEAR Calendar Class
more...
 
In the hitg report...
Crime Scenes
Animal Attraction
Lord Of The Strings
more...
 
In boombox...
Patience - George Michael
Think Tank - Blur
My Private Nation - Train
more...
 
In colophon...
Hostage - Robert Crais
The Dead Heart - Douglas Kennedy
Right As Rain - George Pelecanos
more...
 
In cut!...
American Chai
The Core
Possession
more...
 
Find out how you can use this article on your own Web site!


Copyright © 1998-
Strict Standards: mktime(): You should be using the time() function instead in /home3/vvaswani/public_html/community/columns/trog/article.php on line 731
2018 Melonfire. All rights reserved
Terms and Conditions | Feedback