1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
|
<?xml version="1.0" encoding="UTF-8"?>
<!-- Reviewed: no -->
<sect1 id="zend.console.getopt.rules">
<title>Declaring Getopt Rules</title>
<para>
The constructor for the <classname>Zend_Console_Getopt</classname> class takes
from one to three arguments. The first argument
declares which options are supported by your application.
This class supports alternative syntax forms for declaring the options.
See the sections below for the format and usage of these syntax forms.
</para>
<para>
The constructor takes two more arguments, both of which are optional.
The second argument may contain the command-line arguments.
This defaults to <varname>$_SERVER['argv']</varname>.
</para>
<para>
The third argument of the constructor may contain an
configuration options to customize the behavior of
<classname>Zend_Console_Getopt</classname>.
See <link linkend="zend.console.getopt.configuration.config">Adding Configuration</link>
for reference on the options available.
</para>
<sect2 id="zend.console.getopt.rules.short">
<title>Declaring Options with the Short Syntax</title>
<para>
<classname>Zend_Console_Getopt</classname> supports a compact syntax similar
to that used by <acronym>GNU</acronym> Getopt (see <ulink
url="http://www.gnu.org/software/libc/manual/html_node/Getopt.html">http://www.gnu.org/software/libc/manual/html_node/Getopt.html</ulink>.
This syntax supports only single-character flags. In a single
string, you type each of the letters that correspond to flags
supported by your application. A letter followed by a colon
character (<emphasis>:</emphasis>) indicates a flag that requires a
parameter.
</para>
<example id="zend.console.getopt.rules.short.example">
<title>Using the Short Syntax</title>
<programlisting language="php"><![CDATA[
$opts = new Zend_Console_Getopt('abp:');
]]></programlisting>
</example>
<para>
The example above shows using <classname>Zend_Console_Getopt</classname>
to declare that options may be given as <command>-a</command>,
<command>-b</command>, or <command>-p</command>. The latter flag
requires a parameter.
</para>
<para>
The short syntax is limited to flags of a single character.
Aliases, parameter types, and help strings are not supported
in the short syntax.
</para>
</sect2>
<sect2 id="zend.console.getopt.rules.long">
<title>Declaring Options with the Long Syntax</title>
<para>
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.
</para>
<para>
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 ("<emphasis>|</emphasis>"). Following this series of flag
aliases, if the option requires a parameter, is an equals symbol
("<emphasis>=</emphasis>") with a letter that stands for the
<emphasis>type</emphasis> of the parameter:
</para>
<itemizedlist>
<listitem>
<para>
"<emphasis>=s</emphasis>" for a string parameter
</para>
</listitem>
<listitem>
<para>
"<emphasis>=w</emphasis>" for a word parameter
(a string containing no whitespace)
</para>
</listitem>
<listitem>
<para>
"<emphasis>=i</emphasis>" for an integer parameter
</para>
</listitem>
</itemizedlist>
<para>
If the parameter is optional, use a dash ("<emphasis>-</emphasis>")
instead of the equals symbol.
</para>
<para>
The value of each element in the associative array is a help string
to describe to a user how to use your program.
</para>
<example id="zend.console.getopt.rules.long.example">
<title>Using the Long Syntax</title>
<programlisting language="php"><![CDATA[
$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'
)
);
]]></programlisting>
</example>
<para>
In the example declaration above, there are three options.
<command>--apple</command> and <command>-a</command> are aliases for each
other, and the option takes no parameter.
<command>--banana</command> and <command>-b</command> are aliases for each
other, and the option takes a mandatory integer parameter.
Finally, <command>--pear</command> and <command>-p</command> are aliases
for each other, and the option may take an optional string parameter.
</para>
</sect2>
</sect1>
|