Skip to content

Commit

Permalink
[Console] Add ArgvInput::__toString and ArrayInput::__toString, fixes #…
Browse files Browse the repository at this point in the history
  • Loading branch information
Seldaek committed Apr 12, 2013
1 parent 09fd2af commit 659eb66
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/Symfony/Component/Console/Input/ArgvInput.php
Expand Up @@ -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);
}
}
23 changes: 23 additions & 0 deletions src/Symfony/Component/Console/Input/ArrayInput.php
Expand Up @@ -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.
*/
Expand Down
9 changes: 9 additions & 0 deletions src/Symfony/Component/Console/Tests/Input/ArgvInputTest.php
Expand Up @@ -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
*/
Expand Down
6 changes: 6 additions & 0 deletions src/Symfony/Component/Console/Tests/Input/ArrayInputTest.php
Expand Up @@ -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);
}
}
12 changes: 12 additions & 0 deletions src/Symfony/Component/Console/Tests/Input/StringInputTest.php
Expand Up @@ -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);
}
}

0 comments on commit 659eb66

Please sign in to comment.