Zend_Console_Getopt-Configuration.xml 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.console.getopt.configuration">
  4. <title>Configuring Zend_Console_Getopt</title>
  5. <sect2 id="zend.console.getopt.configuration.addrules">
  6. <title>Adding Option Rules</title>
  7. <para>
  8. You can add more option rules in addition to those you specified
  9. in the <classname>Zend_Console_Getopt</classname> constructor, using the
  10. <methodname>addRules()</methodname> method. The argument to
  11. <methodname>addRules()</methodname> is the same as the first argument to the
  12. class constructor. It is either a string in the format of the
  13. short syntax options specification, or else an associative array
  14. in the format of a long syntax options specification.
  15. See <link linkend="zend.console.getopt.rules">Declaring Getopt Rules</link>
  16. for details on the syntax for specifying options.
  17. </para>
  18. <example id="zend.console.getopt.configuration.addrules.example">
  19. <title>Using addRules()</title>
  20. <programlisting language="php"><![CDATA[
  21. $opts = new Zend_Console_Getopt('abp:');
  22. $opts->addRules(
  23. array(
  24. 'verbose|v' => 'Print verbose output'
  25. )
  26. );
  27. ]]></programlisting>
  28. </example>
  29. <para>
  30. The example above shows adding the <command>--verbose</command> option
  31. with an alias of <command>-v</command> to a set of options
  32. defined in the call to the constructor. Notice that you can mix
  33. short format options and long format options in the same instance
  34. of <classname>Zend_Console_Getopt</classname>.
  35. </para>
  36. </sect2>
  37. <sect2 id="zend.console.getopt.configuration.addhelp">
  38. <title>Adding Help Messages</title>
  39. <para>
  40. In addition to specifying the help strings when declaring option
  41. rules in the long format, you can associate help strings
  42. with option rules using the <methodname>setHelp()</methodname>
  43. method. The argument to the <methodname>setHelp()</methodname> method is an
  44. associative array, in which the key is a flag, and the value is a
  45. corresponding help string.
  46. </para>
  47. <example id="zend.console.getopt.configuration.addhelp.example">
  48. <title>Using setHelp()</title>
  49. <programlisting language="php"><![CDATA[
  50. $opts = new Zend_Console_Getopt('abp:');
  51. $opts->setHelp(
  52. array(
  53. 'a' => 'apple option, with no parameter',
  54. 'b' => 'banana option, with required integer parameter',
  55. 'p' => 'pear option, with optional string parameter'
  56. )
  57. );
  58. ]]></programlisting>
  59. </example>
  60. <para>
  61. If you declared options with aliases, you can use any of the
  62. aliases as the key of the associative array.
  63. </para>
  64. <para>
  65. The <methodname>setHelp()</methodname> method is the only way to define help
  66. strings if you declared the options using the short syntax.
  67. </para>
  68. </sect2>
  69. <sect2 id="zend.console.getopt.configuration.addaliases">
  70. <title>Adding Option Aliases</title>
  71. <para>
  72. You can declare aliases for options using the <methodname>setAliases()</methodname>
  73. method. The argument is an associative array, whose key is
  74. a flag string declared previously, and whose value is a new
  75. alias for that flag. These aliases are merged with any existing
  76. aliases. In other words, aliases you declared earlier are
  77. still in effect.
  78. </para>
  79. <para>
  80. An alias may be declared only once. If you try to redefine
  81. an alias, a <classname>Zend_Console_Getopt_Exception</classname> is thrown.
  82. </para>
  83. <example id="zend.console.getopt.configuration.addaliases.example">
  84. <title>Using setAliases()</title>
  85. <programlisting language="php"><![CDATA[
  86. $opts = new Zend_Console_Getopt('abp:');
  87. $opts->setAliases(
  88. array(
  89. 'a' => 'apple',
  90. 'a' => 'apfel',
  91. 'p' => 'pear'
  92. )
  93. );
  94. ]]></programlisting>
  95. </example>
  96. <para>
  97. In the example above, after declaring these aliases,
  98. <command>-a</command>, <command>--apple</command> and
  99. <command>--apfel</command> are aliases for each other.
  100. Also <command>-p</command> and <command>--pear</command> are aliases
  101. for each other.
  102. </para>
  103. <para>
  104. The <methodname>setAliases()</methodname> method is the only way to define aliases
  105. if you declared the options using the short syntax.
  106. </para>
  107. </sect2>
  108. <sect2 id="zend.console.getopt.configuration.addargs">
  109. <title>Adding Argument Lists</title>
  110. <para>
  111. By default, <classname>Zend_Console_Getopt</classname> uses
  112. <varname>$_SERVER['argv']</varname> for the array of command-line
  113. arguments to parse. You can alternatively specify the array of
  114. arguments as the second constructor argument. Finally, you
  115. can append more arguments to those already used using the
  116. <methodname>addArguments()</methodname> method, or you can replace the current
  117. array of arguments using the <methodname>setArguments()</methodname> method.
  118. In both cases, the parameter to these methods is a simple array of
  119. strings. The former method appends the array to the current
  120. arguments, and the latter method substitutes the array for the
  121. current arguments.
  122. </para>
  123. <example id="zend.console.getopt.configuration.addargs.example">
  124. <title>Using addArguments() and setArguments()</title>
  125. <programlisting language="php"><![CDATA[
  126. // By default, the constructor uses $_SERVER['argv']
  127. $opts = new Zend_Console_Getopt('abp:');
  128. // Append an array to the existing arguments
  129. $opts->addArguments(array('-a', '-p', 'p_parameter', 'non_option_arg'));
  130. // Substitute a new array for the existing arguments
  131. $opts->setArguments(array('-a', '-p', 'p_parameter', 'non_option_arg'));
  132. ]]></programlisting>
  133. </example>
  134. </sect2>
  135. <sect2 id="zend.console.getopt.configuration.config">
  136. <title>Adding Configuration</title>
  137. <para>
  138. The third parameter to the <classname>Zend_Console_Getopt</classname>
  139. constructor is an array of configuration options that affect
  140. the behavior of the object instance returned. You can also
  141. specify configuration options using the <methodname>setOptions()</methodname>
  142. method, or you can set an individual option using the
  143. <methodname>setOption()</methodname> method.
  144. </para>
  145. <note>
  146. <title>Clarifying the Term "option"</title>
  147. <para>
  148. The term "option" is used for configuration of the
  149. <classname>Zend_Console_Getopt</classname> class to match terminology
  150. used elsewhere in Zend Framework. These are not the same
  151. things as the command-line options that are parsed by
  152. the <classname>Zend_Console_Getopt</classname> class.
  153. </para>
  154. </note>
  155. <para>
  156. The currently supported
  157. options have const definitions in the class. The options,
  158. their const identifiers (with literal values in parentheses)
  159. are listed below:
  160. </para>
  161. <itemizedlist>
  162. <listitem>
  163. <para>
  164. <constant>Zend_Console_Getopt::CONFIG_DASHDASH</constant> ("dashDash"),
  165. if <constant>TRUE</constant>, enables the special flag <command>--</command> to
  166. signify the end of flags. Command-line arguments following
  167. the double-dash signifier are not interpreted as options,
  168. even if the arguments start with a dash. This configuration
  169. option is <constant>TRUE</constant> by default.
  170. </para>
  171. </listitem>
  172. <listitem>
  173. <para>
  174. <constant>Zend_Console_Getopt::CONFIG_IGNORECASE</constant> ("ignoreCase"),
  175. if <constant>TRUE</constant>, makes flags aliases of each other if they differ
  176. only in their case. That is, <command>-a</command> and
  177. <command>-A</command> will be considered to be synonymous flags.
  178. This configuration option is <constant>FALSE</constant> by default.
  179. </para>
  180. </listitem>
  181. <listitem>
  182. <para>
  183. <constant>Zend_Console_Getopt::CONFIG_RULEMODE</constant>
  184. ("ruleMode") may have values
  185. <constant>Zend_Console_Getopt::MODE_ZEND</constant> ("zend") and
  186. <constant>Zend_Console_Getopt::MODE_GNU</constant> ("gnu"). It should not be
  187. necessary to use this option unless you extend the class with additional syntax
  188. forms. The two modes supported in the base
  189. <classname>Zend_Console_Getopt</classname> class are unambiguous. If the
  190. specifier is a string, the class assumes <constant>MODE_GNU</constant>,
  191. otherwise it assumes <constant>MODE_ZEND</constant>. But if you extend the
  192. class and add more syntax forms, you may need to specify the mode
  193. using this option.
  194. </para>
  195. </listitem>
  196. </itemizedlist>
  197. <para>
  198. More configuration options may be added as future enhancements
  199. of this class.
  200. </para>
  201. <para>
  202. The two arguments to the <methodname>setOption()</methodname> method are
  203. a configuration option name and an option value.
  204. </para>
  205. <example id="zend.console.getopt.configuration.config.example.setoption">
  206. <title>Using setOption()</title>
  207. <programlisting language="php"><![CDATA[
  208. $opts = new Zend_Console_Getopt('abp:');
  209. $opts->setOption('ignoreCase', true);
  210. ]]></programlisting>
  211. </example>
  212. <para>
  213. The argument to the <methodname>setOptions()</methodname> method is
  214. an associative array. The keys of this array are the configuration
  215. option names, and the values are configuration values.
  216. This is also the array format used in the class constructor.
  217. The configuration values you specify are merged with the current
  218. configuration; you don't have to list all options.
  219. </para>
  220. <example id="zend.console.getopt.configuration.config.example.setoptions">
  221. <title>Using setOptions()</title>
  222. <programlisting language="php"><![CDATA[
  223. $opts = new Zend_Console_Getopt('abp:');
  224. $opts->setOptions(
  225. array(
  226. 'ignoreCase' => true,
  227. 'dashDash' => false
  228. )
  229. );
  230. ]]></programlisting>
  231. </example>
  232. </sect2>
  233. </sect1>