Skip to content

Commit

Permalink
More consistent syntax:
Browse files Browse the repository at this point in the history
- options with array values are now defined like this: `--option=*` (was previously `[--option=]*`
- options with optional values (`--option=?`) have been removed as they are completely buggy and unusable in Symfony…
  • Loading branch information
mnapoli committed Feb 14, 2015
1 parent 7084866 commit 914770b
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 42 deletions.
25 changes: 12 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,24 +49,23 @@ Commands are defined using a string expression. The expression must start with t

A command can take arguments:

| Description | Example |
|----------------------------------------|---------------|
| Required argument | `greet name` |
| Optional argument | `greet name?` |
| Array argument with 0-n values | `greet name*` |
| Array argument with at least one value | `greet name+` |
| Description | Example |
|--------------------------------|---------------|
| Required argument | `greet name` |
| Optional argument | `greet name?` |
| Array argument with 0-n values | `greet name*` |
| Array argument with 1-n values | `greet name+` |

#### Options

A command can take options:

| Description | Example |
|--------------------------------|-------------------------|
| Simple flag | `greet --yell` |
| Option with an mandatory value | `greet --iterations=` |
| Option with an optional value | `greet --iterations[=]` |
| Option that can be used multiple times <br> (the value would be an array) | `greet [--iterations=]*` |
| Option with a shortcut | `greet -y|--yell` |
| Description | Example |
|-------------------------------------------------|------------------------|
| Simple flag (boolean value) | `greet --yell` |
| Option with an mandatory value | `greet --iterations=` |
| Option that can be used 0-n times (array value) | `greet --iterations=*` |
| Option with a shortcut | `greet -y|--yell` |

Options are always optional (duh). If an option is required, then it should be an argument.

Expand Down
16 changes: 3 additions & 13 deletions src/Command/ExpressionParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,7 @@ private function parseArgument($token)

private function parseOption($token)
{
// It's an array if it looks like `[--iterations=]*`
$isArrayValue = false;
if ($this->startsWith($token, '[-') && $this->endsWith($token, ']*')) {
$isArrayValue = true;
$token = substr($token, 2, -2);
}

// Shortcut `-y--yell`
// Shortcut `-y|--yell`
if (strpos($token, '|') !== false) {
list($shortcut, $token) = explode('|', $token, 2);
$shortcut = ltrim($shortcut, '-');
Expand All @@ -85,15 +78,12 @@ private function parseOption($token)

$name = ltrim($token, '-');

if ($isArrayValue) {
if ($this->endsWith($token, '=*')) {
$mode = InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY;
$name = rtrim($name, '=');
$name = substr($name, 0, -2);
} elseif ($this->endsWith($token, '=')) {
$mode = InputOption::VALUE_REQUIRED;
$name = rtrim($name, '=');
} elseif ($this->endsWith($token, '[=]')) {
$mode = InputOption::VALUE_OPTIONAL;
$name = substr($name, 0, -3);
} else {
$mode = InputOption::VALUE_NONE;
}
Expand Down
18 changes: 2 additions & 16 deletions tests/Command/ExpressionParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,26 +118,12 @@ public function it_parses_options_with_mandatory_values()
]);
}

/**
* @test
*/
public function it_parses_options_with_optional_values()
{
$this->assertParsesTo('greet --yell[=]', [
'name' => 'greet',
'arguments' => [],
'options' => [
new InputOption('yell', null, InputOption::VALUE_OPTIONAL),
],
]);
}

/**
* @test
*/
public function it_parses_options_with_multiple_values()
{
$this->assertParsesTo('greet [--name=]*', [
$this->assertParsesTo('greet --name=*', [
'name' => 'greet',
'arguments' => [],
'options' => [
Expand All @@ -151,7 +137,7 @@ public function it_parses_options_with_multiple_values()
*/
public function it_parses_options_with_shortcuts()
{
$this->assertParsesTo('greet -y|--yell -it|--iterations= [-n|--name=]*', [
$this->assertParsesTo('greet -y|--yell -it|--iterations= -n|--name=*', [
'name' => 'greet',
'arguments' => [],
'options' => [
Expand Down
14 changes: 14 additions & 0 deletions tests/FunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,20 @@ public function it_should_run_a_command_with_an_option()
$this->assertOutputIs('greet --iterations=123', '123');
}

/**
* @test
*/
public function it_should_run_a_command_with_multiple_options()
{
$this->application->command('greet -d|--dir=*', function ($dir, Out $output) {
$output->write('[' . implode(', ', $dir) . ']');
});
$this->assertOutputIs('greet', '[]');
$this->assertOutputIs('greet -d foo', '[foo]');
$this->assertOutputIs('greet -d foo -d bar', '[foo, bar]');
$this->assertOutputIs('greet --dir=foo --dir=bar', '[foo, bar]');
}

private function assertOutputIs($command, $expected)
{
$output = new SpyOutput();
Expand Down

0 comments on commit 914770b

Please sign in to comment.