PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://github.com/Xerkus/zf2-documentation
ReStructuredText | 89 lines | 59 code | 30 blank | 0 comment | 0 complexity | 28f715db1ef91dce09ad44d0730f74b7 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. .. _zend.console.getopt.rules:
  2. Declaring Getopt Rules
  3. ======================
  4. The constructor for the ``Zend\Console\Getopt`` class takes from one to three arguments. The first argument
  5. declares which options are supported by your application. This class supports alternative syntax forms for
  6. declaring the options. See the sections below for the format and usage of these syntax forms.
  7. The constructor takes two more arguments, both of which are optional. The second argument may contain the
  8. command-line arguments. This defaults to ``$_SERVER['argv']``.
  9. The third argument of the constructor may contain an configuration options to customize the behavior of
  10. ``Zend\Console\Getopt``. See :ref:`Adding Configuration <zend.console.getopt.configuration.config>` for reference
  11. on the options available.
  12. .. _zend.console.getopt.rules.short:
  13. Declaring Options with the Short Syntax
  14. ---------------------------------------
  15. ``Zend\Console\Getopt`` supports a compact syntax similar to that used by *GNU* Getopt (see
  16. http://www.gnu.org/software/libc/manual/html_node/Getopt.html. This syntax supports only single-character flags.
  17. In a single string, you type each of the letters that correspond to flags supported by your application. A letter
  18. followed by a colon character (**:**) indicates a flag that requires a parameter.
  19. .. _zend.console.getopt.rules.short.example:
  20. Using the Short Syntax
  21. ^^^^^^^^^^^^^^^^^^^^^^
  22. .. code-block:: php
  23. :linenos:
  24. $opts = new Zend\Console\Getopt('abp:');
  25. The example above shows using ``Zend\Console\Getopt`` to declare that options may be given as ``-a``, ``-b``, or
  26. ``-p``. The latter flag requires a parameter.
  27. The short syntax is limited to flags of a single character. Aliases, parameter types, and help strings are not
  28. supported in the short syntax.
  29. .. _zend.console.getopt.rules.long:
  30. Declaring Options with the Long Syntax
  31. --------------------------------------
  32. A different syntax with more features is also available. This syntax allows you to specify aliases for flags, types
  33. of option parameters, and also help strings to describe usage to the user. Instead of the single string used in the
  34. short syntax to declare the options, the long syntax uses an associative array as the first argument to the
  35. constructor.
  36. The key of each element of the associative array is a string with a format that names the flag, with any aliases,
  37. separated by the pipe symbol ("**|**"). Following this series of flag aliases, if the option requires a parameter,
  38. is an equals symbol ("**=**") with a letter that stands for the **type** of the parameter:
  39. - "**=s**" for a string parameter
  40. - "**=w**" for a word parameter (a string containing no whitespace)
  41. - "**=i**" for an integer parameter
  42. If the parameter is optional, use a dash ("**-**") instead of the equals symbol.
  43. The value of each element in the associative array is a help string to describe to a user how to use your program.
  44. .. _zend.console.getopt.rules.long.example:
  45. Using the Long Syntax
  46. ^^^^^^^^^^^^^^^^^^^^^
  47. .. code-block:: php
  48. :linenos:
  49. $opts = new Zend\Console\Getopt(
  50. array(
  51. 'apple|a' => 'apple option, with no parameter',
  52. 'banana|b=i' => 'banana option, with required integer parameter',
  53. 'pear|p-s' => 'pear option, with optional string parameter'
  54. )
  55. );
  56. In the example declaration above, there are three options. ``--apple`` and ``-a`` are aliases for each other, and
  57. the option takes no parameter. ``--banana`` and ``-b`` are aliases for each other, and the option takes a mandatory
  58. integer parameter. Finally, ``--pear`` and ``-p`` are aliases for each other, and the option may take an optional
  59. string parameter.