diff --git a/src/Symfony/Component/Console/Input/ArgvInput.php b/src/Symfony/Component/Console/Input/ArgvInput.php index 9addf1483d926..6ec7a1a57fdee 100644 --- a/src/Symfony/Component/Console/Input/ArgvInput.php +++ b/src/Symfony/Component/Console/Input/ArgvInput.php @@ -312,4 +312,23 @@ public function getParameterOption($values, $default = false) return $default; } + + /** + * Returns a stringified representation of the args passed to the command + * + * @return string + */ + public function __toString() + { + $tokens = array_map(function ($token) { + $token = addcslashes($token, '"'); + if (false !== strpos($token, ' ')) { + return '"'.$token.'"'; + } + + return $token; + }, $this->tokens); + + return implode(' ', $tokens); + } } diff --git a/src/Symfony/Component/Console/Input/ArrayInput.php b/src/Symfony/Component/Console/Input/ArrayInput.php index 9921fffc855ff..330aeb807bba2 100644 --- a/src/Symfony/Component/Console/Input/ArrayInput.php +++ b/src/Symfony/Component/Console/Input/ArrayInput.php @@ -110,6 +110,29 @@ public function getParameterOption($values, $default = false) return $default; } + /** + * Returns a stringified representation of the args passed to the command + * + * @return string + */ + public function __toString() + { + $params = array(); + foreach ($this->parameters as $param => $val) { + $val = addcslashes($val, '"'); + if (false !== strpos($val, ' ')) { + $val = '"'.$val.'"'; + } + if ($param && '-' === $param[0]) { + $params[] = $param . ('' != $val ? ' '.$val : $val); + } else { + $params[] = $val; + } + } + + return implode(' ', $params); + } + /** * Processes command line arguments. */ diff --git a/src/Symfony/Component/Console/Tests/Input/ArgvInputTest.php b/src/Symfony/Component/Console/Tests/Input/ArgvInputTest.php index 38cc038ea70a3..46b345ce5a7e4 100644 --- a/src/Symfony/Component/Console/Tests/Input/ArgvInputTest.php +++ b/src/Symfony/Component/Console/Tests/Input/ArgvInputTest.php @@ -255,6 +255,15 @@ public function testHasParameterOption() $this->assertFalse($input->hasParameterOption('--foo'), '->hasParameterOption() returns false if the given short option is not in the raw input'); } + public function testToString() + { + $input = new ArgvInput(array('cli.php', '-f', 'foo')); + $this->assertEquals('-f foo', (string) $input); + + $input = new ArgvInput(array('cli.php', '-f', '--bar=foo', 'a b c d')); + $this->assertEquals('-f --bar=foo "a b c d"', (string) $input); + } + /** * @dataProvider provideGetParameterOptionValues */ diff --git a/src/Symfony/Component/Console/Tests/Input/ArrayInputTest.php b/src/Symfony/Component/Console/Tests/Input/ArrayInputTest.php index e1838a721e926..55f56e67d244e 100644 --- a/src/Symfony/Component/Console/Tests/Input/ArrayInputTest.php +++ b/src/Symfony/Component/Console/Tests/Input/ArrayInputTest.php @@ -120,4 +120,10 @@ public function provideInvalidInput() ) ); } + + public function testToString() + { + $input = new ArrayInput(array('-f' => null, '-b' => 'bar', '--foo' => 'b a z', '--lala' => null, 'test' => 'Foo')); + $this->assertEquals('-f -b bar --foo "b a z" --lala Foo', (string) $input); + } } diff --git a/src/Symfony/Component/Console/Tests/Input/StringInputTest.php b/src/Symfony/Component/Console/Tests/Input/StringInputTest.php index 1ce5fe62743a5..ce1b298f9b47c 100644 --- a/src/Symfony/Component/Console/Tests/Input/StringInputTest.php +++ b/src/Symfony/Component/Console/Tests/Input/StringInputTest.php @@ -73,4 +73,16 @@ public function getTokenizeData() array('foo -a -ffoo --long bar', array('foo', '-a', '-ffoo', '--long', 'bar'), '->tokenize() parses when several arguments and options'), ); } + + public function testToString() + { + $input = new StringInput('-f foo'); + $this->assertEquals('-f foo', (string) $input); + + $input = new StringInput('-f --bar=foo "a b c d"'); + $this->assertEquals('-f --bar=foo "a b c d"', (string) $input); + + $input = new StringInput('-f --bar=foo \'a b c d\''); + $this->assertEquals('-f --bar=foo "a b c d"', (string) $input); + } }