Zend Console – get values passed

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(
    array(
        '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

or

> 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

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>