Skip to content

Commit 1ce59ee

Browse files
committed
finish #3744
1 parent f503596 commit 1ce59ee

File tree

2 files changed

+62
-55
lines changed

2 files changed

+62
-55
lines changed

components/console/console_arguments.rst

+61-55
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,18 @@ It can be difficult to understand the way arguments are handled by the console a
88
The Symfony Console application, like many other CLI utility tools, follows the behavior
99
described in the `docopt`_ standards.
1010

11-
Let's see a complete example on how the arguments are understood by Console application,
12-
regarding to the Console Options or Arguments defined in the application::
11+
Have a look at the following command that has three options::
1312

14-
namespace Acme\Console\Command;
13+
namespace Acme\Console\Command;
1514

16-
use Symfony\Component\Console\Command\Command;
17-
use Symfony\Component\Console\Input\InputArgument;
18-
use Symfony\Component\Console\Input\InputInterface;
19-
use Symfony\Component\Console\Input\InputOption;
20-
use Symfony\Component\Console\Output\OutputInterface;
15+
use Symfony\Component\Console\Command\Command;
16+
use Symfony\Component\Console\Input\InputArgument;
17+
use Symfony\Component\Console\Input\InputInterface;
18+
use Symfony\Component\Console\Input\InputOption;
19+
use Symfony\Component\Console\Output\OutputInterface;
2120

22-
class DemoArgsCommand extends Command
23-
{
21+
class DemoArgsCommand extends Command
22+
{
2423
protected function configure()
2524
{
2625
$this
@@ -29,56 +28,63 @@ regarding to the Console Options or Arguments defined in the application::
2928
->setDefinition(
3029
new InputDefinition(array(
3130
new InputOption('foo', 'f'),
32-
new InputOption('bar', 'br', InputOption::VALUE_REQUIRED),
33-
new InputOption('baz', 'bz', InputOption::VALUE_OPTIONAL)
34-
)
35-
)
36-
;
31+
new InputOption('bar', 'b', InputOption::VALUE_REQUIRED),
32+
new InputOption('cat', 'c', InputOption::VALUE_OPTIONAL),
33+
))
34+
);
3735
}
3836

3937
protected function execute(InputInterface $input, OutputInterface $output)
4038
{
4139
// ...
4240
}
43-
}
44-
45-
Let's take a look to the values results for differents inputs:
46-
47-
==================== =========================================
48-
Input Values
49-
==================== =========================================
50-
--bar=Hello foo = false, bar = "Hello"
51-
--bar Hello foo = false, bar = "Hello"
52-
-br=Hello foo = false, bar = "Hello"
53-
-br Hello foo = false, bar = "Hello"
54-
-brHello foo = false, bar = "Hello"
55-
-fbzWorld -br Hello foo = true, bar = "Hello", baz = "World"
56-
-bzfWorld -br Hello foo = false, bar = "Hello", baz ="fWorld"
57-
-bzbrWorld foo = false, bz = "brWorld", baz = null
58-
==================== =========================================
59-
60-
61-
Now, assume there is also an optional argument in the input definition::
62-
63-
new InputDefinition(array(
64-
// ...
65-
new InputArgument('arg', InputArgument::OPTIONAL),
66-
));
67-
68-
========================== ========================================
69-
Input Values
70-
========================== ========================================
71-
--bar Hello bar = "Hello", arg = null
72-
--bar Hello World bar = "Hello", arg = "World"
73-
--bar Hello --baz World bar = "Hello", baz = "World", arg = null
74-
--bar Hello --baz -- World bar = "Hello", baz = null, arg = "World"
75-
-b Hello -bz World bar = "Hello", baz = "World", arg = null
76-
========================== ========================================
77-
78-
The fourth example shows the special ``--`` seperator which -as you can read
79-
in docopt- seperates the options from the arguments. By that, ``World`` is
80-
no longer interpreted as a value of the ``baz`` option (which has an optional value),
81-
but as the value for the argument.
41+
}
42+
43+
Since the ``foo`` option doesn't accept a value, it will be either ``false``
44+
(when it is not passed to the command) or ``true`` (when ``--foo`` was passed
45+
by the user). The value of the ``bar`` option (and its ``b`` shortcut respectively)
46+
is required. It can be separated from the option name either by spaces or
47+
``=`` characters. The ``cat`` option (and its ``c`` shortcut) behaves similar
48+
except that it doesn't require a value. Have a look at the following table
49+
to get an overview of the possible ways to pass options:
50+
51+
===================== ========= =========== ============
52+
Input ``foo`` ``bar`` ``cat``
53+
===================== ========= =========== ============
54+
``--bar=Hello`` ``false`` ``"Hello"`` ``null``
55+
``--bar Hello`` ``false`` ``"Hello"`` ``null``
56+
``-b=Hello`` ``false`` ``"Hello"`` ``null``
57+
``-b Hello`` ``false`` ``"Hello"`` ``null``
58+
``-bHello`` ``false`` ``"Hello"`` ``null``
59+
``-fcWorld -b Hello`` ``true`` ``"Hello"`` ``"World"``
60+
``-cfWorld -b Hello`` ``false`` ``"Hello"`` ``"fWorld"``
61+
``-cbWorld`` ``false`` ``null`` ``"bWorld"``
62+
===================== ========= =========== ============
63+
64+
Things get a little bit more tricky when the command also accepts an optional
65+
argument::
66+
67+
// ...
68+
69+
new InputDefinition(array(
70+
// ...
71+
new InputArgument('arg', InputArgument::OPTIONAL),
72+
));
73+
74+
You might have to use the special ``--`` separator to separate options from
75+
arguments. Have a look at the fifth example in the following table where it
76+
is used to tell the command that ``World`` is the value for ``arg`` and not
77+
the value of the optional ``cat`` option:
78+
79+
============================== ================= =========== ===========
80+
Input ``bar`` ``cat`` ``arg``
81+
============================== ================= =========== ===========
82+
``--bar Hello`` ``"Hello"`` ``null`` ``null``
83+
``--bar Hello World`` ``"Hello"`` ``null`` ``"World"``
84+
``--bar "Hello World"`` ``"Hello World"`` ``null`` ``null``
85+
``--bar Hello --cat World`` ``"Hello"`` ``"World"`` ``null``
86+
``--bar Hello --cat -- World`` ``"Hello"`` ``null`` ``"World"``
87+
``-b Hello -c World`` ``"Hello"`` ``"World"`` ``null``
88+
============================== ================= =========== ===========
8289

8390
.. _docopt: http://docopt.org/
84-

components/map.rst.inc

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
* :doc:`/components/console/introduction`
2222
* :doc:`/components/console/usage`
2323
* :doc:`/components/console/single_command_tool`
24+
* :doc:`/components/console/console_arguments`
2425
* :doc:`/components/console/events`
2526
* :doc:`/components/console/helpers/index`
2627

0 commit comments

Comments
 (0)