When using a CLI script with options, I like to use the Zend_Console.

If my options are as follows:

$console = new Zend_Console_Getopt(
        'i-s'  => 'test option 1',
        'e'    => 'test option 2')

For instance,

> php myScript.php -i OPTION

To get the “OPTION” string for the -i parameter

$arg = $console->getOption('i');

$arg will now contain the passed option, but if none was passed, it will be NULL

When outlining the available options that can be used with your script, you use the following syntax:

long|short  => description;
test|t => 'Test the script';

You can use,

> php myScript.php -t


> php myScript.php --test

to run the -test parameter. You can also pass values, such as an integer or a string:

'test|t=i' => 'Option with required integer parameter';
'test|t-s' => 'Option with optional string parameter'

For more information, refer to the Zend Documentation : http://framework.zend.com/manual/en/zend.console.getopt.introduction.html

By admin

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.