@@ -8,19 +8,18 @@ It can be difficult to understand the way arguments are handled by the console a
8
8
The Symfony Console application, like many other CLI utility tools, follows the behavior
9
9
described in the `docopt `_ standards.
10
10
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::
13
12
14
- namespace Acme\Console\Command;
13
+ namespace Acme\Console\Command;
15
14
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;
21
20
22
- class DemoArgsCommand extends Command
23
- {
21
+ class DemoArgsCommand extends Command
22
+ {
24
23
protected function configure()
25
24
{
26
25
$this
@@ -29,56 +28,63 @@ regarding to the Console Options or Arguments defined in the application::
29
28
->setDefinition(
30
29
new InputDefinition(array(
31
30
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
+ );
37
35
}
38
36
39
37
protected function execute(InputInterface $input, OutputInterface $output)
40
38
{
41
39
// ...
42
40
}
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
+ ============================== ================= =========== ===========
82
89
83
90
.. _docopt : http://docopt.org/
84
-
0 commit comments