diff --git a/src/Symfony/Component/Console/Input/ArgvInput.php b/src/Symfony/Component/Console/Input/ArgvInput.php index 9addf1483d..6ec7a1a57f 100644 --- a/src/Symfony/Component/Console/Input/ArgvInput.php +++ b/src/Symfony/Component/Console/Input/ArgvInput.php @@ -312,4 +312,23 @@ class ArgvInput extends Input 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 9921fffc85..330aeb807b 100644 --- a/src/Symfony/Component/Console/Input/ArrayInput.php +++ b/src/Symfony/Component/Console/Input/ArrayInput.php @@ -110,6 +110,29 @@ class ArrayInput extends Input 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 38cc038ea7..46b345ce5a 100644 --- a/src/Symfony/Component/Console/Tests/Input/ArgvInputTest.php +++ b/src/Symfony/Component/Console/Tests/Input/ArgvInputTest.php @@ -255,6 +255,15 @@ class ArgvInputTest extends \PHPUnit_Framework_TestCase $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 e1838a721e..55f56e67d2 100644 --- a/src/Symfony/Component/Console/Tests/Input/ArrayInputTest.php +++ b/src/Symfony/Component/Console/Tests/Input/ArrayInputTest.php @@ -120,4 +120,10 @@ class ArrayInputTest extends \PHPUnit_Framework_TestCase ) ); } + + 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 1ce5fe6274..ce1b298f9b 100644 --- a/src/Symfony/Component/Console/Tests/Input/StringInputTest.php +++ b/src/Symfony/Component/Console/Tests/Input/StringInputTest.php @@ -73,4 +73,16 @@ class StringInputTest extends \PHPUnit_Framework_TestCase 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); + } }