Skip to content

Commit 288cdfd

Browse files
committed
Moved console arguments and options to seperate article
1 parent c76a32b commit 288cdfd

File tree

2 files changed

+187
-162
lines changed

2 files changed

+187
-162
lines changed

components/console.rst

Lines changed: 0 additions & 162 deletions
Original file line numberDiff line numberDiff line change
@@ -266,168 +266,6 @@ without actually printing.
266266
than wrapping your output calls in conditions. For an example use in
267267
the Symfony Framework, see :doc:`/logging/monolog_console`.
268268

269-
Using Command Arguments
270-
-----------------------
271-
272-
The most interesting part of the commands are the arguments and options that
273-
you can make available. Arguments are the strings - separated by spaces - that
274-
come after the command name itself. They are ordered, and can be optional
275-
or required. For example, add an optional ``last_name`` argument to the command
276-
and make the ``name`` argument required::
277-
278-
$this
279-
// ...
280-
->addArgument(
281-
'name',
282-
InputArgument::REQUIRED,
283-
'Who do you want to greet?'
284-
)
285-
->addArgument(
286-
'last_name',
287-
InputArgument::OPTIONAL,
288-
'Your last name?'
289-
);
290-
291-
You now have access to a ``last_name`` argument in your command::
292-
293-
if ($lastName = $input->getArgument('last_name')) {
294-
$text .= ' '.$lastName;
295-
}
296-
297-
The command can now be used in either of the following ways:
298-
299-
.. code-block:: bash
300-
301-
$ php application.php demo:greet Fabien
302-
$ php application.php demo:greet Fabien Potencier
303-
304-
It is also possible to let an argument take a list of values (imagine you want
305-
to greet all your friends). For this it must be specified at the end of the
306-
argument list::
307-
308-
$this
309-
// ...
310-
->addArgument(
311-
'names',
312-
InputArgument::IS_ARRAY,
313-
'Who do you want to greet (separate multiple names with a space)?'
314-
);
315-
316-
To use this, just specify as many names as you want:
317-
318-
.. code-block:: bash
319-
320-
$ php application.php demo:greet Fabien Ryan Bernhard
321-
322-
You can access the ``names`` argument as an array::
323-
324-
if ($names = $input->getArgument('names')) {
325-
$text .= ' '.implode(', ', $names);
326-
}
327-
328-
There are three argument variants you can use:
329-
330-
=========================== ===========================================================================================================
331-
Mode Value
332-
=========================== ===========================================================================================================
333-
``InputArgument::REQUIRED`` The argument is required
334-
``InputArgument::OPTIONAL`` The argument is optional and therefore can be omitted
335-
``InputArgument::IS_ARRAY`` The argument can contain an indefinite number of arguments and must be used at the end of the argument list
336-
=========================== ===========================================================================================================
337-
338-
You can combine ``IS_ARRAY`` with ``REQUIRED`` and ``OPTIONAL`` like this::
339-
340-
$this
341-
// ...
342-
->addArgument(
343-
'names',
344-
InputArgument::IS_ARRAY | InputArgument::REQUIRED,
345-
'Who do you want to greet (separate multiple names with a space)?'
346-
);
347-
348-
Using Command Options
349-
---------------------
350-
351-
Unlike arguments, options are not ordered (meaning you can specify them in any
352-
order) and are specified with two dashes (e.g. ``--yell`` - you can also
353-
declare a one-letter shortcut that you can call with a single dash like
354-
``-y``). Options are *always* optional, and can be setup to accept a value
355-
(e.g. ``--dir=src``) or simply as a boolean flag without a value (e.g.
356-
``--yell``).
357-
358-
.. tip::
359-
360-
There is nothing forbidding you to create a command with an option that
361-
optionally accepts a value. However, there is no way you can distinguish
362-
when the option was used without a value (``command --yell``) or when it
363-
wasn't used at all (``command``). In both cases, the value retrieved for
364-
the option will be ``null``.
365-
366-
For example, add a new option to the command that can be used to specify
367-
how many times in a row the message should be printed::
368-
369-
$this
370-
// ...
371-
->addOption(
372-
'iterations',
373-
null,
374-
InputOption::VALUE_REQUIRED,
375-
'How many times should the message be printed?',
376-
1
377-
);
378-
379-
Next, use this in the command to print the message multiple times:
380-
381-
.. code-block:: php
382-
383-
for ($i = 0; $i < $input->getOption('iterations'); $i++) {
384-
$output->writeln($text);
385-
}
386-
387-
Now, when you run the task, you can optionally specify a ``--iterations``
388-
flag:
389-
390-
.. code-block:: bash
391-
392-
$ php application.php demo:greet Fabien
393-
$ php application.php demo:greet Fabien --iterations=5
394-
395-
The first example will only print once, since ``iterations`` is empty and
396-
defaults to ``1`` (the last argument of ``addOption``). The second example
397-
will print five times.
398-
399-
Recall that options don't care about their order. So, either of the following
400-
will work:
401-
402-
.. code-block:: bash
403-
404-
$ php application.php demo:greet Fabien --iterations=5 --yell
405-
$ php application.php demo:greet Fabien --yell --iterations=5
406-
407-
There are 4 option variants you can use:
408-
409-
=============================== =====================================================================================
410-
Option Value
411-
=============================== =====================================================================================
412-
``InputOption::VALUE_IS_ARRAY`` This option accepts multiple values (e.g. ``--dir=/foo --dir=/bar``)
413-
``InputOption::VALUE_NONE`` Do not accept input for this option (e.g. ``--yell``)
414-
``InputOption::VALUE_REQUIRED`` This value is required (e.g. ``--iterations=5``), the option itself is still optional
415-
``InputOption::VALUE_OPTIONAL`` This option may or may not have a value (e.g. ``--yell`` or ``--yell=loud``)
416-
=============================== =====================================================================================
417-
418-
You can combine ``VALUE_IS_ARRAY`` with ``VALUE_REQUIRED`` or ``VALUE_OPTIONAL`` like this:
419-
420-
.. code-block:: php
421-
422-
$this
423-
// ...
424-
->addOption(
425-
'colors',
426-
null,
427-
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
428-
'Which colors do you like?',
429-
array('blue', 'red')
430-
);
431269

