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