[Console] Escape default value when dumping help

This commit is contained in:
Grégoire Pineau 2016-12-28 15:28:00 +01:00
parent 7e8e9e3a6e
commit c24269005b
14 changed files with 92 additions and 0 deletions

View File

@ -13,6 +13,7 @@ namespace Symfony\Component\Console\Descriptor;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Formatter\OutputFormatter;
use Symfony\Component\Console\Helper\Helper;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputDefinition;
@ -236,6 +237,16 @@ class TextDescriptor extends Descriptor
*/
private function formatDefaultValue($default)
{
if (is_string($default)) {
$default = OutputFormatter::escape($default);
} elseif (is_array($default)) {
foreach ($default as $key => $value) {
if (is_string($value)) {
$default[$key] = OutputFormatter::escape($value);
}
}
}
if (PHP_VERSION_ID < 50400) {
return str_replace(array('\/', '\\\\'), array('/', '\\'), json_encode($default));
}

View File

@ -31,6 +31,7 @@ class ObjectsProvider
'input_argument_2' => new InputArgument('argument_name', InputArgument::IS_ARRAY, 'argument description'),
'input_argument_3' => new InputArgument('argument_name', InputArgument::OPTIONAL, 'argument description', 'default_value'),
'input_argument_4' => new InputArgument('argument_name', InputArgument::REQUIRED, "multiline\nargument description"),
'input_argument_with_style' => new InputArgument('argument_name', InputArgument::OPTIONAL, 'argument description', '<comment>style</>'),
);
}
@ -43,6 +44,8 @@ class ObjectsProvider
'input_option_4' => new InputOption('option_name', 'o', InputOption::VALUE_IS_ARRAY | InputOption::VALUE_OPTIONAL, 'option description', array()),
'input_option_5' => new InputOption('option_name', 'o', InputOption::VALUE_REQUIRED, "multiline\noption description"),
'input_option_6' => new InputOption('option_name', array('o', 'O'), InputOption::VALUE_REQUIRED, 'option with multiple shortcuts'),
'input_option_with_style' => new InputOption('option_name', 'o', InputOption::VALUE_REQUIRED, 'option description', '<comment>style</>'),
'input_option_with_style_array' => new InputOption('option_name', 'o', InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED, 'option description', array('<comment>Hello</comment>', '<info>world</info>')),
);
}

View File

@ -0,0 +1,7 @@
{
"name": "argument_name",
"is_required": false,
"is_array": false,
"description": "argument description",
"default": "<comment>style</>"
}

View File

@ -0,0 +1,7 @@
**argument_name:**
* Name: argument_name
* Is required: no
* Is array: no
* Description: argument description
* Default: `'<comment>style</>'`

View File

@ -0,0 +1 @@
<info>argument_name</info> argument description<comment> [default: "\<comment>style\</>"]</comment>

View File

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<argument name="argument_name" is_required="0" is_array="0">
<description>argument description</description>
<defaults>
<default>&lt;comment&gt;style&lt;/&gt;</default>
</defaults>
</argument>

View File

@ -0,0 +1,9 @@
{
"name": "--option_name",
"shortcut": "-o",
"accept_value": true,
"is_value_required": true,
"is_multiple": false,
"description": "option description",
"default": "<comment>style</>"
}

View File

@ -0,0 +1,9 @@
**option_name:**
* Name: `--option_name`
* Shortcut: `-o`
* Accept value: yes
* Is value required: yes
* Is multiple: no
* Description: option description
* Default: `'<comment>style</>'`

View File

@ -0,0 +1 @@
<info>-o, --option_name=OPTION_NAME</info> option description<comment> [default: "\<comment>style\</>"]</comment>

View File

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<option name="--option_name" shortcut="-o" accept_value="1" is_value_required="1" is_multiple="0">
<description>option description</description>
<defaults>
<default>&lt;comment&gt;style&lt;/&gt;</default>
</defaults>
</option>

View File

@ -0,0 +1,12 @@
{
"name": "--option_name",
"shortcut": "-o",
"accept_value": true,
"is_value_required": true,
"is_multiple": true,
"description": "option description",
"default": [
"<comment>Hello</comment>",
"<info>world</info>"
]
}

View File

@ -0,0 +1,9 @@
**option_name:**
* Name: `--option_name`
* Shortcut: `-o`
* Accept value: yes
* Is value required: yes
* Is multiple: yes
* Description: option description
* Default: `array ( 0 => '<comment>Hello</comment>', 1 => '<info>world</info>',)`

View File

@ -0,0 +1 @@
<info>-o, --option_name=OPTION_NAME</info> option description<comment> [default: ["\<comment>Hello\</comment>","\<info>world\</info>"]]</comment><comment> (multiple values allowed)</comment>

View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<option name="--option_name" shortcut="-o" accept_value="1" is_value_required="1" is_multiple="1">
<description>option description</description>
<defaults>
<default>&lt;comment&gt;Hello&lt;/comment&gt;</default>
<default>&lt;info&gt;world&lt;/info&gt;</default>
</defaults>
</option>