1 <?xml version="1.0" encoding="UTF-8"?>
3 <sect1 id="zend.console.getopt.fetching">
4 <title>Fetching Options and Arguments</title>
7 After you have declared the options that the
8 <classname>Zend_Console_Getopt</classname> object should recognize, and supply
9 arguments from the command-line or an array, you can
10 query the object to find out which options were specified by a user in
11 a given command-line invocation of your program. The class implements
12 magic methods so you can query for options by name.
16 The parsing of the data is deferred until the first query you make
17 against the <classname>Zend_Console_Getopt</classname> object to find out if an
18 option was given, the object performs its parsing. This allows you to
19 use several method calls to configure the options, arguments, help
20 strings, and configuration options before parsing takes place.
23 <sect2 id="zend.console.getopt.fetching.exceptions">
24 <title>Handling Getopt Exceptions</title>
27 If the user gave any invalid options on the command-line,
28 the parsing function throws a <classname>Zend_Console_Getopt_Exception</classname>.
29 You should catch this exception in your application code.
30 You can use the <methodname>parse()</methodname> method to force the object
31 to parse the arguments. This is useful because you can invoke
32 <methodname>parse()</methodname> in a <emphasis>try</emphasis> block. If it passes,
33 you can be sure that the parsing won't throw an exception again.
34 The exception thrown has a custom method <methodname>getUsageMessage()</methodname>,
35 which returns as a string the formatted set of usage messages for
39 <example id="zend.console.getopt.fetching.exceptions.example">
40 <title>Catching Getopt Exceptions</title>
42 <programlisting language="php"><![CDATA[
44 $opts = new Zend_Console_Getopt('abp:');
46 } catch (Zend_Console_Getopt_Exception $e) {
47 echo $e->getUsageMessage();
54 Cases where parsing throws an exception include:
60 Option given is not recognized.
66 Option requires a parameter but none was given.
72 Option parameter is of the wrong type.
73 E.g. a non-numeric string when an integer
80 <sect2 id="zend.console.getopt.fetching.byname">
81 <title>Fetching Options by Name</title>
84 You can use the <methodname>getOption()</methodname> method to query the value
85 of an option. If the option had a parameter, this method returns
86 the value of the parameter. If the option had no parameter but
87 the user did specify it on the command-line, the method returns
88 <constant>TRUE</constant>. Otherwise the method returns <constant>NULL</constant>.
91 <example id="zend.console.getopt.fetching.byname.example.setoption">
92 <title>Using getOption()</title>
94 <programlisting language="php"><![CDATA[
95 $opts = new Zend_Console_Getopt('abp:');
96 $b = $opts->getOption('b');
97 $p_parameter = $opts->getOption('p');
102 Alternatively, you can use the magic <methodname>__get()</methodname> function
103 to retrieve the value of an option as if it were a class member
104 variable. The <methodname>__isset()</methodname> magic method is also
108 <example id="zend.console.getopt.fetching.byname.example.magic">
109 <title>Using __get() and __isset() Magic Methods</title>
111 <programlisting language="php"><![CDATA[
112 $opts = new Zend_Console_Getopt('abp:');
113 if (isset($opts->b)) {
114 echo "I got the b option.\n";
116 $p_parameter = $opts->p; // null if not set
121 If your options are declared with aliases, you may use any of the
122 aliases for an option in the methods above.
126 <sect2 id="zend.console.getopt.fetching.reporting">
127 <title>Reporting Options</title>
130 There are several methods to report the full set of
131 options given by the user on the current command-line.
137 As a string: use the <methodname>toString()</methodname> method. The options
138 are returned as a space-separated string of <command>flag=value</command>
139 pairs. The value of an option that does not have a parameter
140 is the literal string "<constant>TRUE</constant>".
146 As an array: use the <methodname>toArray()</methodname> method. The options
147 are returned in a simple integer-indexed array of strings, the flag
148 strings followed by parameter strings, if any.
154 As a string containing <acronym>JSON</acronym> data: use the
155 <methodname>toJson()</methodname> method.
161 As a string containing <acronym>XML</acronym> data: use the
162 <methodname>toXml()</methodname> method.
168 In all of the above dumping methods, the flag string is the
169 first string in the corresponding list of aliases. For example,
170 if the option aliases were declared like <command>verbose|v</command>,
171 then the first string, <command>verbose</command>, is used as the
172 canonical name of the option. The name of the option flag does not
173 include any preceding dashes.
177 <sect2 id="zend.console.getopt.fetching.remainingargs">
178 <title>Fetching Non-option Arguments</title>
181 After option arguments and their parameters have been
182 parsed from the command-line, there may be additional arguments
183 remaining. You can query these arguments using the
184 <methodname>getRemainingArgs()</methodname> method. This method returns
185 an array of the strings that were not part of any options.
188 <example id="zend.console.getopt.fetching.remainingargs.example">
189 <title>Using getRemainingArgs()</title>
191 <programlisting language="php"><![CDATA[
192 $opts = new Zend_Console_Getopt('abp:');
193 $opts->setArguments(array('-p', 'p_parameter', 'filename'));
194 $args = $opts->getRemainingArgs(); // returns array('filename')
199 <classname>Zend_Console_Getopt</classname> supports the <acronym>GNU</acronym>
200 convention that an argument consisting of a double-dash signifies the end of
201 options. Any arguments following this signifier must be treated as
202 non-option arguments. This is useful if you might have a non-option
203 argument that begins with a dash.
204 For example: "<command>rm -- -filename-with-dash</command>".