432270
Console Helpers
433271
---------------

console/input.rst

Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
Console Input (Arguments & Options)
2+
===================================
3+
4+
The most interesting part of the commands are the arguments and options that
5+
you can make available. These arguments and options allow you to pass dynamic
6+
information from the terminal to the command.
7+
8+
Using Command Arguments
9+
-----------------------
10+
11+
Arguments are the strings - separated by spaces - that
12+
come after the command name itself. They are ordered, and can be optional
13+
or required. For example, to add an optional ``last_name`` argument to the command
14+
and make the ``name`` argument required::
15+
16+
// ...
17+
use Symfony\Component\Console\Input\InputArgument;
18+
19+
class GreetCommand extends Command
20+
{
21+
// ...
22+
23+
protected function configure()
24+
{
25+
$this
26+
// ...
27+
->addArgument('name', InputArgument::REQUIRED, 'Who do you want to greet?')
28+
->addArgument('last_name', InputArgument::OPTIONAL, 'Your last name?')
29+
;
30+
}
31+
}
32+
33+
You now have access to a ``last_name`` argument in your command::
34+
35+
// ...
36+
class GreetCommand extends Command
37+
{
38+
// ...
39+
40+
protected function execute(InputInterface $input, OutputInterface $output)
41+
{
42+
$text = 'Hi '.$input->getArgument('name');
43+
44+
$lastName = $input->getArgument('last_name')
45+
if ($lastName) {
46+
$text .= ' '.$lastName;
47+
}
48+
49+
$output->writeln($text.'!');
50+
}
51+
}
52+
53+
The command can now be used in either of the following ways:
54+
55+
.. code-block:: bash
56+
57+
$ php app/console app:greet Fabien
58+
Hi Fabien!
59+
60+
$ php app/console app:greet Fabien Potencier
61+
Hi Fabien Potencier!
62+
63+
It is also possible to let an argument take a list of values (imagine you want
64+
to greet all your friends). Only the last argument can be a list::
65+
66+
$this
67+
// ...
68+
->addArgument(
69+
'names',
70+
InputArgument::IS_ARRAY,
71+
'Who do you want to greet (separate multiple names with a space)?'
72+
);
73+
74+
To use this, just specify as many names as you want:
75+
76+
.. code-block:: bash
77+
78+
$ php app/console app:greet Fabien Ryan Bernhard
79+
80+
You can access the ``names`` argument as an array::
81+
82+
$names = $input->getArgument('names')
83+
if (count($names) > 0) {
84+
$text .= ' '.implode(', ', $names);
85+
}
86+
87+
There are three argument variants you can use:
88+
89+
=========================== ===========================================================================================================
90+
Mode Value
91+
=========================== ===========================================================================================================
92+
``InputArgument::REQUIRED`` The argument is required
93+
``InputArgument::OPTIONAL`` The argument is optional and therefore can be omitted
94+
``InputArgument::IS_ARRAY`` The argument can contain an indefinite number of arguments and must be used at the end of the argument list
95+
=========================== ===========================================================================================================
96+
97+
You can combine ``IS_ARRAY`` with ``REQUIRED`` and ``OPTIONAL`` like this::
98+
99+
$this
100+
// ...
101+
->addArgument(
102+
'names',
103+
InputArgument::IS_ARRAY | InputArgument::REQUIRED,
104+
'Who do you want to greet (separate multiple names with a space)?'
105+
);
106+
107+
Using Command Options
108+
---------------------
109+
110+
Unlike arguments, options are not ordered (meaning you can specify them in any
111+
order) and are specified with two dashes (e.g. ``--yell`` - you can also
112+
declare a one-letter shortcut that you can call with a single dash like
113+
``-y``). Options are *always* optional, and can be setup to accept a value
114+
(e.g. ``--dir=src``) or simply as a boolean flag without a value (e.g.
115+
``--yell``).
116+
117+
.. tip::
118+
119+
There is nothing forbidding you to create a command with an option that
120+
optionally accepts a value. However, there is no way you can distinguish
121+
when the option was used without a value (``command --yell``) or when it
122+
wasn't used at all (``command``). In both cases, the value retrieved for
123+
the option will be ``null``.
124+
125+
For example, add a new option to the command that can be used to specify
126+
how many times in a row the message should be printed::
127+
128+
$this
129+
// ...
130+
->addOption(
131+
'iterations',
132+
null,
133+
InputOption::VALUE_REQUIRED,
134+
'How many times should the message be printed?',
135+
1
136+
);
137+
138+
Next, use this in the command to print the message multiple times::
139+
140+
for ($i = 0; $i < $input->getOption('iterations'); $i++) {
141+
$output->writeln($text);
142+
}
143+
144+
Now, when you run the task, you can optionally specify a ``--iterations``
145+
flag:
146+
147+
.. code-block:: bash
148+
149+
# no --iterations provided, the default (1) is used
150+
$ php app/console app:greet Fabien
151+
Hi Fabien!
152+
153+
$ php app/console app:greet Fabien --iterations=5
154+
Hi Fabien
155+
Hi Fabien
156+
Hi Fabien
157+
Hi Fabien
158+
Hi Fabien
159+
160+
# the order of options isn't important
161+
$ php app/console app:greet Fabien --iterations=5 --yell
162+
$ php app/console app:greet Fabien --yell --iterations=5
163+
$ php app/console app:greet --yell --iterations=5 Fabien
164+
165+
There are 4 option variants you can use:
166+
167+
=============================== =====================================================================================
168+
Option Value
169+
=============================== =====================================================================================
170+
``InputOption::VALUE_IS_ARRAY`` This option accepts multiple values (e.g. ``--dir=/foo --dir=/bar``)
171+
``InputOption::VALUE_NONE`` Do not accept input for this option (e.g. ``--yell``)
172+
``InputOption::VALUE_REQUIRED`` This value is required (e.g. ``--iterations=5``), the option itself is still optional
173+
``InputOption::VALUE_OPTIONAL`` This option may or may not have a value (e.g. ``--yell`` or ``--yell=loud``)
174+
=============================== =====================================================================================
175+
176+
You can combine ``VALUE_IS_ARRAY`` with ``VALUE_REQUIRED`` or
177+
``VALUE_OPTIONAL`` like this::
178+
179+
$this
180+
// ...
181+
->addOption(
182+
'colors',
183+
null,
184+
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
185+
'Which colors do you like?',
186+
array('blue', 'red')
187+
);

0 commit comments

Comments
 (0)