PageRenderTime 48ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

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

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