Skip to content
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

[11.x] Support New Lines in Command Argument and Option Descriptions #54057

Closed
Closed
Show file tree
Hide file tree
Changes from 3 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
18 changes: 9 additions & 9 deletions src/Illuminate/Console/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ class Parser
* @param string $expression
* @return array
*
* @throws \InvalidArgumentException
* @throws InvalidArgumentException
*/
public static function parse(string $expression)
public static function parse(string $expression): array
shamimulalam marked this conversation as resolved.
Show resolved Hide resolved
{
$name = static::name($expression);

if (preg_match_all('/\{\s*(.*?)\s*\}/', $expression, $matches) && count($matches[1])) {
if (preg_match_all('/\{([^}]*)}/s', $expression, $matches) && count($matches[1])) {
return array_merge([$name], static::parameters($matches[1]));
}

Expand All @@ -33,9 +33,9 @@ public static function parse(string $expression)
* @param string $expression
* @return string
*
* @throws \InvalidArgumentException
* @throws InvalidArgumentException
*/
protected static function name(string $expression)
protected static function name(string $expression): string
shamimulalam marked this conversation as resolved.
Show resolved Hide resolved
{
if (! preg_match('/[^\s]+/', $expression, $matches)) {
throw new InvalidArgumentException('Unable to determine command name from signature.');
Expand All @@ -50,7 +50,7 @@ protected static function name(string $expression)
* @param array $tokens
* @return array
*/
protected static function parameters(array $tokens)
protected static function parameters(array $tokens): array
shamimulalam marked this conversation as resolved.
Show resolved Hide resolved
{
$arguments = [];

Expand All @@ -73,7 +73,7 @@ protected static function parameters(array $tokens)
* @param string $token
* @return \Symfony\Component\Console\Input\InputArgument
*/
protected static function parseArgument(string $token)
protected static function parseArgument(string $token): InputArgument
shamimulalam marked this conversation as resolved.
Show resolved Hide resolved
{
[$token, $description] = static::extractDescription($token);

Expand All @@ -99,7 +99,7 @@ protected static function parseArgument(string $token)
* @param string $token
* @return \Symfony\Component\Console\Input\InputOption
*/
protected static function parseOption(string $token)
protected static function parseOption(string $token): InputOption
shamimulalam marked this conversation as resolved.
Show resolved Hide resolved
{
[$token, $description] = static::extractDescription($token);

Expand Down Expand Up @@ -132,7 +132,7 @@ protected static function parseOption(string $token)
* @param string $token
* @return array
*/
protected static function extractDescription(string $token)
protected static function extractDescription(string $token): array
shamimulalam marked this conversation as resolved.
Show resolved Hide resolved
{
$parts = preg_split('/\s+:\s+/', trim($token), 2);

Expand Down
22 changes: 22 additions & 0 deletions tests/Console/ConsoleParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use Illuminate\Console\Parser;
use InvalidArgumentException;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;

class ConsoleParserTest extends TestCase
{
Expand Down Expand Up @@ -161,4 +163,24 @@ public function testNameInEmptyException()

Parser::parse('');
}

public function testItParsesCommandWithArgumentAndOptionWithNewLines()
{
$signature = 'command:name
{argument= :
The description of the argument.
This can span multiple lines for clarity.}
{--o|option= : The description of the option. Can be a value like "value1", "value2", etc.}';

$result = Parser::parse($signature);

$this->assertIsArray($result);
$this->assertEquals('command:name', $result[0]);

$this->assertCount(1, $result[1]);
$this->assertInstanceOf(InputArgument::class, $result[1][0]);

$this->assertCount(1, $result[2]);
$this->assertInstanceOf(InputOption::class, $result[2][0]);
}
}
Loading