Zend_Console_Getopt-Fetching.xml 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.console.getopt.fetching">
  4. <title>Fetching Options and Arguments</title>
  5. <para>
  6. After you have declared the options that the
  7. <classname>Zend_Console_Getopt</classname> object should recognize, and supply
  8. arguments from the command-line or an array, you can
  9. query the object to find out which options were specified by a user in
  10. a given command-line invocation of your program. The class implements
  11. magic methods so you can query for options by name.
  12. </para>
  13. <para>
  14. The parsing of the data is deferred until the first query you make
  15. against the <classname>Zend_Console_Getopt</classname> object to find out if an
  16. option was given, the object performs its parsing. This allows you to
  17. use several method calls to configure the options, arguments, help
  18. strings, and configuration options before parsing takes place.
  19. </para>
  20. <sect2 id="zend.console.getopt.fetching.exceptions">
  21. <title>Handling Getopt Exceptions</title>
  22. <para>
  23. If the user gave any invalid options on the command-line,
  24. the parsing function throws a <classname>Zend_Console_Getopt_Exception</classname>.
  25. You should catch this exception in your application code.
  26. You can use the <methodname>parse()</methodname> method to force the object
  27. to parse the arguments. This is useful because you can invoke
  28. <methodname>parse()</methodname> in a <emphasis>try</emphasis> block. If it passes,
  29. you can be sure that the parsing won't throw an exception again.
  30. The exception thrown has a custom method <methodname>getUsageMessage()</methodname>,
  31. which returns as a string the formatted set of usage messages for
  32. all declared options.
  33. </para>
  34. <example id="zend.console.getopt.fetching.exceptions.example">
  35. <title>Catching Getopt Exceptions</title>
  36. <programlisting language="php"><![CDATA[
  37. try {
  38. $opts = new Zend_Console_Getopt('abp:');
  39. $opts->parse();
  40. } catch (Zend_Console_Getopt_Exception $e) {
  41. echo $e->getUsageMessage();
  42. exit;
  43. }
  44. ]]></programlisting>
  45. </example>
  46. <para>
  47. Cases where parsing throws an exception include:
  48. </para>
  49. <itemizedlist>
  50. <listitem>
  51. <para>
  52. Option given is not recognized.
  53. </para>
  54. </listitem>
  55. <listitem>
  56. <para>
  57. Option requires a parameter but none was given.
  58. </para>
  59. </listitem>
  60. <listitem>
  61. <para>
  62. Option parameter is of the wrong type.
  63. E.g. a non-numeric string when an integer
  64. was required.
  65. </para>
  66. </listitem>
  67. </itemizedlist>
  68. </sect2>
  69. <sect2 id="zend.console.getopt.fetching.byname">
  70. <title>Fetching Options by Name</title>
  71. <para>
  72. You can use the <methodname>getOption()</methodname> method to query the value
  73. of an option. If the option had a parameter, this method returns
  74. the value of the parameter. If the option had no parameter but
  75. the user did specify it on the command-line, the method returns
  76. <constant>TRUE</constant>. Otherwise the method returns <constant>NULL</constant>.
  77. </para>
  78. <example id="zend.console.getopt.fetching.byname.example.setoption">
  79. <title>Using getOption()</title>
  80. <programlisting language="php"><![CDATA[
  81. $opts = new Zend_Console_Getopt('abp:');
  82. $b = $opts->getOption('b');
  83. $p_parameter = $opts->getOption('p');
  84. ]]></programlisting>
  85. </example>
  86. <para>
  87. Alternatively, you can use the magic <methodname>__get()</methodname> function
  88. to retrieve the value of an option as if it were a class member
  89. variable. The <methodname>__isset()</methodname> magic method is also
  90. implemented.
  91. </para>
  92. <example id="zend.console.getopt.fetching.byname.example.magic">
  93. <title>Using __get() and __isset() Magic Methods</title>
  94. <programlisting language="php"><![CDATA[
  95. $opts = new Zend_Console_Getopt('abp:');
  96. if (isset($opts->b)) {
  97. echo "I got the b option.\n";
  98. }
  99. $p_parameter = $opts->p; // null if not set
  100. ]]></programlisting>
  101. </example>
  102. <para>
  103. If your options are declared with aliases, you may use any of the
  104. aliases for an option in the methods above.
  105. </para>
  106. </sect2>
  107. <sect2 id="zend.console.getopt.fetching.reporting">
  108. <title>Reporting Options</title>
  109. <para>
  110. There are several methods to report the full set of
  111. options given by the user on the current command-line.
  112. </para>
  113. <itemizedlist>
  114. <listitem>
  115. <para>
  116. As a string: use the <methodname>toString()</methodname> method. The options
  117. are returned as a space-separated string of <command>flag=value</command>
  118. pairs. The value of an option that does not have a parameter
  119. is the literal string "<constant>TRUE</constant>".
  120. </para>
  121. </listitem>
  122. <listitem>
  123. <para>
  124. As an array: use the <methodname>toArray()</methodname> method. The options
  125. are returned in a simple integer-indexed array of strings, the flag
  126. strings followed by parameter strings, if any.
  127. </para>
  128. </listitem>
  129. <listitem>
  130. <para>
  131. As a string containing <acronym>JSON</acronym> data: use the
  132. <methodname>toJson()</methodname> method.
  133. </para>
  134. </listitem>
  135. <listitem>
  136. <para>
  137. As a string containing <acronym>XML</acronym> data: use the
  138. <methodname>toXml()</methodname> method.
  139. </para>
  140. </listitem>
  141. </itemizedlist>
  142. <para>
  143. In all of the above dumping methods, the flag string is the
  144. first string in the corresponding list of aliases. For example,
  145. if the option aliases were declared like <command>verbose|v</command>,
  146. then the first string, <command>verbose</command>, is used as the
  147. canonical name of the option. The name of the option flag does not
  148. include any preceding dashes.
  149. </para>
  150. </sect2>
  151. <sect2 id="zend.console.getopt.fetching.remainingargs">
  152. <title>Fetching Non-option Arguments</title>
  153. <para>
  154. After option arguments and their parameters have been
  155. parsed from the command-line, there may be additional arguments
  156. remaining. You can query these arguments using the
  157. <methodname>getRemainingArgs()</methodname> method. This method returns
  158. an array of the strings that were not part of any options.
  159. </para>
  160. <example id="zend.console.getopt.fetching.remainingargs.example">
  161. <title>Using getRemainingArgs()</title>
  162. <programlisting language="php"><![CDATA[
  163. $opts = new Zend_Console_Getopt('abp:');
  164. $opts->setArguments(array('-p', 'p_parameter', 'filename'));
  165. $args = $opts->getRemainingArgs(); // returns array('filename')
  166. ]]></programlisting>
  167. </example>
  168. <para>
  169. <classname>Zend_Console_Getopt</classname> supports the <acronym>GNU</acronym>
  170. convention that an argument consisting of a double-dash signifies the end of
  171. options. Any arguments following this signifier must be treated as
  172. non-option arguments. This is useful if you might have a non-option
  173. argument that begins with a dash.
  174. For example: "<command>rm -- -filename-with-dash</command>".
  175. </para>
  176. </sect2>
  177. </sect1>