mirror of
https://github.com/yiisoft/yii2.git
synced 2025-08-26 06:15:19 +08:00
Fixes #15221: Console improvements
This commit is contained in:

committed by
Alexander Makarov

parent
c9faed4480
commit
0948109a19
@ -11,6 +11,9 @@ Yii Framework 2 Change Log
|
|||||||
- Enh #15135: Automatic completion for help in bash and zsh (Valkeru)
|
- 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 #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 #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
|
2.0.13.1 November 14, 2017
|
||||||
--------------------------
|
--------------------------
|
||||||
|
@ -12,6 +12,7 @@ use yii\base\Action;
|
|||||||
use yii\base\InlineAction;
|
use yii\base\InlineAction;
|
||||||
use yii\base\InvalidRouteException;
|
use yii\base\InvalidRouteException;
|
||||||
use yii\helpers\Console;
|
use yii\helpers\Console;
|
||||||
|
use yii\helpers\Inflector;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Controller is the base class of console command classes.
|
* Controller is the base class of console command classes.
|
||||||
@ -110,6 +111,14 @@ class Controller extends \yii\base\Controller
|
|||||||
unset($params['_aliases']);
|
unset($params['_aliases']);
|
||||||
}
|
}
|
||||||
foreach ($params as $name => $value) {
|
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)) {
|
if (in_array($name, $options, true)) {
|
||||||
$default = $this->$name;
|
$default = $this->$name;
|
||||||
if (is_array($default)) {
|
if (is_array($default)) {
|
||||||
@ -544,6 +553,10 @@ class Controller extends \yii\base\Controller
|
|||||||
}
|
}
|
||||||
$defaultValue = $property->getValue($this);
|
$defaultValue = $property->getValue($this);
|
||||||
$tags = $this->parseDocCommentTags($property);
|
$tags = $this->parseDocCommentTags($property);
|
||||||
|
|
||||||
|
// Display camelCase options in kebab-case
|
||||||
|
$name = Inflector::camel2id($name, '-', true);
|
||||||
|
|
||||||
if (isset($tags['var']) || isset($tags['property'])) {
|
if (isset($tags['var']) || isset($tags['property'])) {
|
||||||
$doc = isset($tags['var']) ? $tags['var'] : $tags['property'];
|
$doc = isset($tags['var']) ? $tags['var'] : $tags['property'];
|
||||||
if (is_array($doc)) {
|
if (is_array($doc)) {
|
||||||
|
@ -71,6 +71,7 @@ class Request extends \yii\base\Request
|
|||||||
}
|
}
|
||||||
|
|
||||||
$params = [];
|
$params = [];
|
||||||
|
$prevOption = null;
|
||||||
foreach ($rawParams as $param) {
|
foreach ($rawParams as $param) {
|
||||||
if ($endOfOptionsFound) {
|
if ($endOfOptionsFound) {
|
||||||
$params[] = $param;
|
$params[] = $param;
|
||||||
@ -84,6 +85,7 @@ class Request extends \yii\base\Request
|
|||||||
|
|
||||||
if ($name !== Application::OPTION_APPCONFIG) {
|
if ($name !== Application::OPTION_APPCONFIG) {
|
||||||
$params[$name] = isset($matches[2]) ? $matches[2] : true;
|
$params[$name] = isset($matches[2]) ? $matches[2] : true;
|
||||||
|
$prevOption = &$params[$name];
|
||||||
}
|
}
|
||||||
} elseif (preg_match('/^-([\w-]+)(?:=(.*))?$/', $param, $matches)) {
|
} elseif (preg_match('/^-([\w-]+)(?:=(.*))?$/', $param, $matches)) {
|
||||||
$name = $matches[1];
|
$name = $matches[1];
|
||||||
@ -91,7 +93,11 @@ class Request extends \yii\base\Request
|
|||||||
$params[] = $param;
|
$params[] = $param;
|
||||||
} else {
|
} else {
|
||||||
$params['_aliases'][$name] = isset($matches[2]) ? $matches[2] : true;
|
$params['_aliases'][$name] = isset($matches[2]) ? $matches[2] : true;
|
||||||
|
$prevOption = &$params['_aliases'][$name];
|
||||||
}
|
}
|
||||||
|
} elseif ($prevOption === true) {
|
||||||
|
// `--option value` syntax
|
||||||
|
$prevOption = $param;
|
||||||
} else {
|
} else {
|
||||||
$params[] = $param;
|
$params[] = $param;
|
||||||
}
|
}
|
||||||
|
@ -125,8 +125,8 @@ class HelpController extends Controller
|
|||||||
$this->stdout("\n");
|
$this->stdout("\n");
|
||||||
$options = $controller->getActionOptionsHelp($action);
|
$options = $controller->getActionOptionsHelp($action);
|
||||||
foreach ($options as $argument => $help) {
|
foreach ($options as $argument => $help) {
|
||||||
$description = str_replace("\n", '', addcslashes($help['comment'], ':')) ?: $argument;
|
$description = str_replace("\n", '', addcslashes($help['comment'], ':'));
|
||||||
$this->stdout('--' . $argument . ':' . $description . "\n");
|
$this->stdout('--' . $argument . ($description ? ':' . $description : '') . "\n");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -58,6 +58,11 @@ class ControllerTest extends TestCase
|
|||||||
$this->assertEquals('from params', $fromParam);
|
$this->assertEquals('from params', $fromParam);
|
||||||
$this->assertEquals('notdefault', $other);
|
$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'];
|
$params = ['avaliable'];
|
||||||
$message = Yii::t('yii', 'Missing required arguments: {params}', ['params' => implode(', ', ['missing'])]);
|
$message = Yii::t('yii', 'Missing required arguments: {params}', ['params' => implode(', ', ['missing'])]);
|
||||||
$this->expectException('yii\console\Exception');
|
$this->expectException('yii\console\Exception');
|
||||||
|
@ -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.
|
// PHP does not allow variable name, starting with digit.
|
||||||
// InvalidParamException must be thrown during request resolving:
|
// InvalidParamException must be thrown during request resolving:
|
||||||
|
Reference in New Issue
Block a user