5
Mar

PHP-Commandline-Parameter with Console_Getopt

categories PHP, Tutorials    

Mainly due to historical reasons my guess is most developers are using php as a language to build web pages and web applications. PHP can do alot more: it has a commandline interface, which enables you to write scripts using several different enviroments.

Parameters are stored in a special variable $argv.

Example 1: test_argv.php

<?php
var_dump($argv);
?>

Output Example 1:

d:\php5> php.exe test_argv.php IchBinParam1 UndIch2
array(3) {
  [0]=>
  string(13) "test_argv.php"
  [1]=>
  string(12) "IchBinParam1"
  [2]=>
  string(7) "UndIch2"
}

Not too bad so far. If you now want to start parsing and checking parameters, such a structure can easily get complicated. To make things easier, there is a little class called Console_Getopt in the PEAR project.

Installation
If you’re using PEAR, simply use the pear installer.

d:\php5> pear install Console_Getopt-1.3.0

But you don’t have to. It is sufficent to download the class here and include it into your project.

Usage
Not only installing this class is easy, so is the usage of it. Just include the class into your programm, initialize the class object, define allowed options and then check the parameters.

Here’s a simple example.

Beispiel 2: demo_getopt.php

<?php
// include class
include ("Getopt.php");

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

/** 
 * allowed options:
 * -a Abba
 * -b Beatles
 * -q Queen
 */
$allowedOptions = "abq";

// read commandline
$args = $cg->readPHPArgv();

// filter options
$ret = $cg->getopt($args, $allowedOptions);

// error message
if (PEAR::isError($ret)) {
    die ("error in commandline: " . $ret->getMessage() . "\n");
}

// print the options
print_r($ret);
?> 

Output Example 2:

d:\temp\getopt>php.exe demo_getopt.php -ab Who Genesis
Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [0] => a
                    [1] =>
                )

            [1] => Array
                (
                    [0] => b
                    [1] =>
                )

        )

    [1] => Array
        (
            [0] => Who
            [1] => Genesis
        )

)

d:\temp\getopt>d:\php5\php.exe demo_getopt.php -c
Error in command line: Console_Getopt: unrecognized option -- c

After filtering the options you can now retrieve the command line parameters and work with them.

Example 3: demo_getopt_erweitert.php

.
.
.
// filter options
$ret = $cg->getopt($args, $allowedOptions);

$optionen = $ret[0];
if (sizeof($optionen) > 0) {
    // at least one option has been used
    foreach ($optionen as $o) {
        switch ($o[0]) {
            case 'a':
                echo "you choose: Abba\n"; 
                break;
            case 'b':
                echo "you choose: Beatles\n"; 
                break;
            case 'q':
                echo "you choose: Queen\n"; 
                break;
        }
    }
} 

The class can do a few things more, for example it is possible to check for optional or necessary parameters, check for strings or numeric values and things like this.

If you want to take a closer look into the class, it is well documented. A more detailed article with a few more examples can be found here at Zend.

Kommentare

Leave a comment!