From c24269005bbbca3d62399d4d9fe949d28f8055c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Pineau?= Date: Wed, 28 Dec 2016 15:28:00 +0100 Subject: [PATCH] [Console] Escape default value when dumping help --- .../Component/Console/Descriptor/TextDescriptor.php | 11 +++++++++++ .../Console/Tests/Descriptor/ObjectsProvider.php | 3 +++ .../Tests/Fixtures/input_argument_with_style.json | 7 +++++++ .../Tests/Fixtures/input_argument_with_style.md | 7 +++++++ .../Tests/Fixtures/input_argument_with_style.txt | 1 + .../Tests/Fixtures/input_argument_with_style.xml | 7 +++++++ .../Tests/Fixtures/input_option_with_style.json | 9 +++++++++ .../Tests/Fixtures/input_option_with_style.md | 9 +++++++++ .../Tests/Fixtures/input_option_with_style.txt | 1 + .../Tests/Fixtures/input_option_with_style.xml | 7 +++++++ .../Fixtures/input_option_with_style_array.json | 12 ++++++++++++ .../Tests/Fixtures/input_option_with_style_array.md | 9 +++++++++ .../Tests/Fixtures/input_option_with_style_array.txt | 1 + .../Tests/Fixtures/input_option_with_style_array.xml | 8 ++++++++ 14 files changed, 92 insertions(+) create mode 100644 src/Symfony/Component/Console/Tests/Fixtures/input_argument_with_style.json create mode 100644 src/Symfony/Component/Console/Tests/Fixtures/input_argument_with_style.md create mode 100644 src/Symfony/Component/Console/Tests/Fixtures/input_argument_with_style.txt create mode 100644 src/Symfony/Component/Console/Tests/Fixtures/input_argument_with_style.xml create mode 100644 src/Symfony/Component/Console/Tests/Fixtures/input_option_with_style.json create mode 100644 src/Symfony/Component/Console/Tests/Fixtures/input_option_with_style.md create mode 100644 src/Symfony/Component/Console/Tests/Fixtures/input_option_with_style.txt create mode 100644 src/Symfony/Component/Console/Tests/Fixtures/input_option_with_style.xml create mode 100644 src/Symfony/Component/Console/Tests/Fixtures/input_option_with_style_array.json create mode 100644 src/Symfony/Component/Console/Tests/Fixtures/input_option_with_style_array.md create mode 100644 src/Symfony/Component/Console/Tests/Fixtures/input_option_with_style_array.txt create mode 100644 src/Symfony/Component/Console/Tests/Fixtures/input_option_with_style_array.xml diff --git a/src/Symfony/Component/Console/Descriptor/TextDescriptor.php b/src/Symfony/Component/Console/Descriptor/TextDescriptor.php index c18ed6f224..b49b64d5ac 100644 --- a/src/Symfony/Component/Console/Descriptor/TextDescriptor.php +++ b/src/Symfony/Component/Console/Descriptor/TextDescriptor.php @@ -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)); } diff --git a/src/Symfony/Component/Console/Tests/Descriptor/ObjectsProvider.php b/src/Symfony/Component/Console/Tests/Descriptor/ObjectsProvider.php index 45b3b2fff9..8f825ecb68 100644 --- a/src/Symfony/Component/Console/Tests/Descriptor/ObjectsProvider.php +++ b/src/Symfony/Component/Console/Tests/Descriptor/ObjectsProvider.php @@ -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', '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', 'style'), + 'input_option_with_style_array' => new InputOption('option_name', 'o', InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED, 'option description', array('Hello', 'world')), ); } diff --git a/src/Symfony/Component/Console/Tests/Fixtures/input_argument_with_style.json b/src/Symfony/Component/Console/Tests/Fixtures/input_argument_with_style.json new file mode 100644 index 0000000000..9334235109 --- /dev/null +++ b/src/Symfony/Component/Console/Tests/Fixtures/input_argument_with_style.json @@ -0,0 +1,7 @@ +{ + "name": "argument_name", + "is_required": false, + "is_array": false, + "description": "argument description", + "default": "style" +} diff --git a/src/Symfony/Component/Console/Tests/Fixtures/input_argument_with_style.md b/src/Symfony/Component/Console/Tests/Fixtures/input_argument_with_style.md new file mode 100644 index 0000000000..45adf2f488 --- /dev/null +++ b/src/Symfony/Component/Console/Tests/Fixtures/input_argument_with_style.md @@ -0,0 +1,7 @@ +**argument_name:** + +* Name: argument_name +* Is required: no +* Is array: no +* Description: argument description +* Default: `'style'` diff --git a/src/Symfony/Component/Console/Tests/Fixtures/input_argument_with_style.txt b/src/Symfony/Component/Console/Tests/Fixtures/input_argument_with_style.txt new file mode 100644 index 0000000000..35384a6be8 --- /dev/null +++ b/src/Symfony/Component/Console/Tests/Fixtures/input_argument_with_style.txt @@ -0,0 +1 @@ + argument_name argument description [default: "\style\"] diff --git a/src/Symfony/Component/Console/Tests/Fixtures/input_argument_with_style.xml b/src/Symfony/Component/Console/Tests/Fixtures/input_argument_with_style.xml new file mode 100644 index 0000000000..73332c796e --- /dev/null +++ b/src/Symfony/Component/Console/Tests/Fixtures/input_argument_with_style.xml @@ -0,0 +1,7 @@ + + + argument description + + <comment>style</> + + diff --git a/src/Symfony/Component/Console/Tests/Fixtures/input_option_with_style.json b/src/Symfony/Component/Console/Tests/Fixtures/input_option_with_style.json new file mode 100644 index 0000000000..df328bf824 --- /dev/null +++ b/src/Symfony/Component/Console/Tests/Fixtures/input_option_with_style.json @@ -0,0 +1,9 @@ +{ + "name": "--option_name", + "shortcut": "-o", + "accept_value": true, + "is_value_required": true, + "is_multiple": false, + "description": "option description", + "default": "style" +} diff --git a/src/Symfony/Component/Console/Tests/Fixtures/input_option_with_style.md b/src/Symfony/Component/Console/Tests/Fixtures/input_option_with_style.md new file mode 100644 index 0000000000..3f6dd2369a --- /dev/null +++ b/src/Symfony/Component/Console/Tests/Fixtures/input_option_with_style.md @@ -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: `'style'` diff --git a/src/Symfony/Component/Console/Tests/Fixtures/input_option_with_style.txt b/src/Symfony/Component/Console/Tests/Fixtures/input_option_with_style.txt new file mode 100644 index 0000000000..880a53518e --- /dev/null +++ b/src/Symfony/Component/Console/Tests/Fixtures/input_option_with_style.txt @@ -0,0 +1 @@ + -o, --option_name=OPTION_NAME option description [default: "\style\"] diff --git a/src/Symfony/Component/Console/Tests/Fixtures/input_option_with_style.xml b/src/Symfony/Component/Console/Tests/Fixtures/input_option_with_style.xml new file mode 100644 index 0000000000..764b9e6521 --- /dev/null +++ b/src/Symfony/Component/Console/Tests/Fixtures/input_option_with_style.xml @@ -0,0 +1,7 @@ + + diff --git a/src/Symfony/Component/Console/Tests/Fixtures/input_option_with_style_array.json b/src/Symfony/Component/Console/Tests/Fixtures/input_option_with_style_array.json new file mode 100644 index 0000000000..b1754550b5 --- /dev/null +++ b/src/Symfony/Component/Console/Tests/Fixtures/input_option_with_style_array.json @@ -0,0 +1,12 @@ +{ + "name": "--option_name", + "shortcut": "-o", + "accept_value": true, + "is_value_required": true, + "is_multiple": true, + "description": "option description", + "default": [ + "Hello", + "world" + ] +} diff --git a/src/Symfony/Component/Console/Tests/Fixtures/input_option_with_style_array.md b/src/Symfony/Component/Console/Tests/Fixtures/input_option_with_style_array.md new file mode 100644 index 0000000000..24e58b5302 --- /dev/null +++ b/src/Symfony/Component/Console/Tests/Fixtures/input_option_with_style_array.md @@ -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 => 'Hello', 1 => 'world',)` diff --git a/src/Symfony/Component/Console/Tests/Fixtures/input_option_with_style_array.txt b/src/Symfony/Component/Console/Tests/Fixtures/input_option_with_style_array.txt new file mode 100644 index 0000000000..265c18c5a4 --- /dev/null +++ b/src/Symfony/Component/Console/Tests/Fixtures/input_option_with_style_array.txt @@ -0,0 +1 @@ + -o, --option_name=OPTION_NAME option description [default: ["\Hello\","\world\"]] (multiple values allowed) diff --git a/src/Symfony/Component/Console/Tests/Fixtures/input_option_with_style_array.xml b/src/Symfony/Component/Console/Tests/Fixtures/input_option_with_style_array.xml new file mode 100644 index 0000000000..09dc865830 --- /dev/null +++ b/src/Symfony/Component/Console/Tests/Fixtures/input_option_with_style_array.xml @@ -0,0 +1,8 @@ + +