PageRenderTime 37ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/docs/languages/en/modules/zend.console.getopt.configuration.rst

https://github.com/gullij/zf2-documentation
ReStructuredText | 191 lines | 131 code | 60 blank | 0 comment | 0 complexity | 109a27574425bbe98aef120bbaa96f44 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. .. _zend.console.getopt.configuration:
  2. Configuring Zend\Console\Getopt
  3. ===============================
  4. .. _zend.console.getopt.configuration.addrules:
  5. Adding Option Rules
  6. -------------------
  7. You can add more option rules in addition to those you specified in the ``Zend\Console\Getopt`` constructor, using
  8. the ``addRules()`` method. The argument to ``addRules()`` is the same as the first argument to the class
  9. constructor. It is either a string in the format of the short syntax options specification, or else an associative
  10. array in the format of a long syntax options specification. See :ref:`Declaring Getopt Rules
  11. <zend.console.getopt.rules>` for details on the syntax for specifying options.
  12. .. _zend.console.getopt.configuration.addrules.example:
  13. .. rubric:: Using addRules()
  14. .. code-block:: php
  15. :linenos:
  16. $opts = new Zend\Console\Getopt('abp:');
  17. $opts->addRules(
  18. array(
  19. 'verbose|v' => 'Print verbose output'
  20. )
  21. );
  22. The example above shows adding the ``--verbose`` option with an alias of ``-v`` to a set of options defined in the
  23. call to the constructor. Notice that you can mix short format options and long format options in the same instance
  24. of ``Zend\Console\Getopt``.
  25. .. _zend.console.getopt.configuration.addhelp:
  26. Adding Help Messages
  27. --------------------
  28. In addition to specifying the help strings when declaring option rules in the long format, you can associate help
  29. strings with option rules using the ``setHelp()`` method. The argument to the ``setHelp()`` method is an
  30. associative array, in which the key is a flag, and the value is a corresponding help string.
  31. .. _zend.console.getopt.configuration.addhelp.example:
  32. .. rubric:: Using setHelp()
  33. .. code-block:: php
  34. :linenos:
  35. $opts = new Zend\Console\Getopt('abp:');
  36. $opts->setHelp(
  37. array(
  38. 'a' => 'apple option, with no parameter',
  39. 'b' => 'banana option, with required integer parameter',
  40. 'p' => 'pear option, with optional string parameter'
  41. )
  42. );
  43. If you declared options with aliases, you can use any of the aliases as the key of the associative array.
  44. The ``setHelp()`` method is the only way to define help strings if you declared the options using the short syntax.
  45. .. _zend.console.getopt.configuration.addaliases:
  46. Adding Option Aliases
  47. ---------------------
  48. You can declare aliases for options using the ``setAliases()`` method. The argument is an associative array, whose
  49. key is a flag string declared previously, and whose value is a new alias for that flag. These aliases are merged
  50. with any existing aliases. In other words, aliases you declared earlier are still in effect.
  51. An alias may be declared only once. If you try to redefine an alias, a ``Zend\Console\Getopt\Exception`` is thrown.
  52. .. _zend.console.getopt.configuration.addaliases.example:
  53. .. rubric:: Using setAliases()
  54. .. code-block:: php
  55. :linenos:
  56. $opts = new Zend\Console\Getopt('abp:');
  57. $opts->setAliases(
  58. array(
  59. 'a' => 'apple',
  60. 'a' => 'apfel',
  61. 'p' => 'pear'
  62. )
  63. );
  64. In the example above, after declaring these aliases, ``-a``, ``--apple`` and ``--apfel`` are aliases for each
  65. other. Also ``-p`` and ``--pear`` are aliases for each other.
  66. The ``setAliases()`` method is the only way to define aliases if you declared the options using the short syntax.
  67. .. _zend.console.getopt.configuration.addargs:
  68. Adding Argument Lists
  69. ---------------------
  70. By default, ``Zend\Console\Getopt`` uses ``$_SERVER['argv']`` for the array of command-line arguments to parse. You
  71. can alternatively specify the array of arguments as the second constructor argument. Finally, you can append more
  72. arguments to those already used using the ``addArguments()`` method, or you can replace the current array of
  73. arguments using the ``setArguments()`` method. In both cases, the parameter to these methods is a simple array of
  74. strings. The former method appends the array to the current arguments, and the latter method substitutes the array
  75. for the current arguments.
  76. .. _zend.console.getopt.configuration.addargs.example:
  77. .. rubric:: Using addArguments() and setArguments()
  78. .. code-block:: php
  79. :linenos:
  80. // By default, the constructor uses $_SERVER['argv']
  81. $opts = new Zend\Console\Getopt('abp:');
  82. // Append an array to the existing arguments
  83. $opts->addArguments(array('-a', '-p', 'p_parameter', 'non_option_arg'));
  84. // Substitute a new array for the existing arguments
  85. $opts->setArguments(array('-a', '-p', 'p_parameter', 'non_option_arg'));
  86. .. _zend.console.getopt.configuration.config:
  87. Adding Configuration
  88. --------------------
  89. The third parameter to the ``Zend\Console\Getopt`` constructor is an array of configuration options that affect the
  90. behavior of the object instance returned. You can also specify configuration options using the ``setOptions()``
  91. method, or you can set an individual option using the ``setOption()`` method.
  92. .. note::
  93. **Clarifying the Term "option"**
  94. The term "option" is used for configuration of the ``Zend\Console\Getopt`` class to match terminology used
  95. elsewhere in Zend Framework. These are not the same things as the command-line options that are parsed by the
  96. ``Zend\Console\Getopt`` class.
  97. The currently supported options have const definitions in the class. The options, their const identifiers (with
  98. literal values in parentheses) are listed below:
  99. - ``Zend\Console\Getopt::CONFIG_DASHDASH`` ("dashDash"), if ``TRUE``, enables the special flag ``--`` to signify
  100. the end of flags. Command-line arguments following the double-dash signifier are not interpreted as options, even
  101. if the arguments start with a dash. This configuration option is ``TRUE`` by default.
  102. - ``Zend\Console\Getopt::CONFIG_IGNORECASE`` ("ignoreCase"), if ``TRUE``, makes flags aliases of each other if they
  103. differ only in their case. That is, ``-a`` and ``-A`` will be considered to be synonymous flags. This
  104. configuration option is ``FALSE`` by default.
  105. - ``Zend\Console\Getopt::CONFIG_RULEMODE`` ("ruleMode") may have values ``Zend\Console\Getopt::MODE_ZEND`` ("zend")
  106. and ``Zend\Console\Getopt::MODE_GNU`` ("gnu"). It should not be necessary to use this option unless you extend
  107. the class with additional syntax forms. The two modes supported in the base ``Zend\Console\Getopt`` class are
  108. unambiguous. If the specifier is a string, the class assumes ``MODE_GNU``, otherwise it assumes ``MODE_ZEND``.
  109. But if you extend the class and add more syntax forms, you may need to specify the mode using this option.
  110. More configuration options may be added as future enhancements of this class.
  111. The two arguments to the ``setOption()`` method are a configuration option name and an option value.
  112. .. _zend.console.getopt.configuration.config.example.setoption:
  113. .. rubric:: Using setOption()
  114. .. code-block:: php
  115. :linenos:
  116. $opts = new Zend\Console\Getopt('abp:');
  117. $opts->setOption('ignoreCase', true);
  118. The argument to the ``setOptions()`` method is an associative array. The keys of this array are the configuration
  119. option names, and the values are configuration values. This is also the array format used in the class constructor.
  120. The configuration values you specify are merged with the current configuration; you don't have to list all options.
  121. .. _zend.console.getopt.configuration.config.example.setoptions:
  122. .. rubric:: Using setOptions()
  123. .. code-block:: php
  124. :linenos:
  125. $opts = new Zend\Console\Getopt('abp:');
  126. $opts->setOptions(
  127. array(
  128. 'ignoreCase' => true,
  129. 'dashDash' => false
  130. )
  131. );