Zend_Console_Getopt-Fetching.xml 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <sect1 id="zend.console.getopt.fetching">
  2. <title> 读取(Fetching)选项和参数 </title>
  3. <para>
  4. 在声明 <code>Zend_Console_Getopt</code> 对象认可的选项和从命令行或数组提供参数后,可以查询这个对象来找出哪个选项被用户在程序中给定的命令行调用中被指定。这个类实现魔术方法,所以可以用名字来查询选项。
  5. </para>
  6. <para>
  7. 数据的解析被延迟到第一个依靠 <code>Zend_Console_Getopt</code> 对象查询是否发现一个选项被给出,然后这个对象来执行解析。它允许在解析发生之前使用若干个方法调用来配置选项、参数、帮助内容和配置选项。
  8. </para>
  9. <sect2 id="zend.console.getopt.fetching.exceptions">
  10. <title> 操作 Getopt 异常 </title>
  11. <para>
  12. 如果用户在命令行中给出任何无效选项,解析函数抛出一个<code>Zend_Console_Getopt_Exception</code>。你应当在程序代码理捕捉这个异常。可以使用<code>parse()</code>方法强制对象来解析参数。这很有用,因为可以在<code>try</code> 块中调用<code>parse()</code>。如果通过,解析不再抛出异常。异常的抛出有一个定制的方法 <code>getUsageMessage()</code>,它作为字符串返回,这个字符串是所有被声明的选项的用法信息。
  13. </para>
  14. <example id="zend.console.getopt.fetching.exceptions.example">
  15. <title> 捕捉 Getopt 异常 </title>
  16. <programlisting role="php"><![CDATA[
  17. try {
  18. $opts = new Zend_Console_Getopt('abp:');
  19. $opts->parse();
  20. } catch (Zend_Console_Getopt_Exception $e) {
  21. echo $e->getUsageMessage();
  22. exit;
  23. }
  24. ]]>
  25. </programlisting>
  26. </example>
  27. <para>
  28. 解析抛出异常的情况包括:
  29. </para>
  30. <itemizedlist>
  31. <listitem>
  32. <para>
  33. 给出的选项不被认可。
  34. </para>
  35. </listitem>
  36. <listitem>
  37. <para>
  38. 选项需要参数,但没有给出。
  39. </para>
  40. </listitem>
  41. <listitem>
  42. <para>
  43. 选项参数类型错误。例如,当要求整数却给出一个非数字字符串。
  44. </para>
  45. </listitem>
  46. </itemizedlist>
  47. </sect2>
  48. <sect2 id="zend.console.getopt.fetching.byname">
  49. <title> 通过名字读取 (Fetching)选项 </title>
  50. <para>
  51. 可以使用 <code>getOption()</code> 方法来查询选项的值。如果选项有一个参数,这个方法返回参数的值。如果选项不带参数但用户的确在命令行中指定了,这个方法返回<code>true</code>,否则,返回<code>null</code>。
  52. </para>
  53. <example id="zend.console.getopt.fetching.byname.example.setoption">
  54. <title> 使用 getOption()</title>
  55. <programlisting role="php"><![CDATA[
  56. $opts = new Zend_Console_Getopt('abp:');
  57. $b = $opts->getOption('b');
  58. $p_parameter = $opts->getOption('p');
  59. ]]>
  60. </programlisting>
  61. </example>
  62. <para>
  63. 另外,可以使用魔术函数 <code>__get()</code> 来获取选项的值,好像它是类成员变量。 <code>__isset()</code> 魔术方法也可以实现。
  64. </para>
  65. <example id="zend.console.getopt.fetching.byname.example.magic">
  66. <title> 使用 __get() 和 __isset() 魔术方法 </title>
  67. <programlisting role="php"><![CDATA[
  68. $opts = new Zend_Console_Getopt('abp:');
  69. if (isset($opts->b)) {
  70. echo "I got the b option.\n";
  71. }
  72. $p_parameter = $opts->p; // null if not set
  73. ]]>
  74. </programlisting>
  75. </example>
  76. <para>
  77. 如果选项被带有别名声明,在上面的方法中可以使用任何别名。
  78. </para>
  79. </sect2>
  80. <sect2 id="zend.console.getopt.fetching.reporting">
  81. <title> 报告选项 </title>
  82. <para>
  83. 有若干方法来报告由用户在当前命令行给出的选项的全集。
  84. </para>
  85. <itemizedlist>
  86. <listitem>
  87. <para>
  88. 作为字符串:使用<code>toString()</code>方法。选项被返回为用空格分隔的"<code>flag=value</code>"对的字符串。没有参数的选项值是字面上的"<code>true</code>"。
  89. </para>
  90. </listitem>
  91. <listitem>
  92. <para>
  93. 作为数组:使用<code>toArray()</code>方法。选项被返回在一个简单的整数索引的字符串数组,flag 字符串在参数字符串之后,如果有的话。
  94. </para>
  95. </listitem>
  96. <listitem>
  97. <para>
  98. 作为包含JSON数据的字符串:使用 <code>toJson()</code> 方法。
  99. </para>
  100. </listitem>
  101. <listitem>
  102. <para>
  103. 作为包含 XML 数据的字符串: 使用 <code>toXml()</code> 方法。
  104. </para>
  105. </listitem>
  106. </itemizedlist>
  107. <para>
  108. 在上述所有的方法中,flag 字符串是对应于别名列表中的第一个字符串。例如:如果选项别名被声明如"<code>verbose|v</code>",那么第一个字符串,"<code>verbose</code>",被用作选项的规范名称。选项flag的名称不包括任何前面所述的短横线。
  109. </para>
  110. </sect2>
  111. <sect2 id="zend.console.getopt.fetching.remainingargs">
  112. <title> 读取非选项参数 </title>
  113. <para>
  114. 在选项参数和它们的参数从命令行中解析后,可能还有另外的参数剩余。可以使用<code>getRemainingArgs()</code>方法来查询这些参数。这个方法返回一个不属于任何选项的字符串数组。
  115. </para>
  116. <example id="zend.console.getopt.fetching.remainingargs.example">
  117. <title> 使用 getRemainingArgs()</title>
  118. <programlisting role="php"><![CDATA[
  119. $opts = new Zend_Console_Getopt('abp:');
  120. $opts->setArguments(array('-p', 'p_parameter', 'filename'));
  121. $args = $opts->getRemainingArgs(); // returns array('filename')
  122. ]]>
  123. </programlisting>
  124. </example>
  125. <para>
  126. <code>Zend_Console_Getopt</code>支持 GNU 惯例,在参数中包含双短横线表示选项的结尾。在这个符号后面的任何参数必须当作非选项参数。如果有以一个短横线开头的非选项参数,这很有用。例如:"<command>rm -- -filename-with-dash</command>"。
  127. </para>
  128. </sect2>
  129. </sect1>