Skip to content

Commit f503596

Browse files
mickaelandrieuxabbuh
authored andcommitted
[WIP] - Console add Console arguments page
1 parent d2b69b6 commit f503596

File tree

4 files changed

+87
-1
lines changed

4 files changed

+87
-1
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
.. index::
2+
single: Console; Console arguments
3+
4+
Understand how Console Arguments are Handled
5+
============================================
6+
7+
It can be difficult to understand the way arguments are handled by the console application.
8+
The Symfony Console application, like many other CLI utility tools, follows the behavior
9+
described in the `docopt`_ standards.
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::
13+
14+
namespace Acme\Console\Command;
15+
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;
21+
22+
class DemoArgsCommand extends Command
23+
{
24+
protected function configure()
25+
{
26+
$this
27+
->setName('demo:args')
28+
->setDescription('Describe args behaviors')
29+
->setDefinition(
30+
new InputDefinition(array(
31+
new InputOption('foo', 'f'),
32+
new InputOption('bar', 'br', InputOption::VALUE_REQUIRED),
33+
new InputOption('baz', 'bz', InputOption::VALUE_OPTIONAL)
34+
)
35+
)
36+
;
37+
}
38+
39+
protected function execute(InputInterface $input, OutputInterface $output)
40+
{
41+
// ...
42+
}
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.
82+
83+
.. _docopt: http://docopt.org/
84+

components/console/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@ Console
77
introduction
88
usage
99
single_command_tool
10+
console_arguments
1011
events
1112
helpers/index

components/console/introduction.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,7 @@ Learn More!
495495
* :doc:`/components/console/usage`
496496
* :doc:`/components/console/single_command_tool`
497497
* :doc:`/components/console/events`
498+
* :doc:`/components/console/console_arguments`
498499

499500
.. _Packagist: https://packagist.org/packages/symfony/console
500501
.. _ANSICON: https://github.com/adoxa/ansicon/releases

components/console/single_command_tool.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
.. index::
2-
single: Console; Single command application
2+
single: Console; Single command application
33

44
Building a single Command Application
55
=====================================

0 commit comments

Comments
 (0)