1 <?xml version="1.0" encoding="UTF-8"?>
3 <sect1 id="zend.console.getopt.rules">
4 <title>Declaring Getopt Rules</title>
7 The constructor for the <classname>Zend_Console_Getopt</classname> class takes
8 from one to three arguments. The first argument
9 declares which options are supported by your application.
10 This class supports alternative syntax forms for declaring the options.
11 See the sections below for the format and usage of these syntax forms.
15 The constructor takes two more arguments, both of which are optional.
16 The second argument may contain the command-line arguments.
17 This defaults to <varname>$_SERVER['argv']</varname>.
21 The third argument of the constructor may contain an
22 configuration options to customize the behavior of
23 <classname>Zend_Console_Getopt</classname>.
24 See <link linkend="zend.console.getopt.configuration.config">Adding Configuration</link>
25 for reference on the options available.
28 <sect2 id="zend.console.getopt.rules.short">
29 <title>Declaring Options with the Short Syntax</title>
32 <classname>Zend_Console_Getopt</classname> supports a compact syntax similar
33 to that used by <acronym>GNU</acronym> Getopt (see <ulink
34 url="http://www.gnu.org/software/libc/manual/html_node/Getopt.html">http://www.gnu.org/software/libc/manual/html_node/Getopt.html</ulink>.
35 This syntax supports only single-character flags. In a single
36 string, you type each of the letters that correspond to flags
37 supported by your application. A letter followed by a colon
38 character (<emphasis>:</emphasis>) indicates a flag that requires a
42 <example id="zend.console.getopt.rules.short.example">
43 <title>Using the Short Syntax</title>
45 <programlisting language="php"><![CDATA[
46 $opts = new Zend_Console_Getopt('abp:');
51 The example above shows using <classname>Zend_Console_Getopt</classname>
52 to declare that options may be given as <command>-a</command>,
53 <command>-b</command>, or <command>-p</command>. The latter flag
58 The short syntax is limited to flags of a single character.
59 Aliases, parameter types, and help strings are not supported
64 <sect2 id="zend.console.getopt.rules.long">
65 <title>Declaring Options with the Long Syntax</title>
68 A different syntax with more features is also available. This
69 syntax allows you to specify aliases for flags, types of option
70 parameters, and also help strings to describe usage to the user.
71 Instead of the single string used in the short syntax to declare
72 the options, the long syntax uses an associative array as the
73 first argument to the constructor.
77 The key of each element of the associative array is a string with
78 a format that names the flag, with any aliases, separated by the
79 pipe symbol ("<emphasis>|</emphasis>"). Following this series of flag
80 aliases, if the option requires a parameter, is an equals symbol
81 ("<emphasis>=</emphasis>") with a letter that stands for the
82 <emphasis>type</emphasis> of the parameter:
88 "<emphasis>=s</emphasis>" for a string parameter
94 "<emphasis>=w</emphasis>" for a word parameter
95 (a string containing no whitespace)
101 "<emphasis>=i</emphasis>" for an integer parameter
107 If the parameter is optional, use a dash ("<emphasis>-</emphasis>")
108 instead of the equals symbol.
112 The value of each element in the associative array is a help string
113 to describe to a user how to use your program.
116 <example id="zend.console.getopt.rules.long.example">
117 <title>Using the Long Syntax</title>
119 <programlisting language="php"><![CDATA[
120 $opts = new Zend_Console_Getopt(
122 'apple|a' => 'apple option, with no parameter',
123 'banana|b=i' => 'banana option, with required integer parameter',
124 'pear|p-s' => 'pear option, with optional string parameter'
131 In the example declaration above, there are three options.
132 <command>--apple</command> and <command>-a</command> are aliases for each
133 other, and the option takes no parameter.
134 <command>--banana</command> and <command>-b</command> are aliases for each
135 other, and the option takes a mandatory integer parameter.
136 Finally, <command>--pear</command> and <command>-p</command> are aliases
137 for each other, and the option may take an optional string parameter.