Skip to content

Commit

Permalink
[5.5] Allow Default Value for Command Array Input. (#18572)
Browse files Browse the repository at this point in the history
* Allow console command signature to define input array with default value

* Move * character

* Add tests for ConsoleParser default values

* StyleCI
  • Loading branch information
CupOfTea696 authored and taylorotwell committed Apr 1, 2017
1 parent 3d68993 commit 717e24d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Illuminate/Console/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ protected static function parseArgument($token)
return new InputArgument(trim($token, '*'), InputArgument::IS_ARRAY | InputArgument::REQUIRED, $description);
case Str::endsWith($token, '?'):
return new InputArgument(trim($token, '?'), InputArgument::OPTIONAL, $description);
case preg_match('/(.+)\=\*(.+)/', $token, $matches):
return new InputArgument($matches[1], InputArgument::IS_ARRAY, $description, preg_split('/,\s?/', $matches[2]));
case preg_match('/(.+)\=(.+)/', $token, $matches):
return new InputArgument($matches[1], InputArgument::OPTIONAL, $description, $matches[2]);
default:
Expand Down Expand Up @@ -120,6 +122,8 @@ protected static function parseOption($token)
return new InputOption(trim($token, '='), $shortcut, InputOption::VALUE_OPTIONAL, $description);
case Str::endsWith($token, '=*'):
return new InputOption(trim($token, '=*'), $shortcut, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, $description);
case preg_match('/(.+)\=\*(.+)/', $token, $matches):
return new InputOption($matches[1], $shortcut, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, $description, preg_split('/,\s?/', $matches[2]));
case preg_match('/(.+)\=(.+)/', $token, $matches):
return new InputOption($matches[1], $shortcut, InputOption::VALUE_OPTIONAL, $description, $matches[2]);
default:
Expand Down
19 changes: 19 additions & 0 deletions tests/Console/ConsoleParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,23 @@ public function testShortcutNameParsing()
$this->assertTrue($results[2][0]->acceptValue());
$this->assertTrue($results[2][0]->isArray());
}

public function testDefaultValueParsing()
{
$results = Parser::parse('command:name {argument=defaultArgumentValue} {--option=defaultOptionValue}');

$this->assertFalse($results[1][0]->isRequired());
$this->assertEquals('defaultArgumentValue', $results[1][0]->getDefault());
$this->assertTrue($results[2][0]->acceptValue());
$this->assertEquals('defaultOptionValue', $results[2][0]->getDefault());

$results = Parser::parse('command:name {argument=*defaultArgumentValue1,defaultArgumentValue2} {--option=*defaultOptionValue1,defaultOptionValue2}');

$this->assertTrue($results[1][0]->isArray());
$this->assertFalse($results[1][0]->isRequired());
$this->assertEquals(['defaultArgumentValue1', 'defaultArgumentValue2'], $results[1][0]->getDefault());
$this->assertTrue($results[2][0]->acceptValue());
$this->assertTrue($results[2][0]->isArray());
$this->assertEquals(['defaultOptionValue1', 'defaultOptionValue2'], $results[2][0]->getDefault());
}
}

0 comments on commit 717e24d

Please sign in to comment.