Zend_Console_Getopt-Rules.xml 5.5 KB

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