PhpRiot
Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...


When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store

Declaring Options with the Long Syntax

A different syntax with more features is also available. This syntax allows you to specify aliases for flags, types of option parameters, and also help strings to describe usage to the user. Instead of the single string used in the short syntax to declare the options, the long syntax uses an associative array as the first argument to the constructor.

The key of each element of the associative array is a string with a format that names the flag, with any aliases, separated by the pipe symbol ("|"). Following this series of flag aliases, if the option requires a parameter, is an equals symbol ("=") with a letter that stands for the type of the parameter:

  • "=s" for a string parameter

  • "=w" for a word parameter (a string containing no whitespace)

  • "=i" for an integer parameter

If the parameter is optional, use a dash ("-") instead of the equals symbol.

The value of each element in the associative array is a help string to describe to a user how to use your program.

Example 127. Using the Long Syntax

<?php
$opts 
= new Zend_Console_Getopt(
  array(
    
'apple|a'    => 'apple option, with no parameter',
    
'banana|b=i' => 'banana option, with required integer parameter',
    
'pear|p-s'   => 'pear option, with optional string parameter'
  
)
);

In the example declaration above, there are three options. --apple and -a are aliases for each other, and the option takes no parameter. --banana and -b are aliases for each other, and the option takes a mandatory integer parameter. Finally, --pear and -p are aliases for each other, and the option may take an optional string parameter.

Zend Framework