@@ -133,301 +133,7 @@ Commands have three lifecycle methods:
133133 This method is executed after ``interact() `` and ``initialize() ``.
134134 It contains the logic you want the command to execute.
135135
136- .. _components-console-coloring :
137136
138- Coloring the Output
139- ~~~~~~~~~~~~~~~~~~~
140-
141- .. note ::
142-
143- By default, the Windows command console doesn't support output coloring. The
144- Console component disables output coloring for Windows systems, but if your
145- commands invoke other scripts which emit color sequences, they will be
146- wrongly displayed as raw escape characters. Install the `Cmder `_, `ConEmu `_, `ANSICON `_
147- or `Mintty `_ (used by default in GitBash and Cygwin) free applications
148- to add coloring support to your Windows command console.
149-
150- Whenever you output text, you can surround the text with tags to color its
151- output. For example::
152-
153- // green text
154- $output->writeln('<info>foo</info>');
155-
156- // yellow text
157- $output->writeln('<comment>foo</comment>');
158-
159- // black text on a cyan background
160- $output->writeln('<question>foo</question>');
161-
162- // white text on a red background
163- $output->writeln('<error>foo</error>');
164-
165- The closing tag can be replaced by ``</> ``, which revokes all formatting options
166- established by the last opened tag.
167-
168- It is possible to define your own styles using the class
169- :class: `Symfony\\ Component\\ Console\\ Formatter\\ OutputFormatterStyle `::
170-
171- use Symfony\Component\Console\Formatter\OutputFormatterStyle;
172-
173- // ...
174- $style = new OutputFormatterStyle('red', 'yellow', array('bold', 'blink'));
175- $output->getFormatter()->setStyle('fire', $style);
176- $output->writeln('<fire>foo</>');
177-
178- Available foreground and background colors are: ``black ``, ``red ``, ``green ``,
179- ``yellow ``, ``blue ``, ``magenta ``, ``cyan `` and ``white ``.
180-
181- And available options are: ``bold ``, ``underscore ``, ``blink ``, ``reverse ``
182- (enables the "reverse video" mode where the background and foreground colors
183- are swapped) and ``conceal `` (sets the foreground color to transparent, making
184- the typed text invisible - although it can be selected and copied; this option is
185- commonly used when asking the user to type sensitive information).
186-
187- You can also set these colors and options inside the tagname::
188-
189- // green text
190- $output->writeln('<fg=green>foo</>');
191-
192- // black text on a cyan background
193- $output->writeln('<fg=black;bg=cyan>foo</>');
194-
195- // bold text on a yellow background
196- $output->writeln('<bg=yellow;options=bold>foo</>');
197-
198- .. _verbosity-levels :
199-
200- Verbosity Levels
201- ~~~~~~~~~~~~~~~~
202-
203- .. versionadded :: 2.3
204- The ``VERBOSITY_VERY_VERBOSE `` and ``VERBOSITY_DEBUG `` constants were introduced
205- in version 2.3
206-
207- The console has five verbosity levels. These are defined in the
208- :class: `Symfony\\ Component\\ Console\\ Output\\ OutputInterface `:
209-
210- =========================================== ================================== =====================
211- Value Meaning Console option
212- =========================================== ================================== =====================
213- ``OutputInterface::VERBOSITY_QUIET `` Do not output any messages ``-q `` or ``--quiet ``
214- ``OutputInterface::VERBOSITY_NORMAL `` The default verbosity level (none)
215- ``OutputInterface::VERBOSITY_VERBOSE `` Increased verbosity of messages ``-v ``
216- ``OutputInterface::VERBOSITY_VERY_VERBOSE `` Informative non essential messages ``-vv ``
217- ``OutputInterface::VERBOSITY_DEBUG `` Debug messages ``-vvv ``
218- =========================================== ================================== =====================
219-
220- .. tip ::
221-
222- The full exception stacktrace is printed if the ``VERBOSITY_VERBOSE ``
223- level or above is used.
224-
225- It is possible to print a message in a command for only a specific verbosity
226- level. For example::
227-
228- if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) {
229- $output->writeln(...);
230- }
231-
232- There are also more semantic methods you can use to test for each of the
233- verbosity levels::
234-
235- if ($output->isQuiet()) {
236- // ...
237- }
238-
239- if ($output->isVerbose()) {
240- // ...
241- }
242-
243- if ($output->isVeryVerbose()) {
244- // ...
245- }
246-
247- if ($output->isDebug()) {
248- // ...
249- }
250-
251- .. note ::
252-
253- These semantic methods are defined in the ``OutputInterface `` starting from
254- Symfony 3.0. In previous Symfony versions they are defined in the different
255- implementations of the interface (e.g. :class: `Symfony\\ Component\\ Console\\ Output\\ Output `)
256- in order to keep backwards compatibility.
257-
258- When the quiet level is used, all output is suppressed as the default
259- :method: `Symfony\\ Component\\ Console\\ Output\\ Output::write ` method returns
260- without actually printing.
261-
262- .. tip ::
263-
264- The MonologBridge provides a :class: `Symfony\\ Bridge\\ Monolog\\ Handler\\ ConsoleHandler `
265- class that allows you to display messages on the console. This is cleaner
266- than wrapping your output calls in conditions. For an example use in
267- the Symfony Framework, see :doc: `/logging/monolog_console `.
268-
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- );
431137
432138Console Helpers
433139---------------
0 commit comments