Browse Source

Fixes #15221: Console improvements

tags/2.0.14
Brandon Kelly 7 years ago committed by Alexander Makarov
parent
commit
0948109a19
  1. 3
      framework/CHANGELOG.md
  2. 13
      framework/console/Controller.php
  3. 6
      framework/console/Request.php
  4. 4
      framework/console/controllers/HelpController.php
  5. 5
      tests/framework/console/ControllerTest.php
  6. 39
      tests/framework/console/RequestTest.php

3
framework/CHANGELOG.md

@ -11,6 +11,9 @@ Yii Framework 2 Change Log
- Enh #15135: Automatic completion for help in bash and zsh (Valkeru)
- Enh #14662: Added support for custom `Content-Type` specification to `yii\web\JsonResponseFormatter` (Kolyunya)
- Enh #14568: Refactored migration templates to use `safeUp()` and `safeDown()` methods (Kolyunya)
- Enh #15221: Added support for specifying `--camelCase` console options in `--kebab-case` (brandonkelly)
- Enh #15221: Added support for the `--<option> <value>` console option syntax (brandonkelly)
- Enh #15221: Improved the `help/list-action-options` console command output for command options without a description (brandonkelly)
2.0.13.1 November 14, 2017
--------------------------

13
framework/console/Controller.php

@ -12,6 +12,7 @@ use yii\base\Action;
use yii\base\InlineAction;
use yii\base\InvalidRouteException;
use yii\helpers\Console;
use yii\helpers\Inflector;
/**
* Controller is the base class of console command classes.
@ -110,6 +111,14 @@ class Controller extends \yii\base\Controller
unset($params['_aliases']);
}
foreach ($params as $name => $value) {
// Allow camelCase options to be entered in kebab-case
if (!in_array($name, $options, true) && strpos($name, '-') !== false) {
$altName = lcfirst(Inflector::id2camel($name));
if (in_array($altName, $options, true)) {
$name = $altName;
}
}
if (in_array($name, $options, true)) {
$default = $this->$name;
if (is_array($default)) {
@ -544,6 +553,10 @@ class Controller extends \yii\base\Controller
}
$defaultValue = $property->getValue($this);
$tags = $this->parseDocCommentTags($property);
// Display camelCase options in kebab-case
$name = Inflector::camel2id($name, '-', true);
if (isset($tags['var']) || isset($tags['property'])) {
$doc = isset($tags['var']) ? $tags['var'] : $tags['property'];
if (is_array($doc)) {

6
framework/console/Request.php

@ -71,6 +71,7 @@ class Request extends \yii\base\Request
}
$params = [];
$prevOption = null;
foreach ($rawParams as $param) {
if ($endOfOptionsFound) {
$params[] = $param;
@ -84,6 +85,7 @@ class Request extends \yii\base\Request
if ($name !== Application::OPTION_APPCONFIG) {
$params[$name] = isset($matches[2]) ? $matches[2] : true;
$prevOption = &$params[$name];
}
} elseif (preg_match('/^-([\w-]+)(?:=(.*))?$/', $param, $matches)) {
$name = $matches[1];
@ -91,7 +93,11 @@ class Request extends \yii\base\Request
$params[] = $param;
} else {
$params['_aliases'][$name] = isset($matches[2]) ? $matches[2] : true;
$prevOption = &$params['_aliases'][$name];
}
} elseif ($prevOption === true) {
// `--option value` syntax
$prevOption = $param;
} else {
$params[] = $param;
}

4
framework/console/controllers/HelpController.php

@ -125,8 +125,8 @@ class HelpController extends Controller
$this->stdout("\n");
$options = $controller->getActionOptionsHelp($action);
foreach ($options as $argument => $help) {
$description = str_replace("\n", '', addcslashes($help['comment'], ':')) ?: $argument;
$this->stdout('--' . $argument . ':' . $description . "\n");
$description = str_replace("\n", '', addcslashes($help['comment'], ':'));
$this->stdout('--' . $argument . ($description ? ':' . $description : '') . "\n");
}
}

5
tests/framework/console/ControllerTest.php

@ -58,6 +58,11 @@ class ControllerTest extends TestCase
$this->assertEquals('from params', $fromParam);
$this->assertEquals('notdefault', $other);
$params = ['test-array' => 'from params,notdefault'];
list($fromParam, $other) = $controller->runAction('aksi6', $params);
$this->assertEquals('from params', $fromParam);
$this->assertEquals('notdefault', $other);
$params = ['avaliable'];
$message = Yii::t('yii', 'Missing required arguments: {params}', ['params' => implode(', ', ['missing'])]);
$this->expectException('yii\console\Exception');

39
tests/framework/console/RequestTest.php

@ -122,6 +122,45 @@ class RequestTest extends TestCase
],
],
],
// Case: `--<option> <value>` and `-<alias> <value>` syntax
[
'params' => [
'controller/route',
'param1',
'-12345',
'--option1',
'--option2',
'testValue1',
'--option-3',
'testValue2',
'--option_4',
'testValue3',
'-alias1',
'-alias2',
'testValue1',
'-alias-3',
'testValue2',
'-alias_4',
'testValue3',
],
'expected' => [
'route' => 'controller/route',
'params' => [
'param1',
'-12345',
'option1' => true,
'option2' => 'testValue1',
'option-3' => 'testValue2',
'option_4' => 'testValue3',
'_aliases' => [
'alias1' => true,
'alias2' => 'testValue1',
'alias-3' => 'testValue2',
'alias_4' => 'testValue3',
],
],
],
],
[
// PHP does not allow variable name, starting with digit.
// InvalidParamException must be thrown during request resolving:

Loading…
Cancel
Save