8000 [Console] Add ArgvInput::__toString, fixes #7257 by Seldaek · Pull Request #7648 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Console] Add ArgvInput::__toString, fixes #7257 #7648

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 12, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/Symfony/Component/Console/Input/ArgvInput.php
Original file line number Diff line number Diff line change
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()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Lea 8000 rn more.

what about having a __toString() on all other Input classes.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

String inherits already, I'll see what I can do about ArrayInput. IMO it could inherit from Argv but maybe I missed something

{
$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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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